-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Join of types depends on order of types #10442
Comments
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:
|
yield from
fails unless a Union is involved
I'm not sure where to try to use Sequence. As stated above Here is an example: from __future__ import annotations
from typing import Any, Generic, Iterator, TypeVar
ValueType = TypeVar("ValueType")
class A(Generic[ValueType]):
def test(self, a: ValueType) -> ValueType:
return a
class B(A[int]):
pass
class C(A[str]):
pass
b: B
c: C
bc: B | C
def check() -> Iterator[A[Any]]:
yield from (b, c, bc) # error: Incompatible types in "yield from" (actual type "object", expected type "A[Any]")
yield from (b, bc, c) # passes I just want to iterate over a couple of instances derived from But my understanding of mypy is not even close enough to say if this made any difference. |
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 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 |
Thank you very much, I'll look into variance. As a workaround def check() -> Iterator[A[Any]]:
yield b
yield c works perfectly fine without ignore. Or even use |
Closing as a duplicate of #3339 |
Bug Report
When using
yield from
to return an Iterator of any generics mypy fails to identify the type and falls back toobject
unless a Union is involved.To Reproduce
Using
list[...]
as example. Behavior is the same with custom Generics.Expected Behavior
I'd expect to infer the type to
list[Any]
orlist[str | int]
foryield from ( l_int, l_str,)
.Actual Behavior
(Write what happened.)
Your Environment
mypy.ini
(and other config files):The text was updated successfully, but these errors were encountered: