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

Stubtest: check that the stub is abstract if the runtime is, even when the stub is an overloaded method #14955

Merged
merged 1 commit into from
Mar 24, 2023
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
11 changes: 11 additions & 0 deletions mypy/stubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,17 @@ def verify_overloadedfuncdef(
if not callable(runtime):
return

# mypy doesn't allow overloads where one overload is abstract but another isn't,
# so it should be okay to just check whether the first overload is abstract or not.
#
# TODO: Mypy *does* allow properties where e.g. the getter is abstract but the setter is not;
# and any property with a setter is represented as an OverloadedFuncDef internally;
# not sure exactly what (if anything) we should do about that.
first_part = stub.items[0]
if isinstance(first_part, nodes.Decorator) and first_part.is_overload:
for msg in _verify_abstract_status(first_part.func, runtime):
yield Error(object_path, msg, stub, runtime)

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

Expand Down
58 changes: 55 additions & 3 deletions mypy/test/teststubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1475,7 +1475,10 @@ def test_metaclass_abcmeta(self) -> Iterator[Case]:
@collect_cases
def test_abstract_methods(self) -> Iterator[Case]:
yield Case(
stub="from abc import abstractmethod",
stub="""
from abc import abstractmethod
from typing import overload
""",
runtime="from abc import abstractmethod",
error=None,
)
Expand Down Expand Up @@ -1504,15 +1507,64 @@ def some(self) -> None: ...
""",
error=None,
)
# Runtime can miss `@abstractmethod`:
yield Case(
stub="""
class A3:
@overload
def some(self, other: int) -> str: ...
@overload
def some(self, other: str) -> int: ...
""",
runtime="""
class A3:
@abstractmethod
def some(self, other) -> None: ...
""",
error="A3.some",
)
yield Case(
stub="""
class A4:
@overload
@abstractmethod
def some(self, other: int) -> str: ...
@overload
@abstractmethod
def some(self, other: str) -> int: ...
""",
runtime="""
class A4:
@abstractmethod
def some(self, other) -> None: ...
""",
error=None,
)
yield Case(
stub="""
class A5:
@abstractmethod
@overload
def some(self, other: int) -> str: ...
@abstractmethod
@overload
def some(self, other: str) -> int: ...
""",
runtime="""
class A5:
@abstractmethod
def some(self, other) -> None: ...
""",
error=None,
)
# Runtime can miss `@abstractmethod`:
yield Case(
stub="""
class A6:
@abstractmethod
def some(self) -> None: ...
""",
runtime="""
class A3:
class A6:
def some(self) -> None: ...
""",
error=None,
Expand Down