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 1 commit
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
30 changes: 18 additions & 12 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 @@ -1044,8 +1036,13 @@ def verify_paramspecexpr(
return


def _verify_readonly_property(stub: nodes.Decorator, runtime: Any) -> Iterator[str]:
def _verify_property(stub: nodes.Decorator, runtime: Any) -> Iterator[str]:
assert stub.func.is_property
yield from _verify_readonly_property(stub, runtime)
yield from _verify_abstract_status(stub.func, runtime)


def _verify_readonly_property(stub: nodes.Decorator, runtime: Any) -> Iterator[str]:
if isinstance(runtime, property):
return
if inspect.isdatadescriptor(runtime):
Expand All @@ -1066,6 +1063,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 @@ -1122,7 +1128,7 @@ def verify_decorator(
yield Error(object_path, "is not present at runtime", stub, runtime)
return
if stub.func.is_property:
for message in _verify_readonly_property(stub, runtime):
for message in _verify_property(stub, runtime):
yield Error(object_path, message, stub, runtime)
return

Expand Down
14 changes: 14 additions & 0 deletions mypy/test/teststubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1391,6 +1391,7 @@ def test_abstract_properties(self) -> Iterator[Case]:
yield Case(
stub="""
class AP1:
@property
def some(self) -> int: ...
""",
runtime="""
Expand All @@ -1401,6 +1402,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