-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
dict_values isn't considered a Collection nor a Container #76648
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
Comments
a >>> 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:
IMHO: 1 is out. 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. |
It would be okay to let ValuesView inherit from Collection. Am marking this a 3.7 only because it isn't important to anyone's actual code and there is no known use case. Technically, it isn't even a bug. The __contains__ method is supported implicitly (as it is for many classes that just define __iter__), so there is no requirement that this be recognized as a Collection. Also, the existing behavior was explicitly tested (see line 848 in Lib/test/test_collections). I'm going forward with this because it does offer a sense of neatness (in comparison to KeysView and ItemsView) and it might avoid a pedantic StackOverflow question somewhere down the line. |
Perfect for me. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: