https://github.com/python/cpython/commit/54514467c26c657eb1a6f8885786297979689d2d
commit: 54514467c26c657eb1a6f8885786297979689d2d
branch: 3.12
author: Miss Islington (bot) <[email protected]>
committer: hugovk <[email protected]>
date: 2024-03-26T09:37:41Z
summary:

[3.12] gh-83845: Add tests for operator module (GH-115883) (#117253)

Co-authored-by: Hugo van Kemenade <[email protected]>
Co-authored-by: Karthikeyan Singaravelan <[email protected]>

files:
M Lib/test/test_operator.py

diff --git a/Lib/test/test_operator.py b/Lib/test/test_operator.py
index 1db738d228b1b9..0d34d671563d19 100644
--- a/Lib/test/test_operator.py
+++ b/Lib/test/test_operator.py
@@ -1,6 +1,8 @@
 import unittest
 import pickle
 import sys
+from decimal import Decimal
+from fractions import Fraction
 
 from test import support
 from test.support import import_helper
@@ -508,6 +510,44 @@ def __getitem__(self, other): return 5  # so that C is a 
sequence
         self.assertEqual(operator.ixor     (c, 5), "ixor")
         self.assertEqual(operator.iconcat  (c, c), "iadd")
 
+    def test_iconcat_without_getitem(self):
+        operator = self.module
+
+        msg = "'int' object can't be concatenated"
+        with self.assertRaisesRegex(TypeError, msg):
+            operator.iconcat(1, 0.5)
+
+    def test_index(self):
+        operator = self.module
+        class X:
+            def __index__(self):
+                return 1
+
+        self.assertEqual(operator.index(X()), 1)
+        self.assertEqual(operator.index(0), 0)
+        self.assertEqual(operator.index(1), 1)
+        self.assertEqual(operator.index(2), 2)
+        with self.assertRaises((AttributeError, TypeError)):
+            operator.index(1.5)
+        with self.assertRaises((AttributeError, TypeError)):
+            operator.index(Fraction(3, 7))
+        with self.assertRaises((AttributeError, TypeError)):
+            operator.index(Decimal(1))
+        with self.assertRaises((AttributeError, TypeError)):
+            operator.index(None)
+
+    def test_not_(self):
+        operator = self.module
+        class C:
+            def __bool__(self):
+                raise SyntaxError
+        self.assertRaises(TypeError, operator.not_)
+        self.assertRaises(SyntaxError, operator.not_, C())
+        self.assertFalse(operator.not_(5))
+        self.assertFalse(operator.not_([0]))
+        self.assertTrue(operator.not_(0))
+        self.assertTrue(operator.not_([]))
+
     def test_length_hint(self):
         operator = self.module
         class X(object):
@@ -533,6 +573,13 @@ def __length_hint__(self):
         with self.assertRaises(LookupError):
             operator.length_hint(X(LookupError))
 
+        class Y: pass
+
+        msg = "'str' object cannot be interpreted as an integer"
+        with self.assertRaisesRegex(TypeError, msg):
+            operator.length_hint(X(2), "abc")
+        self.assertEqual(operator.length_hint(Y(), 10), 10)
+
     def test_call(self):
         operator = self.module
 

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to