Add MappingView.mapping for parity with dictview#28892
Add MappingView.mapping for parity with dictview#28892jab wants to merge 1 commit intopython:mainfrom
Conversation
As of bpo-40890 / pythongh-20749.
|
Please open a new issue. bpo-40890 is closed long time ago, and it was about feature added in 3.10. Why not use property? |
|
Also, new feature needs tests, documentation, etc. |
|
Sure, just want to hear if @rhettinger agrees with this before I put any more time into it. |
|
This is a nice thought but can't go forward. The ABCs specify a minimum API requirement, not just for derived classes but also for registered classes. Accordingly, we cannot ever expand the API because formerly compliant APIs would stop being compliant. In general, concrete APIs such as dictview are allowed to add extra capabilities, but the ABCs themselves cannot expand once they are published. If an extension is truly needed, then we could offer an new ABC with the extra capability. It the case of the mapping attribute, it likely isn't worth it. |
|
I didn’t think this changes the contract for any class registered as a MappingView. Only methods marked |
|
Going back to the https://docs.python.org/3/library/collections.abc.html docs, I guess you’re considering the new |
|
@rhettinger, your concern for the |


As of gh-20749, built-in dictviews now have a
mappingattribute.This PR adds the same to
collections.abc.MappingView, so that non-builtin mappings can get feature parity, not to mention a consistent interface, just by inheriting fromcollections.abc.Mapping(as many already do), without having to add any boilerplate code of their own.From the associated original issue bpo-40890, it looks like this was never discussed or considered. Perhaps this would have been added at the same time as gh-20749 if it had been part of the original discussion. Submitting this (currently draft) 3-line PR as a starting point for further discussion. The changes in this PR already make the following code work:
$ ./python.exe -VV Python 3.11.0a1+ (heads/main-dirty:7103356455, Oct 11 2021, 17:28:12) [Clang 12.0.0 (clang-1200.0.32.29)] $ ./python.exe -m doctest demo.txt # no output + exits zero => it worksIf it makes sense to pursue this, I can create a proper bpo issue and put the finishing touches on this PR.
Context: I noticed this while reviewing the changes in Python 3.10 to see if they called for any corresponding changes to my bidict library, which I believe is one of several libraries that would benefit from fixing this issue.
/cc @rhettinger @sweeneyde et al.