1
1
import unittest
2
+ import inspect
2
3
import pickle
3
4
import sys
5
+ from decimal import Decimal
6
+ from fractions import Fraction
4
7
5
8
from test import support
6
9
from test .support import import_helper
@@ -508,6 +511,44 @@ def __getitem__(self, other): return 5 # so that C is a sequence
508
511
self .assertEqual (operator .ixor (c , 5 ), "ixor" )
509
512
self .assertEqual (operator .iconcat (c , c ), "iadd" )
510
513
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
+
511
552
def test_length_hint (self ):
512
553
operator = self .module
513
554
class X (object ):
@@ -533,6 +574,13 @@ def __length_hint__(self):
533
574
with self .assertRaises (LookupError ):
534
575
operator .length_hint (X (LookupError ))
535
576
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
+
536
584
def test_call (self ):
537
585
operator = self .module
538
586
@@ -555,13 +603,53 @@ def test_dunder_is_original(self):
555
603
if dunder :
556
604
self .assertIs (dunder , orig )
557
605
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
+
558
631
class PyOperatorTestCase (OperatorTestCase , unittest .TestCase ):
559
632
module = py_operator
560
633
561
634
@unittest .skipUnless (c_operator , 'requires _operator' )
562
635
class COperatorTestCase (OperatorTestCase , unittest .TestCase ):
563
636
module = c_operator
564
637
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
+
565
653
566
654
class OperatorPickleTestCase :
567
655
def copy (self , obj , proto ):
0 commit comments