mirror of
https://github.com/python/cpython.git
synced 2025-10-07 07:31:46 +00:00
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:
parent
17e52649c0
commit
18b711c5a7
12 changed files with 86 additions and 22 deletions
|
@ -6,6 +6,7 @@ import unittest
|
|||
import sys
|
||||
import pickle
|
||||
from test import support
|
||||
from test.support import ALWAYS_EQ, NEVER_EQ
|
||||
|
||||
# Various iterables
|
||||
# This is used for checking the constructor (here and in test_deque.py)
|
||||
|
@ -221,15 +222,15 @@ class CommonTest(unittest.TestCase):
|
|||
self.assertRaises(TypeError, u.__contains__)
|
||||
|
||||
def test_contains_fake(self):
|
||||
class AllEq:
|
||||
# Sequences must use rich comparison against each item
|
||||
# (unless "is" is true, or an earlier item answered)
|
||||
# So instances of AllEq must be found in all non-empty sequences.
|
||||
def __eq__(self, other):
|
||||
return True
|
||||
__hash__ = None # Can't meet hash invariant requirements
|
||||
self.assertNotIn(AllEq(), self.type2test([]))
|
||||
self.assertIn(AllEq(), self.type2test([1]))
|
||||
# Sequences must use rich comparison against each item
|
||||
# (unless "is" is true, or an earlier item answered)
|
||||
# So ALWAYS_EQ must be found in all non-empty sequences.
|
||||
self.assertNotIn(ALWAYS_EQ, self.type2test([]))
|
||||
self.assertIn(ALWAYS_EQ, self.type2test([1]))
|
||||
self.assertIn(1, self.type2test([ALWAYS_EQ]))
|
||||
self.assertNotIn(NEVER_EQ, self.type2test([]))
|
||||
self.assertNotIn(ALWAYS_EQ, self.type2test([NEVER_EQ]))
|
||||
self.assertIn(NEVER_EQ, self.type2test([ALWAYS_EQ]))
|
||||
|
||||
def test_contains_order(self):
|
||||
# Sequences must test in-order. If a rich comparison has side
|
||||
|
@ -350,6 +351,11 @@ class CommonTest(unittest.TestCase):
|
|||
self.assertEqual(a.count(1), 3)
|
||||
self.assertEqual(a.count(3), 0)
|
||||
|
||||
self.assertEqual(a.count(ALWAYS_EQ), 9)
|
||||
self.assertEqual(self.type2test([ALWAYS_EQ, ALWAYS_EQ]).count(1), 2)
|
||||
self.assertEqual(self.type2test([ALWAYS_EQ, ALWAYS_EQ]).count(NEVER_EQ), 2)
|
||||
self.assertEqual(self.type2test([NEVER_EQ, NEVER_EQ]).count(ALWAYS_EQ), 0)
|
||||
|
||||
self.assertRaises(TypeError, a.count)
|
||||
|
||||
class BadExc(Exception):
|
||||
|
@ -378,6 +384,11 @@ class CommonTest(unittest.TestCase):
|
|||
self.assertEqual(u.index(0, 3, 4), 3)
|
||||
self.assertRaises(ValueError, u.index, 2, 0, -10)
|
||||
|
||||
self.assertEqual(u.index(ALWAYS_EQ), 0)
|
||||
self.assertEqual(self.type2test([ALWAYS_EQ, ALWAYS_EQ]).index(1), 0)
|
||||
self.assertEqual(self.type2test([ALWAYS_EQ, ALWAYS_EQ]).index(NEVER_EQ), 0)
|
||||
self.assertRaises(ValueError, self.type2test([NEVER_EQ, NEVER_EQ]).index, ALWAYS_EQ)
|
||||
|
||||
self.assertRaises(TypeError, u.index)
|
||||
|
||||
class BadExc(Exception):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue