This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author yahya-abou-imran
Recipients yahya-abou-imran
Date 2017-12-31.15:36:23
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1514734584.09.0.467229070634.issue32467@psf.upfronthosting.co.za>
In-reply-to
Content
a `dict_values` instance behaves like a Collection (a Sized Iterable Container):

>>> values = {1: 'one', 2: 'two'}.values()
>>> 'two' in values
True
>>> 'three' in values
False
>>> for value in values: print(value)
one
two
>>> len(values)
2

But...

>>> isinstance(values, abc.Collection)
False
>>> isinstance(values, abc.Container)
False

The reason is the lack of __contains__():

>>> '__contains__' in dir(values)
False

The 'in' operator works above because it uses __iter__() to check the presence of the value.

I think it's inconsistent with the fact that dict_values is in the registry of ValuesView witch passes the test:

>>> issubclass(abc.ValuesView, abc.Collection)
True

A class passed in the registry of ValuesView should guarantee this behaviour (witch is the case here by the way, but informally).


I can think about several solutions for this:

1. add a __contains__() to the dict_values class;
2. if an ABC is considered a subclass of another ABC, all the classes in  the registry of the first (and recursively all of their subclasses) should be too;
3. pass dict_values in the registry of Container or Collection;
4. make ValuesView inherit from Container or Collection.


IMHO:

1 is out.
2 makes sense, but may be difficult to implement since it implies that a class has to know all the registries it's been passed in.
3 and 4 are by far the easiest ways to do it.


I think the inheritance from Collection is the best solution, because KeysView and ItemsView are already Collections by inheriting from Set witch inherits from Collection. The only fondamental difference between the three views (in terms of protocol and API), it's that ValuesView is not a Set.
History
Date User Action Args
2017-12-31 15:36:24yahya-abou-imransetrecipients: + yahya-abou-imran
2017-12-31 15:36:24yahya-abou-imransetmessageid: <1514734584.09.0.467229070634.issue32467@psf.upfronthosting.co.za>
2017-12-31 15:36:24yahya-abou-imranlinkissue32467 messages
2017-12-31 15:36:23yahya-abou-imrancreate