-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Description
Bug Report
Consider the following code, where wrap is some decorator that preserves the function signature:
class Base:
def f(self, a: int) -> int:
return a + 1
def g(self, a: int) -> int:
return a + 1
class Derived(Base):
@wrap
def f(self, a: int) -> int:
return a + 2
def _g(self, a: int) -> int:
return a + 2
g = wrap(_g)The assignment g = wrap(_g) fails with Argument 1 to "wrap" has incompatible type "Callable[[Dervied, int], int]"; expected "Callable[[Base, int], int]". I would have expected both ways of wrapping a function to be the same (ans as far as I can tell they do so at runtime) and therefore both should be allowed.
This is also not a general issue with assignments as the following code type checks:
class Base:
def f(self, a: int) -> int:
return a + 1
class Derived(Base):
def _f(self, a: int) -> int:
return a + 2
f = _fTo Reproduce
https://mypy-play.net/?mypy=master&python=3.11&gist=2455e6265bf5ec9cc5ffac2d95accf98
Note that mypy-play seems to use c660354 rather than the latest commit from master for some reason. The output on current master is slightly different (the type of C.abc is no longer consider def (self: __main__.A, a: builtins.int) -> builtins.int):
test_assign_function_subclass.py:32: error: Incompatible types in assignment (expression has type "Callable[[C, int], int]", base class "A" defined the type as "Callable[[A, int], int]") [assignment]
test_assign_function_subclass.py:50: error: Cannot determine type of "abc" [has-type]
test_assign_function_subclass.py:55: note: Revealed type is "def (self: test_assign_function_subclass.A, a: builtins.int) -> builtins.int"
test_assign_function_subclass.py:56: note: Revealed type is "def (self: test_assign_function_subclass.B, a: builtins.int) -> builtins.int"
test_assign_function_subclass.py:57: error: Cannot determine type of "abc" [has-type]
test_assign_function_subclass.py:57: note: Revealed type is "Any"
test_assign_function_subclass.py:58: note: Revealed type is "def (self: test_assign_function_subclass.D, a: builtins.int) -> builtins.int"
test_assign_function_subclass.py:59: note: Revealed type is "def (self: test_assign_function_subclass.E, a: builtins.int) -> builtins.int"
Found 3 errors in 1 file (checked 1 source file)
Expected Behavior
No error, code type checks
Actual Behavior
main.py:32: error: Argument 1 to "wrap" has incompatible type "Callable[[C, int], int]"; expected "Callable[[A, int], int]" [arg-type]
Your Environment
- Mypy version used: 98cc165
- Mypy command-line flags: None
- Mypy configuration options from
mypy.ini(and other config files): None - Python version used: 3.10 & 3.11