-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Generic[T_co] erroring when T_co = TypeVar("T_co", A, B, covariant=True) and passed subclass of A #8806
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
My understanding is that a typevar with value restriction (like If that's right, the correct fix would be for mypy to disallow having both a value restriction and Have you tried |
Okay @JelleZijlstra I did not know that, and I don't see it stated in PEP 484. I guess this is why. I updated the code snipped in the OP to include the case you mention, which indeed passes (and makes sense that it passes). # Case 3... Success: no issues found
T_co = TypeVar("T_co", bound=Union[A, B], covariant=True) Since I would be happy to make a PR for this, if you think it's worthwhile. If yes, can you give me some pointers, and let me know if you think it's better as a warning or an error? |
I think the But I'd wait for somebody more knowledgeable in the theory behind typevars (maybe @ilevkivskyi?) to confirm that |
I think something like |
In addition to @ilevkivskyi 's post, looking at the OP's linked issue (#8611), they also supply both value restrictions and What I am noticing is that (in both Ivan's example and #8611) the first value restriction is a subclass of the second. Is this how it's supposed to work? I tested those cases here. # Case 4... Success: no issues found
T_co = TypeVar("T_co", A, ASub, covariant=True)
# Case 5... Success: no issues found
T_co = TypeVar("T_co", ASub, A, covariant=True) However, both # Case 6... Success: no issues found
T = TypeVar("T", bound=A) In my opinion, supplying And to circle back to the original question, would you agree that this is a bug? I still think in the original question, that the code is valid, and mypy should not be raising an error. |
Actually, it only makes sense when the classes are related (thanks Ivan for the example!). As I said before, a typevar with value restrictions can only take on the types given as value restrictions, not any subtype of these types. Therefore, in Ivan's example (a covariant typevar restricted to int and float), |
I see where you're going with:
Also, thank you for mentioning mypy sees So then, I guess when supplying both value restrictions and It seems like if the ordering had been Also, to reference the above examples, can you please confirm my understanding? T_co = TypeVar("T_co", ASub, A, covariant=True) # Case 5
|
I don't think the ordering matters; what matters is that I agree with your understanding of case 5. Your second statement is true because |
Thank you for confirming. So in both Ivan's example and my So then that illuminates a potential My original question was built around a misunderstanding and this: T_co = TypeVar("T_co", A, B, covariant=True) # Case 2 (note: A and B aren't related) Do you think it would be a useful feature for I am thinking an error message along the lines of |
Uh oh!
There was an error while loading. Please reload this page.
I am reporting what I believe is a bug in mypy's interpretation of
TypeVar
when passed two constraining types and alsocovariant=True
.I think the below code sample is valid (per Liskov Substitution Principle), and is erroneously being reported as a type error by mypy. Here is my working example:
Potentially Related Issue
I may be re-asking the contents of #8611, but to be honest, I get confused by the core of the issue
Versions
The text was updated successfully, but these errors were encountered: