-
Notifications
You must be signed in to change notification settings - Fork 288
Distribute return type over union #2047
-
Inspired by microsoft/pyright#10673, I looked at this simple example: Code sample in pyright playground
from collections.abc import Sequence def listify[T](x: Sequence[T]) -> list[T]: return list(x) def func(x: Sequence[str] | Sequence[int]): y = listify(x) reveal_type(y) # list[str | int] print(y)
It feels like the revealed type is not actually what we want here. The desirable inference is imo list[str] | list[int].
Is there simple way to annotate listify to make the return type distribute over unions, or is this a general type-checker limitation?
What seems to happen here is that we assign Sequence[str] | Sequence[int] <: Sequence[str | int] = Sequence[T], so we pick T = int | str. However, in principle wouldn't it be more precise if the type checker, when seeing a UnionType as an argument, passes every member separately and yields the UnionType of the individual return types?
I guess with multiple arguments this runs into combinatoric explosion...
Beta Was this translation helpful? Give feedback.