Make MappingView inherit from Collection instead of Sized
After I generate an UML diagram from collections.abc, I found very strange that MappingView inherit from Sized instead of Collection (new in python 3.6). Yes, MappingView only define __len__ and not __iter__ and __contains__, but all of its subclasses define them (KeysView, ValuesView and ItemViews). I tried to run the tests in test/test_collections.py after making this change and on only one fail : Traceback (most recent call last): File "/usr/lib/python3.6/test/test_collections.py", line 789, in test_Collection self.assertNotIsInstance(x, Collection) AssertionError: dict_values([]) is an instance of <class 'collections.abc.Collection'> Wich is absolutely wrong, since in reality a dict_values instance has the behaviour of a Collection:
vals = {1:'a', 2: 'b'}.values() 'a' in vals True 'c' in vals False len(vals) 2 for val in vals: ... print(val) ... a b
The only lack is that it doesn't define a __contains__ method:
'__contains__' in vals False
It uses __iter__ to find the presence of the value. But, hey: we have register() for this cases! In fact, when MappingView inherit from Collection, dict_values is considered as a subclass of Collection since it's in the register of ValuesView, causing the above bug... So, the test have to be changed, and dict_values must be placed in the samples that pass the test, and not in the ones that fail it. Maybe we can open an issue on the python bug tracker?
This sounds like a good observation. I recommend opening a bug and preparing a PR if you can (a PR would also help finding if there are any problems with the idea). On Dec 29, 2017 9:50 AM, "Yahya Abou 'Imran via Python-ideas" < python-ideas@python.org> wrote:
After I generate an UML diagram from collections.abc, I found very strange that MappingView inherit from Sized instead of Collection (new in python 3.6).
Yes, MappingView only define __len__ and not __iter__ and __contains__, but all of its subclasses define them (KeysView, ValuesView and ItemViews).
I tried to run the tests in test/test_collections.py after making this change and on only one fail :
Traceback (most recent call last): File "/usr/lib/python3.6/test/test_collections.py", line 789, in test_Collection self.assertNotIsInstance(x, Collection) AssertionError: dict_values([]) is an instance of <class 'collections.abc.Collection'>
Wich is absolutely wrong, since in reality a dict_values instance has the behaviour of a Collection:
vals = {1:'a', 2: 'b'}.values() 'a' in vals True 'c' in vals False len(vals) 2 for val in vals: ... print(val) ... a b
The only lack is that it doesn't define a __contains__ method:
'__contains__' in vals False
It uses __iter__ to find the presence of the value.
But, hey: we have register() for this cases! In fact, when MappingView inherit from Collection, dict_values is considered as a subclass of Collection since it's in the register of ValuesView, causing the above bug... So, the test have to be changed, and dict_values must be placed in the samples that pass the test, and not in the ones that fail it.
Maybe we can open an issue on the python bug tracker?
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
participants (2)
-
Guido van Rossum
-
Yahya Abou 'Imran