Open couling opened 1 week ago
I am very interested in this task, Can you assign this task to me?
Looking at this again, the problem I've faced is perhapse a little deeper than I'd first thought. What I've written above is certainly true, but I've missed one point which may go beyond MyPy's capability.
To properly evaluate the above code example (which is safe, and theoretically type safe), there needs to be some level of conditional evaluation.
Specifically for TypeVar("_T", bound=A | B)
, the only way to correctly determine the type safety is to evaluate the function under the condition that _T
is A
and seperately under the condition that _T
is B
. It could do this in two passes, one for _T==A
and one for T==B
, or it could do this by maintaining some notion of conditional typing: IE foo
is type A
if _T
is A
else foo
is type B
if _T
is B
...
Since both of these may be a segnificant extension to MyPy's logic, I'm now a little less certain whether what I need is a feature request. As it stands, I think this bug report is still self contained, even if I'm unsure whether it will fix my problem.
Bug Report
MyPy incorrectly asses the type of list comprehensions built from union types. I can't see a way to make this pass typing checks. This and it even affects
TypeVar
. My only option is a# type: ignore
.The problem arises where a variable
foo
has a type ofIterable[Bar] | Iterable[Baz]
and this is then used as part of a list comprehension[v for v in foo]
. MyPy incorrectly evaluates the list comprehension aslist[Bar | Baz]
notlist[Bar] | list[Baz]
.I had thought passing a
TypeVar
might be a simple work around, but it appears the TypeVar is stripped off and only it's binding is maintained.To Reproduce
I ran into this with code like this:
With the MyPy error:
Expected Behavior
A list comprehension formed from iterable
Iterable[Foo] | Iterable[Bar]
should have typelist[Foo] | list[Bar]
Actual Behavior
A list comprehension formed from iterable
Iterable[Foo] | Iterable[Bar]
is assessed as having typelist[Foo | Bar]
Your Environment
mypy.ini
(and other config files):