-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Union[Generic[T], T]] does not work as expected as a parameter in the signature of a generic function #6417
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
Comments
Yeah, inferring against unions is tricky. We have couple similar issues, but there is no simple solution. Just to illustrate why it is tricky, in your case Adding an upper bound to So the only option for you is to use overloads: @overload
def q2(x: Z[F]) -> F: ...
@overload
def q2(x: F) -> F: ...
def q2(x):
if isinstance(x, Z):
return x.y
else:
return x
reveal_type(q2(p)) # Revealed type is "str" which is a bit verbose but removes the ambiguity I explained above, because for overloads the variants are chosen in the order of appearance. |
Thank you so much for the rapid response! I think I understand. If I'm reading this correctly, because To prevent the issue wholesale, then, when constructing Z we would need to make it generic on a typevar that would prevent Is that an accurate understanding of the situation? |
Yes. |
For what it's worth, this is exactly the scenario that we originally encountered this bug in, since intuitively it did make sense that the inference for an unbound type var would be "hard" (even though it's definitely computable given the inputs here and mypy should totally do it!) |
Hm, I just looked at this again, and it looks like adding an upper bound fixes the issue on master. I will add a regression test for this, but otherwise it looks like there are no action items here. |
Hm, I probably messed up something, I am trying it again and it doesn't work. It looks like there however might be a simple solution. |
Bug
or a mock-up repro if the source is private. We would appreciate
if you try to simplify your case to a minimal repro.
When creating a generic function that has a signature where one parameter is a union on a generic of a typevar and that typevar, calling with the generic causes an error.
Z[F]
satisfiesUnion[Z[F], F]
Do you see the same issue after installing mypy from Git master?
Python 3.6.6, using mypy 0.660; same issue happens with master.
No flags
The text was updated successfully, but these errors were encountered: