-
-
Notifications
You must be signed in to change notification settings - Fork 1.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
collections.ChainMap
has wrong type hint for self.maps
, rendering it "immutable"
#6042
Comments
collections.Chainmap
has wrong type hint for self.maps
, rendering it "immutable"collections.ChainMap
has wrong type hint for self.maps
, rendering it "immutable"
But what if you make a ChainMap over immutable Mappings? I'm not sure there is a good solution here. |
That's a good point, @JelleZijlstra. However, I still believe we should change the type hints to
|
To respond directly to your question: "But what if you make a ChainMap over immutable Mappings?" I think the answer is: " Pragmatically, immutable mappings are rare in real Python code, AFAIK. There are no full-fledged implementations in the standard library, only proxies that behave as such. |
We do err on the side of false negatives, but in this case that can go either way, because you could get new false positives if you make a ChainMap over an immutable Mapping. The mypy-primer output on your PR shows issues either way: the change makes some casts and type ignores in pandas unnecessary, but introduces new errors in code in chess and edgedb that uses immutable ChainMaps. Whatever decision we make will cause some false positives. You're right though that ChainMap itself is explicitly declared as a MutableMapping and has some methods that don't make sense if the inner mappings are immutable, so I'm inclined to accept your change, though I'm curious if other maintainers have opinions. The ideal from a typing perspective would be a separate concepts of mutable and immutable ChainMaps, but I can't think of a sensible way to do that in the stubs. |
Can't we make |
How would we access key and value types in e.g. Also, how would inheritance work? Would it inherit from |
I think that would require python/typing#548 because the mapping type is already generic Can we offer an |
Ahem, this gives me new mypy complaints on my code base. From my understanding:
I don't think we can have correct hints for the |
That sounds reasonable to me. I would be in favour of:
Also just to call something out:
While I'm not sure we've stated this out loud, historically, typeshed has very consistently preferred false positives for subclassers of classes over false positives for other users of classes. |
I'm notoriously bad with these overloads but at least I got the double underscores right. What are pro/cons for the
I personally only work with copies of |
IIUC You can do both of those things with |
Opened #8430 to get more feedback and because closed issues cannot move anymore |
Typeshed annotates
collections.ChainMap
as:The
self.maps
attribute holds the chained mappings. It should be annotated aslist[MutableMapping[_KT, _VT]]
.The
*maps
parameter of__init__
and them
parameter ofnew_child
need to beMutableMapping[_KT, _VT]
as well.As it is, the implementation in the standard library violates the type hint for
self.maps
in its__setitem__
method:The text was updated successfully, but these errors were encountered: