-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Invalid infer generic type for contravariant TypeVar inside function call #6482
Comments
Seems to be invariant TypeVar also affected. Inside function call they works as covariants
|
The same is for invariants. |
It looks like here are several misconceptions about what variance is, and about how inference works. Let me clarify the situation:
|
@ilevkivskyi thanks for a comprehensive explanation! I'm still not sure if I got some things right though, could you elaborate please? I used a different example because the original example by @unknownlighter didn't have any specific examples of variance violation (i.e. assignment), perhaps it would be clear for other people if one includes them. from typing import Generic, TypeVar
X = TypeVar('X')
class Box(Generic[X]):
def __init__(self, x: X) -> None:
self.x = x
def consume(v: Box[BaseException]) -> None:
v.x = Exception("boom!")
consume(Box(SystemExit(1))) # I also expected type error here, but it passes mypy
By that, do you mean that because consume(b := Box(SystemExit(123))) # mypy gives type error here
print(b.x.code) # that would be AttributeError in runtime, Exception doesn't have 'code' I guess there are two potential options:
So the external context thing only applies for return values, not arguments? And finally, if I make Thanks for you time! |
I appreciate the gratitude, but no, sorry. I don't have tine to engage in lengthy philosophical discussions. If you have complete concrete examples of false negatives or false positives, please open separate issues. If you want to learn more about mypy to became a contributor, the better alternative is to try fixing some issues. |
Reproduces only on contravariants. Covariants seems to be OK.
Tested on:
mypy-0.680+dev.69eaf88a0b03e9f491cc22534c2d30f1d21832b2
The text was updated successfully, but these errors were encountered: