Skip to content

Commit 1112e01

Browse files
miss-islingtonhugovktirkarthi
authored
[3.11] gh-83845: Add tests for operator module (GH-115883) (#117254)
Co-authored-by: Hugo van Kemenade <[email protected]> Co-authored-by: Karthikeyan Singaravelan <[email protected]>
1 parent 04b26ee commit 1112e01

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

Lib/test/test_operator.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import unittest
22
import pickle
33
import sys
4+
from decimal import Decimal
5+
from fractions import Fraction
46

57
from test import support
68
from test.support import import_helper
@@ -508,6 +510,44 @@ def __getitem__(self, other): return 5 # so that C is a sequence
508510
self.assertEqual(operator.ixor (c, 5), "ixor")
509511
self.assertEqual(operator.iconcat (c, c), "iadd")
510512

513+
def test_iconcat_without_getitem(self):
514+
operator = self.module
515+
516+
msg = "'int' object can't be concatenated"
517+
with self.assertRaisesRegex(TypeError, msg):
518+
operator.iconcat(1, 0.5)
519+
520+
def test_index(self):
521+
operator = self.module
522+
class X:
523+
def __index__(self):
524+
return 1
525+
526+
self.assertEqual(operator.index(X()), 1)
527+
self.assertEqual(operator.index(0), 0)
528+
self.assertEqual(operator.index(1), 1)
529+
self.assertEqual(operator.index(2), 2)
530+
with self.assertRaises((AttributeError, TypeError)):
531+
operator.index(1.5)
532+
with self.assertRaises((AttributeError, TypeError)):
533+
operator.index(Fraction(3, 7))
534+
with self.assertRaises((AttributeError, TypeError)):
535+
operator.index(Decimal(1))
536+
with self.assertRaises((AttributeError, TypeError)):
537+
operator.index(None)
538+
539+
def test_not_(self):
540+
operator = self.module
541+
class C:
542+
def __bool__(self):
543+
raise SyntaxError
544+
self.assertRaises(TypeError, operator.not_)
545+
self.assertRaises(SyntaxError, operator.not_, C())
546+
self.assertFalse(operator.not_(5))
547+
self.assertFalse(operator.not_([0]))
548+
self.assertTrue(operator.not_(0))
549+
self.assertTrue(operator.not_([]))
550+
511551
def test_length_hint(self):
512552
operator = self.module
513553
class X(object):
@@ -533,6 +573,13 @@ def __length_hint__(self):
533573
with self.assertRaises(LookupError):
534574
operator.length_hint(X(LookupError))
535575

576+
class Y: pass
577+
578+
msg = "'str' object cannot be interpreted as an integer"
579+
with self.assertRaisesRegex(TypeError, msg):
580+
operator.length_hint(X(2), "abc")
581+
self.assertEqual(operator.length_hint(Y(), 10), 10)
582+
536583
def test_call(self):
537584
operator = self.module
538585

0 commit comments

Comments
 (0)