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
So most of this is expected to me, since List is invariant (try using Sequence instead!).
The one bit that isn't expected to me is that the result of the type join depends on the order of the types, i.e., that the second and third reveals don't match below:
changed the title [-]Return Generic[Any] from `yield from` fails unless a Union is involved[/-][+]Join of types depends on order of types[/+]on May 7, 2021
So my recommendation would have been to make your generic class covariant (https://mypy.readthedocs.io/en/stable/generics.html#variance-of-generic-types), which in theory would make mypy infer A[object], instead of object. Unfortunately, it looks like mypy still doesn't do the join correctly; the issue for that is #5269 (and I think there's a PR open for it).
Generally speaking, mypy doesn't infer Any types automatically, since that's an easy way to lead to false negatives. I think your best option is to use # type: ignore, or to cast, e.g.: yield from (cast(A[Any], b), c, bc)
Activity
hauntsaninja commentedon May 7, 2021
So most of this is expected to me, since List is invariant (try using Sequence instead!).
The one bit that isn't expected to me is that the result of the type join depends on the order of the types, i.e., that the second and third reveals don't match below:
[-]Return Generic[Any] from `yield from` fails unless a Union is involved[/-][+]Join of types depends on order of types[/+]farmio commentedon May 7, 2021
I'm not sure where to try to use Sequence. As stated above
listis only used for example. The problem in my code occurs with custom generics.Here is an example:
I just want to iterate over a couple of instances derived from
A.But my understanding of mypy is not even close enough to say if this made any difference.
hauntsaninja commentedon May 7, 2021
So my recommendation would have been to make your generic class covariant (https://mypy.readthedocs.io/en/stable/generics.html#variance-of-generic-types), which in theory would make mypy infer
A[object], instead ofobject. Unfortunately, it looks like mypy still doesn't do the join correctly; the issue for that is #5269 (and I think there's a PR open for it).Generally speaking, mypy doesn't infer Any types automatically, since that's an easy way to lead to false negatives. I think your best option is to use
# type: ignore, or tocast, e.g.:yield from (cast(A[Any], b), c, bc)farmio commentedon May 7, 2021
Thank you very much, I'll look into variance.
As a workaround
works perfectly fine without ignore.
Or even use
c: Union[C | C]which is a little bit too hacky for my taste 🙃hauntsaninja commentedon May 21, 2023
Closing as a duplicate of #3339