-
-
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
Behavior of mypy not matching example in PEP 483 #8611
Comments
Derived[Employee] < Base[Manager] is correct, per the PEP. This does seem to be a bug in mypy, which does seem to understand all of the steps in the relationship The following passes:
but this fails So this does seem like a bug. I will also say that it feels pretty low-priority. Mixing co and contra type variables in this way seems like a bad practice! |
I'm not super well-versed in type theory and I don't know the internals of mypy that much, so please bear with me. My understanding is that for an assignment, the variance of the target is the one that applies during type checking:
Is this observation correct? Assuming that it is correct, then what happens - from a type theory perspective - in the context of the Employee/Manager example?
I would assume that mypy behaves as it does because:
What do you think? |
Actually this example in the PEP is kind of "pathological" (btw I wrote it long time ago), because in real life it is hard (if possible at all) to write a non-empty covariant generic subclass of a contravariant base (just because you can only add members). So I would not be worried too much about mypy behavior here. There is actually an old issue to amend example in the PEP to be more realistic (e.g. have an invariant subclass of co- or contravariant base), and make mypy actually prohibit the original example, see #736 |
It appears that there's a discrepancy between one of the examples given in PEP 483 and the actual behavior of mypy (0.750).
So I ran mypy on the following code snippet:
As you can see, mypy complains about the call to func1(), but not for the one to func2().
These results seem to contradict what is stated in PEP 483: "[...] a type checker will also find that, e.g., Derived[Manager] is a subtype of Base[Employee]." But mypy seems to tell us instead that Derived[Employee] < Base[Manager].
Am I understanding things correctly? Is this a bug in mypy, or maybe an error/typo in PEP 483?
The text was updated successfully, but these errors were encountered: