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
{{ message }}
Repository navigation
staticmethod wrapped around function sets first TypeVar argument to class name #15769
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):
Activity
b-kamphorst commentedon Jul 27, 2023
Possibly related to #15015 and #15737
b-kamphorst commentedon Sep 26, 2023
This issue is resolved by #15898, so whatever version comes after 1.5.1 will have this fix included. Great effort by @ilevkivskyi!