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

Delta Between Two Patch Sets: Lib/test/test_contains.py

Issue 25958: Implicit ABCs have no means of "anti-registration"
Left Patch Set: Created 4 years, 1 month ago
Right Patch Set: Created 3 years, 6 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/test/test_collections.py ('k') | Lib/test/test_enumerate.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 86
87 def test_block_fallback(self): 87 def test_block_fallback(self):
88 # blocking fallback with __contains__ = None 88 # blocking fallback with __contains__ = None
89 class ByContains(object): 89 class ByContains(object):
90 def __contains__(self, other): 90 def __contains__(self, other):
91 return False 91 return False
92 c = ByContains() 92 c = ByContains()
93 class BlockContains(ByContains): 93 class BlockContains(ByContains):
94 """Is not a container 94 """Is not a container
95 95
96 This class is a perfectly good iterable, and inherits from 96 This class is a perfectly good iterable (as tested by
97 a perfectly good container, but __contains__ = None blocks 97 list(bc)), as well as inheriting from a perfectly good
98 both of these from working.""" 98 container, but __contains__ = None prevents the usual
99 fallback to iteration in the container protocol. That
100 is, normally, 0 in bc would fall back to the equivalent
101 of any(x==0 for x in bc), but here it's blocked from
102 doing so.
103 """
99 def __iter__(self): 104 def __iter__(self):
100 while False: 105 while False:
101 yield None 106 yield None
102 __contains__ = None 107 __contains__ = None
103 bc = BlockContains() 108 bc = BlockContains()
104 self.assertFalse(0 in c) 109 self.assertFalse(0 in c)
105 self.assertFalse(0 in list(bc)) 110 self.assertFalse(0 in list(bc))
106 self.assertRaises(TypeError, lambda: 0 in bc) 111 self.assertRaises(TypeError, lambda: 0 in bc)
107 112
108
109 if __name__ == '__main__': 113 if __name__ == '__main__':
110 unittest.main() 114 unittest.main()
LEFTRIGHT

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