Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(88)

## Side by Side Diff: Lib/test/test_collections.py

Issue 25958: Implicit ABCs have no means of "anti-registration"
Patch Set: Created 3 years, 6 months ago
 Left: Base Patch Set 1: None Patch Set 2: None Patch Set 3: None Patch Set 4: None Patch Set 5: None Patch Set 6: None Patch Set 7: None Right: Patch Set 1: None Patch Set 2: None Patch Set 3: None Patch Set 4: None Patch Set 5: None Patch Set 6: None Patch Set 7: None
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
« no previous file with comments | « Lib/test/test_bytes.py ('k') | Lib/test/test_contains.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Hide Comments ('s')
OLDNEW
1 """Unit tests for collections.py.""" 1 """Unit tests for collections.py."""
2 2
3 import collections 3 import collections
4 import copy 4 import copy
5 import doctest 5 import doctest
6 import inspect 6 import inspect
7 import keyword 7 import keyword
8 import operator 8 import operator
9 import pickle 9 import pickle
10 from random import choice, randrange 10 from random import choice, randrange
(...skipping 468 matching lines...)
479 __rxor__ = __eq__ 479 __rxor__ = __eq__
480 __rsub__ = __eq__ 480 __rsub__ = __eq__
481 481
482 for name, op in operators.items(): 482 for name, op in operators.items():
483 if not hasattr(instance, name): 483 if not hasattr(instance, name):
484 continue 484 continue
485 other = Other() 485 other = Other()
486 op(instance, other) 486 op(instance, other)
487 self.assertTrue(other.right_side,'Right side not called for %s.%s' 487 self.assertTrue(other.right_side,'Right side not called for %s.%s'
488 % (type(instance), name)) 488 % (type(instance), name))
489
490 def _test_gen():
491 yield
489 492
490 class TestOneTrickPonyABCs(ABCTestCase): 493 class TestOneTrickPonyABCs(ABCTestCase):
491 494
492 def test_Awaitable(self): 495 def test_Awaitable(self):
493 def gen(): 496 def gen():
494 yield 497 yield
495 498
496 @types.coroutine 499 @types.coroutine
497 def coro(): 500 def coro():
498 yield 501 yield
(...skipping 168 matching lines...)
667 def test_Iterable(self): 670 def test_Iterable(self):
668 # Check some non-iterables 671 # Check some non-iterables
669 non_samples = [None, 42, 3.14, 1j] 672 non_samples = [None, 42, 3.14, 1j]
670 for x in non_samples: 673 for x in non_samples:
671 self.assertNotIsInstance(x, Iterable) 674 self.assertNotIsInstance(x, Iterable)
672 self.assertFalse(issubclass(type(x), Iterable), repr(type(x))) 675 self.assertFalse(issubclass(type(x), Iterable), repr(type(x)))
673 # Check some iterables 676 # Check some iterables
674 samples = [bytes(), str(), 677 samples = [bytes(), str(),
675 tuple(), list(), set(), frozenset(), dict(), 678 tuple(), list(), set(), frozenset(), dict(),
676 dict().keys(), dict().items(), dict().values(), 679 dict().keys(), dict().items(), dict().values(),
677 (lambda: (yield))(), 680 _test_gen(),
678 (x for x in []), 681 (x for x in []),
679 ] 682 ]
680 for x in samples: 683 for x in samples:
681 self.assertIsInstance(x, Iterable) 684 self.assertIsInstance(x, Iterable)
682 self.assertTrue(issubclass(type(x), Iterable), repr(type(x))) 685 self.assertTrue(issubclass(type(x), Iterable), repr(type(x)))
683 # Check direct subclassing 686 # Check direct subclassing
684 class I(Iterable): 687 class I(Iterable):
685 def __iter__(self): 688 def __iter__(self):
686 return super().__iter__() 689 return super().__iter__()
687 self.assertEqual(list(I()), []) 690 self.assertEqual(list(I()), [])
688 self.assertFalse(issubclass(str, I)) 691 self.assertFalse(issubclass(str, I))
689 self.validate_abstract_methods(Iterable, '__iter__') 692 self.validate_abstract_methods(Iterable, '__iter__')
690 self.validate_isinstance(Iterable, '__iter__') 693 self.validate_isinstance(Iterable, '__iter__')
694 # Check None blocking
695 class It:
696 def __iter__(self): return iter([])
697 class ItBlocked(It):
698 __iter__ = None
699 self.assertTrue(issubclass(It, Iterable))
700 self.assertTrue(isinstance(It(), Iterable))
701 self.assertFalse(issubclass(ItBlocked, Iterable))
702 self.assertFalse(isinstance(ItBlocked(), Iterable))
691 703
692 def test_Reversible(self): 704 def test_Reversible(self):
693 # Check some non-reversibles 705 # Check some non-reversibles
694 non_samples = [None, 42, 3.14, 1j, dict(), set(), frozenset()] 706 non_samples = [None, 42, 3.14, 1j, dict(), set(), frozenset()]
695 for x in non_samples: 707 for x in non_samples:
696 self.assertNotIsInstance(x, Reversible) 708 self.assertNotIsInstance(x, Reversible)
697 self.assertFalse(issubclass(type(x), Reversible), repr(type(x))) 709 self.assertFalse(issubclass(type(x), Reversible), repr(type(x)))
698 # Check some reversibles 710 # Check some non-reversible iterables
699 samples = [tuple(), list()] 711 non_reversibles = [dict().keys(), dict().items(), dict().values(),
712 Counter(), Counter().keys(), Counter().items(),
713 Counter().values(), _test_gen(),
714 (x for x in []), iter([]), reversed([])]
715 for x in non_reversibles:
716 self.assertNotIsInstance(x, Reversible)
717 self.assertFalse(issubclass(type(x), Reversible), repr(type(x)))
718 # Check some reversible iterables
719 samples = [bytes(), str(), tuple(), list(), OrderedDict(),
720 OrderedDict().keys(), OrderedDict().items(),
721 OrderedDict().values()]
700 for x in samples: 722 for x in samples:
701 self.assertIsInstance(x, Reversible) 723 self.assertIsInstance(x, Reversible)
702 self.assertTrue(issubclass(type(x), Reversible), repr(type(x))) 724 self.assertTrue(issubclass(type(x), Reversible), repr(type(x)))
703 # Check also Mapping, MutableMapping, and Sequence 725 # Check also Mapping, MutableMapping, and Sequence
704 self.assertTrue(issubclass(Sequence, Reversible), repr(Sequence)) 726 self.assertTrue(issubclass(Sequence, Reversible), repr(Sequence))
705 self.assertFalse(issubclass(Mapping, Reversible), repr(Mapping)) 727 self.assertFalse(issubclass(Mapping, Reversible), repr(Mapping))
706 self.assertFalse(issubclass(MutableMapping, Reversible), repr(MutableMap ping)) 728 self.assertFalse(issubclass(MutableMapping, Reversible), repr(MutableMap ping))
707 # Check direct subclassing 729 # Check direct subclassing
708 class R(Reversible): 730 class R(Reversible):
709 def __iter__(self): 731 def __iter__(self):
710 return iter(list()) 732 return iter(list())
711 def __reversed__(self): 733 def __reversed__(self):
712 return iter(list()) 734 return iter(list())
713 self.assertEqual(list(reversed(R())), []) 735 self.assertEqual(list(reversed(R())), [])
714 self.assertFalse(issubclass(float, R)) 736 self.assertFalse(issubclass(float, R))
715 self.validate_abstract_methods(Reversible, '__reversed__', '__iter__') 737 self.validate_abstract_methods(Reversible, '__reversed__', '__iter__')
738 # Check reversible non-iterable (which is not Reversible)
739 class RevNoIter:
740 def __reversed__(self): return reversed([])
741 class RevPlusIter(RevNoIter):
742 def __iter__(self): return iter([])
743 self.assertFalse(issubclass(RevNoIter, Reversible))
744 self.assertFalse(isinstance(RevNoIter(), Reversible))
745 self.assertTrue(issubclass(RevPlusIter, Reversible))
746 self.assertTrue(isinstance(RevPlusIter(), Reversible))
747 # Check None blocking
748 class Rev:
749 def __iter__(self): return iter([])
750 def __reversed__(self): return reversed([])
751 class RevItBlocked(Rev):
752 __iter__ = None
753 class RevRevBlocked(Rev):
754 __reversed__ = None
755 self.assertTrue(issubclass(Rev, Reversible))
756 self.assertTrue(isinstance(Rev(), Reversible))
757 self.assertFalse(issubclass(RevItBlocked, Reversible))
758 self.assertFalse(isinstance(RevItBlocked(), Reversible))
759 self.assertFalse(issubclass(RevRevBlocked, Reversible))
760 self.assertFalse(isinstance(RevRevBlocked(), Reversible))
716 761
717 def test_Iterator(self): 762 def test_Iterator(self):
718 non_samples = [None, 42, 3.14, 1j, b"", "", (), [], {}, set()] 763 non_samples = [None, 42, 3.14, 1j, b"", "", (), [], {}, set()]
719 for x in non_samples: 764 for x in non_samples:
720 self.assertNotIsInstance(x, Iterator) 765 self.assertNotIsInstance(x, Iterator)
721 self.assertFalse(issubclass(type(x), Iterator), repr(type(x))) 766 self.assertFalse(issubclass(type(x), Iterator), repr(type(x)))
722 samples = [iter(bytes()), iter(str()), 767 samples = [iter(bytes()), iter(str()),
723 iter(tuple()), iter(list()), iter(dict()), 768 iter(tuple()), iter(list()), iter(dict()),
724 iter(set()), iter(frozenset()), 769 iter(set()), iter(frozenset()),
725 iter(dict().keys()), iter(dict().items()), 770 iter(dict().keys()), iter(dict().items()),
726 iter(dict().values()), 771 iter(dict().values()),
727 (lambda: (yield))(), 772 _test_gen(),
728 (x for x in []), 773 (x for x in []),
729 ] 774 ]
730 for x in samples: 775 for x in samples:
731 self.assertIsInstance(x, Iterator) 776 self.assertIsInstance(x, Iterator)
732 self.assertTrue(issubclass(type(x), Iterator), repr(type(x))) 777 self.assertTrue(issubclass(type(x), Iterator), repr(type(x)))
733 self.validate_abstract_methods(Iterator, '__next__', '__iter__') 778 self.validate_abstract_methods(Iterator, '__next__', '__iter__')
734 779
735 # Issue 10565 780 # Issue 10565
736 class NextOnly: 781 class NextOnly:
737 def __next__(self): 782 def __next__(self):
(...skipping 67 matching lines...)
805 self.assertRaises(ValueError, FailOnClose().close) 850 self.assertRaises(ValueError, FailOnClose().close)
806 851
807 class IgnoreGeneratorExit(Generator): 852 class IgnoreGeneratorExit(Generator):
808 def send(self, value): return value 853 def send(self, value): return value
809 def throw(self, *args): pass 854 def throw(self, *args): pass
810 855
811 self.assertRaises(RuntimeError, IgnoreGeneratorExit().close) 856 self.assertRaises(RuntimeError, IgnoreGeneratorExit().close)
812 857
813 def test_Sized(self): 858 def test_Sized(self):
814 non_samples = [None, 42, 3.14, 1j, 859 non_samples = [None, 42, 3.14, 1j,
815 (lambda: (yield))(), 860 _test_gen(),
816 (x for x in []), 861 (x for x in []),
817 ] 862 ]
818 for x in non_samples: 863 for x in non_samples:
819 self.assertNotIsInstance(x, Sized) 864 self.assertNotIsInstance(x, Sized)
820 self.assertFalse(issubclass(type(x), Sized), repr(type(x))) 865 self.assertFalse(issubclass(type(x), Sized), repr(type(x)))
821 samples = [bytes(), str(), 866 samples = [bytes(), str(),
822 tuple(), list(), set(), frozenset(), dict(), 867 tuple(), list(), set(), frozenset(), dict(),
823 dict().keys(), dict().items(), dict().values(), 868 dict().keys(), dict().items(), dict().values(),
824 ] 869 ]
825 for x in samples: 870 for x in samples:
826 self.assertIsInstance(x, Sized) 871 self.assertIsInstance(x, Sized)
827 self.assertTrue(issubclass(type(x), Sized), repr(type(x))) 872 self.assertTrue(issubclass(type(x), Sized), repr(type(x)))
828 self.validate_abstract_methods(Sized, '__len__') 873 self.validate_abstract_methods(Sized, '__len__')
829 self.validate_isinstance(Sized, '__len__') 874 self.validate_isinstance(Sized, '__len__')
830 875
831 def test_Container(self): 876 def test_Container(self):
832 non_samples = [None, 42, 3.14, 1j, 877 non_samples = [None, 42, 3.14, 1j,
833 (lambda: (yield))(), 878 _test_gen(),
834 (x for x in []), 879 (x for x in []),
835 ] 880 ]
836 for x in non_samples: 881 for x in non_samples:
837 self.assertNotIsInstance(x, Container) 882 self.assertNotIsInstance(x, Container)
838 self.assertFalse(issubclass(type(x), Container), repr(type(x))) 883 self.assertFalse(issubclass(type(x), Container), repr(type(x)))
839 samples = [bytes(), str(), 884 samples = [bytes(), str(),
840 tuple(), list(), set(), frozenset(), dict(), 885 tuple(), list(), set(), frozenset(), dict(),
841 dict().keys(), dict().items(), 886 dict().keys(), dict().items(),
842 ] 887 ]
843 for x in samples: 888 for x in samples:
844 self.assertIsInstance(x, Container) 889 self.assertIsInstance(x, Container)
845 self.assertTrue(issubclass(type(x), Container), repr(type(x))) 890 self.assertTrue(issubclass(type(x), Container), repr(type(x)))
846 self.validate_abstract_methods(Container, '__contains__') 891 self.validate_abstract_methods(Container, '__contains__')
847 self.validate_isinstance(Container, '__contains__') 892 self.validate_isinstance(Container, '__contains__')
848 893
849 def test_Callable(self): 894 def test_Callable(self):
850 non_samples = [None, 42, 3.14, 1j, 895 non_samples = [None, 42, 3.14, 1j,
851 "", b"", (), [], {}, set(), 896 "", b"", (), [], {}, set(),
852 (lambda: (yield))(), 897 _test_gen(),
853 (x for x in []), 898 (x for x in []),
854 ] 899 ]
855 for x in non_samples: 900 for x in non_samples:
856 self.assertNotIsInstance(x, Callable) 901 self.assertNotIsInstance(x, Callable)
857 self.assertFalse(issubclass(type(x), Callable), repr(type(x))) 902 self.assertFalse(issubclass(type(x), Callable), repr(type(x)))
858 samples = [lambda: None, 903 samples = [lambda: None,
859 type, int, object, 904 type, int, object,
860 len, 905 len,
861 list.append, [].append, 906 list.append, [].append,
862 ] 907 ]
(...skipping 394 matching lines...)
1257 self.validate_abstract_methods(Mapping, '__contains__', '__iter__', '__l en__', 1302 self.validate_abstract_methods(Mapping, '__contains__', '__iter__', '__l en__',
1258 '__getitem__') 1303 '__getitem__')
1259 class MyMapping(Mapping): 1304 class MyMapping(Mapping):
1260 def __len__(self): 1305 def __len__(self):
1261 return 0 1306 return 0
1262 def __getitem__(self, i): 1307 def __getitem__(self, i):
1263 raise IndexError 1308 raise IndexError
1264 def __iter__(self): 1309 def __iter__(self):
1265 return iter(()) 1310 return iter(())
1266 self.validate_comparison(MyMapping()) 1311 self.validate_comparison(MyMapping())
1312 self.assertRaises(TypeError, reversed, MyMapping())
1267 1313
1268 def test_MutableMapping(self): 1314 def test_MutableMapping(self):
1269 for sample in [dict]: 1315 for sample in [dict]:
1270 self.assertIsInstance(sample(), MutableMapping) 1316 self.assertIsInstance(sample(), MutableMapping)
1271 self.assertTrue(issubclass(sample, MutableMapping)) 1317 self.assertTrue(issubclass(sample, MutableMapping))
1272 self.validate_abstract_methods(MutableMapping, '__contains__', '__iter__ ', '__len__', 1318 self.validate_abstract_methods(MutableMapping, '__contains__', '__iter__ ', '__len__',
1273 '__getitem__', '__setitem__', '__delitem__') 1319 '__getitem__', '__setitem__', '__delitem__')
1274 1320
1275 def test_MutableMapping_subclass(self): 1321 def test_MutableMapping_subclass(self):
1276 # Test issue 9214 1322 # Test issue 9214
(...skipping 399 matching lines...)
1676 test_classes = [TestNamedTuple, NamedTupleDocs, TestOneTrickPonyABCs, 1722 test_classes = [TestNamedTuple, NamedTupleDocs, TestOneTrickPonyABCs,
1677 TestCollectionABCs, TestCounter, TestChainMap, 1723 TestCollectionABCs, TestCounter, TestChainMap,
1678 TestUserObjects, 1724 TestUserObjects,
1679 ] 1725 ]
1680 support.run_unittest(*test_classes) 1726 support.run_unittest(*test_classes)
1681 support.run_doctest(collections, verbose) 1727 support.run_doctest(collections, verbose)
1682 1728
1683 1729
1684 if __name__ == "__main__": 1730 if __name__ == "__main__":
1685 test_main(verbose=True) 1731 test_main(verbose=True)
OLDNEW
« no previous file with comments | « Lib/test/test_bytes.py ('k') | Lib/test/test_contains.py » ('j') | no next file with comments »

This is Rietveld 894c83f36cb7+