@@ -640,23 +640,76 @@ c: Literal[15]
640
640
-- to rewrite them in the future.
641
641
--
642
642
643
- [case testLiteralInheritedMethodsInteractCorrectly-skip]
644
- # TODO: fix this test. The method calls are not using the fallbacks.
643
+ [case testLiteralActualAssignment-skip]
644
+ # TODO: fix this test. The 1 is currently always given a type of 'int'
645
+ from typing_extensions import Literal
646
+
647
+ a: Literal[1] = 1
648
+ [out]
649
+
650
+ --
651
+ -- Tests that make sure we're correctly using the fallback
652
+ --
653
+
654
+ [case testLiteralFallbackOperatorsWorkCorrectly]
645
655
from typing_extensions import Literal
646
656
647
657
a: Literal[3]
648
658
b: int
649
- c: Literal['foo']
659
+ c: Literal[4]
660
+ d: Literal['foo']
661
+ e: str
662
+
663
+ reveal_type(a + a) # E: Revealed type is 'builtins.int'
664
+ reveal_type(a + b) # E: Revealed type is 'builtins.int'
665
+ reveal_type(b + a) # E: Revealed type is 'builtins.int'
666
+ reveal_type(a + 1) # E: Revealed type is 'builtins.int'
667
+ reveal_type(1 + a) # E: Revealed type is 'builtins.int'
668
+ reveal_type(a + c) # E: Revealed type is 'builtins.int'
669
+ reveal_type(c + a) # E: Revealed type is 'builtins.int'
670
+
671
+ reveal_type(d + d) # E: Revealed type is 'builtins.str'
672
+ reveal_type(d + e) # E: Revealed type is 'builtins.str'
673
+ reveal_type(e + d) # E: Revealed type is 'builtins.str'
674
+ reveal_type(d + 'foo') # E: Revealed type is 'builtins.str'
675
+ reveal_type('foo' + d) # E: Revealed type is 'builtins.str'
676
+
677
+ reveal_type(a.__add__(b)) # E: Revealed type is 'builtins.int'
678
+ reveal_type(b.__add__(a)) # E: Revealed type is 'builtins.int'
679
+
680
+ a *= b # E: Incompatible types in assignment (expression has type "int", variable has type "Literal[3]")
681
+ b *= a
650
682
651
- reveal_type(a + a) # E: Revealed type is 'builtins.int'
652
- reveal_type(a + b) # E: Revealed type is 'builtins.int'
653
- reveal_type(b + a) # E: Revealed type is 'builtins.int'
654
- reveal_type(c.strip()) # E: Revealed type is 'builtins.str'
683
+ reveal_type(b) # E: Revealed type is 'builtins.int'
655
684
[out]
656
685
657
- [case testLiteralActualAssignment-skip]
658
- # TODO: fix this test. The 1 is currently always given a type of 'int'
686
+ [case testLiteralFallbackInheritedMethodsWorkCorrectly]
659
687
from typing_extensions import Literal
688
+ a: Literal['foo']
689
+ b: str
660
690
661
- a: Literal[1] = 1
691
+ reveal_type(a.startswith(a)) # E: Revealed type is 'builtins.bool'
692
+ reveal_type(b.startswith(a)) # E: Revealed type is 'builtins.bool'
693
+ reveal_type(a.startswith(b)) # E: Revealed type is 'builtins.bool'
694
+ reveal_type(a.strip()) # E: Revealed type is 'builtins.str'
695
+ [builtins fixtures/ops.pyi]
696
+ [out]
697
+
698
+ [case testLiteralFallbackMethodsDoNotCoerceToLiteral]
699
+ from typing_extensions import Literal
700
+
701
+ a: Literal[3]
702
+ b: int
703
+ c: Literal["foo"]
704
+
705
+ a = a * a # E: Incompatible types in assignment (expression has type "int", variable has type "Literal[3]")
706
+ a = a * b # E: Incompatible types in assignment (expression has type "int", variable has type "Literal[3]")
707
+ a = b * a # E: Incompatible types in assignment (expression has type "int", variable has type "Literal[3]")
708
+
709
+ b = a * a
710
+ b = a * b
711
+ b = b * a
712
+
713
+ c = c.strip() # E: Incompatible types in assignment (expression has type "str", variable has type "Literal['foo']")
714
+ [builtins fixtures/ops.pyi]
662
715
[out]
0 commit comments