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
I have a generic class and function, which return Union containing TypeVar as in generic. Mypy will give error [arg-type] if I pass function result in another function which gets a object. But if i save the result in a variable and then pass it in function, mypy won't show error. It's very counterintuitive
To Reproduce
fromtypingimport*T=TypeVar("T")
D=TypeVar("D")
classKey: ...
classFancyKey(Generic[T]): ...
defget(key: FancyKey[T], default: D) ->D|T:
...
returndefaultdeffoo(value: object) ->Any: ...
key=FancyKey[Key]()
foo(
get(key, default=None)
) # mypy: Argument 1 to "bar" of "Bar" has incompatible type "Foo[Lab]"; expected "Foo[object]" [arg-type]treasure=get(key, default=None) # no errorfoo(treasure) # no error
mypy: Argument 1 to "bar" of "Bar" has incompatible type "Foo[Lab]"; expected "Foo[object]" [arg-type]
If a return-type function won't be generic or function will return not a Union - no error will be received
If type of value change to Any - no error will be received
This has to do with whether the signature of get is (over)inferred from the type context (yielding def (key: FancyKey[object], default: object) -> object) or from the arguments types (yielding def (key: FancyKey[Key], default: None) -> Key | None). In the first case, passing FancyKey[Key] becomes invalid, since you declared FancyKey[T] to be invariant.
Bug Report
I have a generic class and function, which return Union containing TypeVar as in generic. Mypy will give error [arg-type] if I pass function result in another function which gets a object. But if i save the result in a variable and then pass it in function, mypy won't show error. It's very counterintuitive
To Reproduce
Playground
Actual Behavior
mypy: Argument 1 to "bar" of "Bar" has incompatible type "Foo[Lab]"; expected "Foo[object]" [arg-type]
If a return-type function won't be generic or function will return not a Union - no error will be received
If type of
value
change to Any - no error will be receivedMaybe this issue related with another
Your Environment
mypy.ini
(and other config files): no configThe text was updated successfully, but these errors were encountered: