bpo-37648: Fixed minor inconsistency in some __contains__. (GH-14904)

The collection's item is now always at the left and
the needle is on the right of ==.
This commit is contained in:
Serhiy Storchaka 2019-08-04 14:12:48 +03:00 committed by GitHub
parent 17e52649c0
commit 18b711c5a7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 86 additions and 22 deletions

View file

@ -3,7 +3,7 @@
import sys
import unittest
from test.support import run_unittest, TESTFN, unlink, cpython_only
from test.support import check_free_after_iterating
from test.support import check_free_after_iterating, ALWAYS_EQ, NEVER_EQ
import pickle
import collections.abc
@ -41,6 +41,14 @@ class IteratingSequenceClass:
def __iter__(self):
return BasicIterClass(self.n)
class IteratorProxyClass:
def __init__(self, i):
self.i = i
def __next__(self):
return next(self.i)
def __iter__(self):
return self
class SequenceClass:
def __init__(self, n):
self.n = n
@ -50,6 +58,12 @@ class SequenceClass:
else:
raise IndexError
class SequenceProxyClass:
def __init__(self, s):
self.s = s
def __getitem__(self, i):
return self.s[i]
class UnlimitedSequenceClass:
def __getitem__(self, i):
return i
@ -635,6 +649,13 @@ class TestCase(unittest.TestCase):
for i in "abc", -1, 5, 42.42, (3, 4), [], {1: 1}, 3-12j, sc5:
self.assertNotIn(i, sc5)
self.assertIn(ALWAYS_EQ, IteratorProxyClass(iter([1])))
self.assertIn(ALWAYS_EQ, SequenceProxyClass([1]))
self.assertNotIn(ALWAYS_EQ, IteratorProxyClass(iter([NEVER_EQ])))
self.assertNotIn(ALWAYS_EQ, SequenceProxyClass([NEVER_EQ]))
self.assertIn(NEVER_EQ, IteratorProxyClass(iter([ALWAYS_EQ])))
self.assertIn(NEVER_EQ, SequenceProxyClass([ALWAYS_EQ]))
self.assertRaises(TypeError, lambda: 3 in 12)
self.assertRaises(TypeError, lambda: 3 not in map)