Skip to content
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

Allow stubtest to raise errors on abstract state mismatch #13323

Merged
merged 6 commits into from
Aug 10, 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
7 changes: 7 additions & 0 deletions mypy/stubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,13 @@ def verify_funcitem(
if not callable(runtime):
return

if isinstance(stub, nodes.FuncDef):
stub_abstract = stub.abstract_status == nodes.IS_ABSTRACT
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can safely skip nodes.IMPLICITLY_ABSTRACT here. Because it can only happen for a protocol.

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, "runtime method is abstract, but stub is not", stub, runtime)
sobolevn marked this conversation as resolved.
Show resolved Hide resolved

for message in _verify_static_class_methods(stub, runtime, object_path):
yield Error(object_path, "is inconsistent, " + message, stub, runtime)

Expand Down
34 changes: 34 additions & 0 deletions mypy/test/teststubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1264,6 +1264,40 @@ def test_type_var(self) -> Iterator[Case]:
)
yield Case(stub="C = ParamSpec('C')", runtime="C = ParamSpec('C')", error=None)

@collect_cases
def test_abstract_methods(self) -> Iterator[Case]:
yield Case(
stub="""
from abc import abstractmethod

class A1:
def some(self) -> None: ...

class A2:
@abstractmethod
def some(self) -> None: ...

class A3:
@abstractmethod
def some(self) -> None: ...
""",
runtime="""
from abc import abstractmethod

class A1:
@abstractmethod
def some(self) -> None: ...

class A2:
@abstractmethod
def some(self) -> None: ...

class A3:
def some(self) -> None: ...
""",
error="A1.some",
)


def remove_color_code(s: str) -> str:
return re.sub("\\x1b.*?m", "", s) # this works!
Expand Down