Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Uh oh!
There was an error while loading. Please reload this page.
Conversation
DevilXD commented Mar 26, 2022 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
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) |
DevilXD commented Mar 26, 2022 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
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 |
srittau commented Mar 26, 2022
|
DevilXD commented Mar 26, 2022
That's... strange then. Isn't This sounds similar to #7153 where |
JelleZijlstra commented Mar 26, 2022
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. |
Akuli commented Mar 26, 2022 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
This is close to how It might help to compare this to |
The
copymethod 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 inMappinginstead ofMutableMapping. This PR adds this missing method. Using thecopymethod avoidsfrom copy import copyimports for creating a quick copy of the original dictionary. The return type is aMutableMappingsince 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 aMappingvariable type or use a cast.I ran into this while trying to deal with
TypedDictnot 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.