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
Passing a union of collections to a generic function that should preserve the type argument erases it instead. (Or should I say it consolidates it to object?)
Revealed type is "Union[builtins.set[builtins.int], builtins.set[builtins.str]]"
Revealed type is "Union[builtins.list[builtins.int], builtins.list[builtins.str]]"
I hope this is reasonable. I mean, the type argument is either one or the other and nothing else.
Actual Behavior
Revealed type is "builtins.set[builtins.object]"
Revealed type is "builtins.list[builtins.object]"
Environment
Python 3.10.8 on Linux; mypy --strict version 0.982
More examples
Clearly mypy is capable of preserving the type argument, when there is no union of iterables passed to set:
reveal_type(set([1, 2])) # Revealed type is "builtins.set[builtins.int]"
It also does not seem to have anything to do with the wrong typeshed stubs, as the same problem occurs with custom generic functions:
fromcollections.abcimportIterablefromtypingimportTypeVarT=TypeVar("T")
deff(i: Iterable[T]) ->T:
...
z: list[int] |list[str]
reveal_type(f(z)) # Revealed type is "builtins.object"
The following gives me an error:
defmy_set(a: list[int] |list[str]) ->set[int] |set[str]:
returnset(a) # Incompatible return value type (got "Set[object]", expected "Union[Set[int], Set[str]]")
And yes, I know this would be better solved with a generic type variable instead of the unions. It is just to provide an additional demonstration of what I consider a bug.
My description of the problem may be poorly worded. Please feel free to correct me, so that I can describe it more precisely.
Bug Report
Passing a union of collections to a generic function that should preserve the type argument erases it instead. (Or should I say it consolidates it to
object
?)To Reproduce
Expected Behavior
I hope this is reasonable. I mean, the type argument is either one or the other and nothing else.
Actual Behavior
Environment
Python 3.10.8 on Linux;
mypy --strict
version0.982
More examples
Clearly
mypy
is capable of preserving the type argument, when there is no union of iterables passed toset
:It also does not seem to have anything to do with the wrong typeshed stubs, as the same problem occurs with custom generic functions:
The following gives me an error:
And yes, I know this would be better solved with a generic type variable instead of the unions. It is just to provide an additional demonstration of what I consider a bug.
My description of the problem may be poorly worded. Please feel free to correct me, so that I can describe it more precisely.
This issue seems vaguely related: #6463
The text was updated successfully, but these errors were encountered: