You're right that there's some inconsistency here. But I don't think it's worth fixing given that the fix would introduce another inconsistency (which you pointed out) and would also risk breaking backwards compatibility. I think this ship has sailed.

On Sat, Dec 30, 2017 at 5:18 PM, Yahya Abou 'Imran via Python-ideas <python-ideas@python.org> wrote:
=== This proposition is purely aesthetic ===

At this time, the __repr__ of the mapping views is showing the whole mapping:

>>> from collections.abc import ValuesView, KeysView, ItemsView
>>> d = {3: 'three', 4: 'four'}
>>> KeysView(d)
KeysView({3: 'three', 4: 'four'})
>>> ValuesView(d)
ValuesView({3: 'three', 4: 'four'})
>>> ItemsView(d)
ItemsView({3: 'three', 4: 'four'})

Witch is not consistent with dict_keys, dict_values, dict_items:

>>> d.keys()
dict_keys([3, 4])
>>> d.values()
dict_values(['three', 'four'])
>>> d.items()
dict_items([(3, 'three'), (4, 'four')])

We could easily change that, since all the views are iterables on what they are designed for, in MappingView:

def __repr__(self):
    viewname = self.__class__.__name__
    elements = ', '.join(map(repr, self))
    return f'{viewname}([elements])

And now:

>>> KeysView(d)
KeysView([3, 4])
>>> ValuesView(d)
ValuesView(['three', 'four'])
>>> ItemsView(d)
ItemsView([(3, 'three'), (4, 'four')])

It's not breaking any test (it seems that there isn't any for this), but it have a real drawback: it's breaking the convention about instantiation by copy/pasting:

>>> ValuesView(['three', 'four'])
Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/IPython/core/formatters.py", line 684, in __call__
    return repr(obj)
  File "/usr/lib/python3.6/_collections_abc.py", line 706, in __repr__
    elements = ', '.join(map(repr, self))
  File "/usr/lib/python3.6/_collections_abc.py", line 764, in __iter__
    yield self._mapping[key]
TypeError: list indices must be integers or slices, not str


It's because __init__ in MappingView treat the passed argument -- wich is stored in self._mapping -- as the whole mapping, not just keys, values or items... And all the other methods (__contains__ and __iter__) in the subclasses are using this _mapping attribute to work.


So what is to prioritize?
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/



--
--Guido van Rossum (python.org/~guido)