11import unittest
2+ import inspect
23import pickle
34import sys
5+ from decimal import Decimal
6+ from fractions import Fraction
47
58from test import support
69from test .support import import_helper
@@ -508,6 +511,44 @@ def __getitem__(self, other): return 5 # so that C is a sequence
508511 self .assertEqual (operator .ixor (c , 5 ), "ixor" )
509512 self .assertEqual (operator .iconcat (c , c ), "iadd" )
510513
514+ def test_iconcat_without_getitem (self ):
515+ operator = self .module
516+
517+ msg = "'int' object can't be concatenated"
518+ with self .assertRaisesRegex (TypeError , msg ):
519+ operator .iconcat (1 , 0.5 )
520+
521+ def test_index (self ):
522+ operator = self .module
523+ class X :
524+ def __index__ (self ):
525+ return 1
526+
527+ self .assertEqual (operator .index (X ()), 1 )
528+ self .assertEqual (operator .index (0 ), 0 )
529+ self .assertEqual (operator .index (1 ), 1 )
530+ self .assertEqual (operator .index (2 ), 2 )
531+ with self .assertRaises ((AttributeError , TypeError )):
532+ operator .index (1.5 )
533+ with self .assertRaises ((AttributeError , TypeError )):
534+ operator .index (Fraction (3 , 7 ))
535+ with self .assertRaises ((AttributeError , TypeError )):
536+ operator .index (Decimal (1 ))
537+ with self .assertRaises ((AttributeError , TypeError )):
538+ operator .index (None )
539+
540+ def test_not_ (self ):
541+ operator = self .module
542+ class C :
543+ def __bool__ (self ):
544+ raise SyntaxError
545+ self .assertRaises (TypeError , operator .not_ )
546+ self .assertRaises (SyntaxError , operator .not_ , C ())
547+ self .assertFalse (operator .not_ (5 ))
548+ self .assertFalse (operator .not_ ([0 ]))
549+ self .assertTrue (operator .not_ (0 ))
550+ self .assertTrue (operator .not_ ([]))
551+
511552 def test_length_hint (self ):
512553 operator = self .module
513554 class X (object ):
@@ -533,6 +574,13 @@ def __length_hint__(self):
533574 with self .assertRaises (LookupError ):
534575 operator .length_hint (X (LookupError ))
535576
577+ class Y : pass
578+
579+ msg = "'str' object cannot be interpreted as an integer"
580+ with self .assertRaisesRegex (TypeError , msg ):
581+ operator .length_hint (X (2 ), "abc" )
582+ self .assertEqual (operator .length_hint (Y (), 10 ), 10 )
583+
536584 def test_call (self ):
537585 operator = self .module
538586
@@ -555,13 +603,53 @@ def test_dunder_is_original(self):
555603 if dunder :
556604 self .assertIs (dunder , orig )
557605
606+ @support .requires_docstrings
607+ def test_attrgetter_signature (self ):
608+ operator = self .module
609+ sig = inspect .signature (operator .attrgetter )
610+ self .assertEqual (str (sig ), '(attr, /, *attrs)' )
611+ sig = inspect .signature (operator .attrgetter ('x' , 'z' , 'y' ))
612+ self .assertEqual (str (sig ), '(obj, /)' )
613+
614+ @support .requires_docstrings
615+ def test_itemgetter_signature (self ):
616+ operator = self .module
617+ sig = inspect .signature (operator .itemgetter )
618+ self .assertEqual (str (sig ), '(item, /, *items)' )
619+ sig = inspect .signature (operator .itemgetter (2 , 3 , 5 ))
620+ self .assertEqual (str (sig ), '(obj, /)' )
621+
622+ @support .requires_docstrings
623+ def test_methodcaller_signature (self ):
624+ operator = self .module
625+ sig = inspect .signature (operator .methodcaller )
626+ self .assertEqual (str (sig ), '(name, /, *args, **kwargs)' )
627+ sig = inspect .signature (operator .methodcaller ('foo' , 2 , y = 3 ))
628+ self .assertEqual (str (sig ), '(obj, /)' )
629+
630+
558631class PyOperatorTestCase (OperatorTestCase , unittest .TestCase ):
559632 module = py_operator
560633
561634@unittest .skipUnless (c_operator , 'requires _operator' )
562635class COperatorTestCase (OperatorTestCase , unittest .TestCase ):
563636 module = c_operator
564637
638+ # TODO: RUSTPYTHON
639+ @unittest .expectedFailure
640+ def test_attrgetter_signature (self ):
641+ super ().test_attrgetter_signature ()
642+
643+ # TODO: RUSTPYTHON
644+ @unittest .expectedFailure
645+ def test_itemgetter_signature (self ):
646+ super ().test_itemgetter_signature ()
647+
648+ # TODO: RUSTPYTHON
649+ @unittest .expectedFailure
650+ def test_methodcaller_signature (self ):
651+ super ().test_methodcaller_signature ()
652+
565653
566654class OperatorPickleTestCase :
567655 def copy (self , obj , proto ):
0 commit comments