-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add a 'copy' method to Mapping #7555
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
Conversation
Diff from mypy_primer, showing the effect of this PR on open source code: xarray (https://github.com/pydata/xarray)
+ xarray/core/dataset.py:1068: error: Signature of "copy" incompatible with supertype "Mapping" [override]
+ xarray/core/dataset.py:1068: note: Superclass:
+ xarray/core/dataset.py:1068: note: def copy(self) -> MutableMapping[Any, Any]
+ xarray/core/dataset.py:1068: note: Subclass:
+ xarray/core/dataset.py:1068: note: def copy(self, deep: bool = ..., data: Optional[Mapping[Any, Any]] = ...) -> Dataset
bidict (https://github.com/jab/bidict)
+ bidict/_base.py: note: In member "copy" of class "BidictBase":
+ bidict/_base.py:471:5: error: Return type "BidictBase[KT, VT]" of "copy" incompatible with return type "MutableMapping[KT, VT]" in supertype "Mapping" [override]
urllib3 (https://github.com/urllib3/urllib3)
+ src/urllib3/_request_methods.py:96: error: Unused "type: ignore" comment
+ src/urllib3/connectionpool.py:689: error: Unused "type: ignore" comment
+ src/urllib3/connectionpool.py:690: error: Unused "type: ignore" comment
arviz (https://github.com/arviz-devs/arviz)
+ arviz/data/inference_data.py:1708: error: Return type "InferenceData" of "copy" incompatible with return type "MutableMapping[str, Dataset]" in supertype "Mapping"
- doc/sphinxext/gallery_generator.py:370: error: Call to untyped function "create_thumbnail" in typed context
- doc/sphinxext/gallery_generator.py:375: note: (Skipping most remaining errors due to unresolved imports or missing stubs; fix these first)
+ doc/sphinxext/gallery_generator.py:370: note: (Skipping most remaining errors due to unresolved imports or missing stubs; fix these first)
|
The only error that appears as the result of this change (excluding the primer) seems to be tied to the same piece of code: Lines 1209 to 1211 in f40747f
typeshed/stdlib/typing_extensions.pyi Lines 63 to 68 in f40747f
This seems to be because |
|
That's... strange then. Isn't This sounds similar to #7153 where |
As @srittau said, there is no You can suggest adding it to CPython first, but it may be difficult because the ABC doesn't provide a way to create an instance of an arbitrary concrete Mapping. |
This is close to how It might help to compare this to |
The
copy
method is missing fromMapping
, which is supposedly to be an immutable representation of a dictionary. Since simple shallow copy operation doesn't modify anything, it belongs inMapping
instead ofMutableMapping
. This PR adds this missing method. Using thecopy
method avoidsfrom copy import copy
imports for creating a quick copy of the original dictionary. The return type is aMutableMapping
since this method is usually used to create a local mutable copy to be passed somewhere, but if immutability needs to be preserved, one can still assign it to aMapping
variable type or use a cast.I ran into this while trying to deal with
TypedDict
not being compatible withDict[str, Any]
(per python/mypy#4976) and converting a JSON-saving function to accept a mapping, which is then modified before saving.