Skip to content

stubtest: Detect abstract properties mismatches #13647

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions mypy/stubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -870,16 +870,8 @@ def verify_funcitem(
return

if isinstance(stub, nodes.FuncDef):
stub_abstract = stub.abstract_status == nodes.IS_ABSTRACT
runtime_abstract = getattr(runtime, "__isabstractmethod__", False)
# The opposite can exist: some implementations omit `@abstractmethod` decorators
if runtime_abstract and not stub_abstract:
yield Error(
object_path,
"is inconsistent, runtime method is abstract but stub is not",
stub,
runtime,
)
for error_text in _verify_abstract_status(stub, runtime):
yield Error(object_path, error_text, stub, runtime)

for message in _verify_static_class_methods(stub, runtime, object_path):
yield Error(object_path, "is inconsistent, " + message, stub, runtime)
Expand Down Expand Up @@ -1066,6 +1058,15 @@ def _verify_readonly_property(stub: nodes.Decorator, runtime: Any) -> Iterator[s
yield "is inconsistent, cannot reconcile @property on stub with runtime object"


def _verify_abstract_status(stub: nodes.FuncDef, runtime: Any) -> Iterator[str]:
stub_abstract = stub.abstract_status == nodes.IS_ABSTRACT
runtime_abstract = getattr(runtime, "__isabstractmethod__", False)
# The opposite can exist: some implementations omit `@abstractmethod` decorators
if runtime_abstract and not stub_abstract:
item_type = "property" if stub.is_property else "method"
yield f"is inconsistent, runtime {item_type} is abstract but stub is not"


def _resolve_funcitem_from_decorator(dec: nodes.OverloadPart) -> nodes.FuncItem | None:
"""Returns a FuncItem that corresponds to the output of the decorator.

Expand Down Expand Up @@ -1124,6 +1125,8 @@ def verify_decorator(
if stub.func.is_property:
for message in _verify_readonly_property(stub, runtime):
yield Error(object_path, message, stub, runtime)
for message in _verify_abstract_status(stub.func, runtime):
yield Error(object_path, message, stub, runtime)
return

func = _resolve_funcitem_from_decorator(stub)
Expand Down
15 changes: 15 additions & 0 deletions mypy/test/teststubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1382,6 +1382,7 @@ def some(self) -> None: ...

@collect_cases
def test_abstract_properties(self) -> Iterator[Case]:
# TODO: test abstract properties with setters
yield Case(
stub="from abc import abstractmethod",
runtime="from abc import abstractmethod",
Expand All @@ -1391,6 +1392,7 @@ def test_abstract_properties(self) -> Iterator[Case]:
yield Case(
stub="""
class AP1:
@property
def some(self) -> int: ...
""",
runtime="""
Expand All @@ -1401,6 +1403,19 @@ def some(self) -> int: ...
""",
error="AP1.some",
)
yield Case(
stub="""
class AP1_2:
def some(self) -> int: ... # missing `@property` decorator
""",
runtime="""
class AP1_2:
@property
@abstractmethod
def some(self) -> int: ...
""",
error="AP1_2.some",
)
yield Case(
stub="""
class AP2:
Expand Down