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

Side by Side Diff: Lib/test/test_contains.py

Issue 25958: Implicit ABCs have no means of "anti-registration"
Patch Set: Created 4 years, 4 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 from collections import deque 1 from collections import deque
2 import unittest 2 import unittest
3 3
4 4
5 class base_set: 5 class base_set:
6 def __init__(self, el): 6 def __init__(self, el):
7 self.el = el 7 self.el = el
8 8
9 class myset(base_set): 9 class myset(base_set):
10 def __contains__(self, el): 10 def __contains__(self, el):
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
76 return 28 76 return 28
77 77
78 values = float('nan'), 1, None, 'abc', MyNonReflexive() 78 values = float('nan'), 1, None, 'abc', MyNonReflexive()
79 constructors = list, tuple, dict.fromkeys, set, frozenset, deque 79 constructors = list, tuple, dict.fromkeys, set, frozenset, deque
80 for constructor in constructors: 80 for constructor in constructors:
81 container = constructor(values) 81 container = constructor(values)
82 for elem in container: 82 for elem in container:
83 self.assertIn(elem, container) 83 self.assertIn(elem, container)
84 self.assertTrue(container == constructor(values)) 84 self.assertTrue(container == constructor(values))
85 self.assertTrue(container == container) 85 self.assertTrue(container == container)
86
87 def test_block_fallback(self):
88 # blocking fallback with __contains__ = None
89 class ByContains(object):
90 def __contains__(self, other):
91 return False
92 c = ByContains()
93 class BlockContains(ByContains):
94 """Is not a container
95
96 This class is a perfectly good iterable, and inherits from
97 a perfectly good container, but __contains__ = None blocks
98 both of these from working."""
Martin Panter 2016/01/09 02:34:57 It only blocks the container aspect from working;
abarnert 2016/01/18 20:28:42 I think my wording is a bit confusing here. Iterat
99 def __iter__(self):
100 while False:
101 yield None
102 __contains__ = None
103 bc = BlockContains()
104 self.assertFalse(0 in c)
105 self.assertFalse(0 in list(bc))
106 self.assertRaises(TypeError, lambda: 0 in bc)
86 107
87 108
88 if __name__ == '__main__': 109 if __name__ == '__main__':
89 unittest.main() 110 unittest.main()
OLDNEW

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+