Skip to content

Check override compatibility for dynamically typed methods #17274

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

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 1 addition & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1004,7 +1004,7 @@ def _visit_func_def(self, defn: FuncDef) -> None:
"""Type check a function definition."""
self.check_func_item(defn, name=defn.name)
if defn.info:
if not defn.is_dynamic() and not defn.is_overload and not defn.is_decorated:
if not defn.is_overload and not defn.is_decorated:
# If the definition is the implementation for an
# overload, the legality of the override has already
# been typechecked, and decorated methods will be
Expand Down
14 changes: 12 additions & 2 deletions test-data/unit/check-abstract.test
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,13 @@ class I(metaclass=ABCMeta):
def g(self, x): pass
class A(I):
def f(self, x): pass
def g(self, x, y): pass
def g(self, x, y): pass # Fail
[out]
main:10: error: Signature of "g" incompatible with supertype "I"
main:10: note: Superclass:
main:10: note: def g(self, x: Any) -> Any
main:10: note: Subclass:
main:10: note: def g(self, x: Any, y: Any) -> Any

[case testAbstractClassWithImplementationUsingDynamicTypes]
from abc import abstractmethod, ABCMeta
Expand All @@ -507,8 +512,13 @@ class I(metaclass=ABCMeta):
def g(self, x: int) -> None: pass
class A(I):
def f(self, x): pass
def g(self, x, y): pass
def g(self, x, y): pass # Fail
[out]
main:10: error: Signature of "g" incompatible with supertype "I"
main:10: note: Superclass:
main:10: note: def g(self, x: int) -> None
main:10: note: Subclass:
main:10: note: def g(self, x: Any, y: Any) -> Any


-- Special cases
Expand Down
5 changes: 5 additions & 0 deletions test-data/unit/check-dynamic-typing.test
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,11 @@ class A(B):
def f(self, x, y): # dynamic function not type checked
x()
[out]
main:5: error: Signature of "f" incompatible with supertype "B"
main:5: note: Superclass:
main:5: note: def f(self, x: A) -> None
main:5: note: Subclass:
main:5: note: def f(self, x: Any, y: Any) -> Any

[case testInvalidOverrideArgumentCountWithImplicitSignature2]
import typing
Expand Down
14 changes: 14 additions & 0 deletions test-data/unit/check-functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -3228,3 +3228,17 @@ class A:
reveal_type(A.f) # N: Revealed type is "__main__.something_callable"
reveal_type(A().f) # N: Revealed type is "builtins.str"
[builtins fixtures/property.pyi]

[case testFinalOverrideOnUntypedDef]
# flags: --python-version 3.12

from typing import final

class Base:
@final
def foo(self):
pass

class Derived(Base):
def foo(self): # E: Cannot override final attribute "foo" (previously declared in base class "Base")
pass
Loading