diff --git a/mypy/stubtest.py b/mypy/stubtest.py index b0ef94e62480..86f1a63c458b 100644 --- a/mypy/stubtest.py +++ b/mypy/stubtest.py @@ -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) diff --git a/mypy/test/teststubtest.py b/mypy/test/teststubtest.py index d39812b5f9b6..c909a0bf9c8d 100644 --- a/mypy/test/teststubtest.py +++ b/mypy/test/teststubtest.py @@ -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, ) @@ -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,