You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Wrapping a function that accepts TypeVar arguments in a staticmethod will fool mypy into thinking that the first TypeVar argument is of type <classname> | None. This only applies if a function is wrapped, not when a method decorated.
To Reproduce
fromtypingimportTypeVarT=TypeVar("T")
deffoo(a: T) ->T:
returnaclassFoo:
foo=staticmethod(foo)
@staticmethoddefbar(a: T) ->T:
returnareveal_type(foo) # Revealed type is "def [T] (a: T`-1) -> T`-1"reveal_type(Foo.foo) # Revealed type is "def [T] (a: Union[__main__.Foo, None]) -> Union[__main__.Foo, None]"reveal_type(Foo.bar) # Revealed type is "def [T] (a: T`-1) -> T`-1"Foo.foo("a") # error: Argument 1 has incompatible type "str"; expected "Foo | None" [arg-type]Foo.bar("a") # no issue
The issue persists if the TypeVar is not the first variable, e.g. def foo(a: str, b: T) has the same issue. If there are multiple TypeVars, then the issue is only with the first one, e.g. def foo(a: T1, b: T2).
The issue was introduced in mypy 1.2.0 -- the example works fine in 1.1.1.
Expected Behavior
I would expect mypy to behave identical for the invocations of staticmethod as an applied function and as a decorator. Both should behave as the staticmethod decorator does (Foo.bar).
Actual Behavior
Mypy incorrectly thinks that the first TypeVar becomes Foo | None, whereas it should still be a fully generic TypeVar.
Your Environment
Mypy version used: 1.2.0 - 1.4.1
Mypy command-line flags:
Mypy configuration options from mypy.ini (and other config files):
Python version used: 3.11
The text was updated successfully, but these errors were encountered:
Bug Report
Wrapping a function that accepts
TypeVar
arguments in astaticmethod
will fool mypy into thinking that the first TypeVar argument is of type<classname> | None
. This only applies if a function is wrapped, not when a method decorated.To Reproduce
https://mypy-play.net/?mypy=latest&python=3.11&gist=2c0e679c14a40c36932905bdc4299cc1
The issue persists if the TypeVar is not the first variable, e.g.
def foo(a: str, b: T)
has the same issue. If there are multiple TypeVars, then the issue is only with the first one, e.g.def foo(a: T1, b: T2)
.The issue was introduced in mypy 1.2.0 -- the example works fine in 1.1.1.
Expected Behavior
I would expect mypy to behave identical for the invocations of
staticmethod
as an applied function and as a decorator. Both should behave as thestaticmethod
decorator does (Foo.bar
).Actual Behavior
Mypy incorrectly thinks that the first
TypeVar
becomesFoo | None
, whereas it should still be a fully generic TypeVar.Your Environment
mypy.ini
(and other config files):The text was updated successfully, but these errors were encountered: