-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Make Mapping covariant in key #1114
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
See also #738. (Which requested covariance in the value -- that's been implemented for a while now.) |
Actually this would not really be safe for key types, as a custom subclass could allow mutation via the |
Maybe define a supertype of
|
see also: python/typing#445 |
Is there anything remaining for this issue? I think it may be OK to close at this point. |
Agree, Mapping has to remain invariant in the key type. |
I've run into a case where I believe it'd be nice to have a covariant key. I have a super class with: and a subclass with: IIUC, the later is valid under LSP. It's nice for people directly using the subclass to know they're always going to have UUIDs so they don't have to handle both types. While perhaps there are reasons we don't want to make Mapping's key covariant, is the conclusion of this bug that we shouldn't be able to express my pattern above? (I see pkch had one suggestion above). Thank you. (Also apologies if it's bad form to comment on a closed issue, but it felt worse opening a new one.) |
Mapping should be covariant. See discussion at #1113.
Note that the key type is used as an argument type to
__getitem__
andget
, so mypy may complain about a covariant key type variable, but we can just ignore that error in the stub, as covariance is safe in this case.Mapping
doesn't support mutation operations and__getitem__
can fail even in fully type safe code.The text was updated successfully, but these errors were encountered: