Skip to content

Commit 0c2c57b

Browse files
committed
rewrite test cases
1 parent ef3824c commit 0c2c57b

2 files changed

Lines changed: 56 additions & 95 deletions

File tree

test-data/unit/check-narrowing.test

Lines changed: 55 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -2807,112 +2807,72 @@ class X:
28072807
[builtins fixtures/dict.pyi]
28082808

28092809

2810-
[case testTypeNarrowingStringInLiteralUnion]
2811-
from typing import Literal, Tuple
2812-
typ: Tuple[Literal['a', 'b'], ...] = ('a', 'b')
2813-
x: str = "hi!"
2814-
if x in typ:
2815-
reveal_type(x) # N: Revealed type is "Literal['a'] | Literal['b']"
2816-
else:
2817-
reveal_type(x) # N: Revealed type is "builtins.str"
2818-
[builtins fixtures/tuple.pyi]
2819-
[typing fixtures/typing-medium.pyi]
2810+
[case testTypeNarrowingStringInLiteralContainer]
2811+
# flags: --strict-equality --warn-unreachable
2812+
from typing import Literal
28202813

2821-
[case testTypeNarrowingStringInLiteralUnionSubset]
2822-
from typing import Literal, Tuple
2823-
typeAlpha: Tuple[Literal['a', 'b', 'c'], ...] = ('a', 'b')
2824-
strIn: str = "b"
2825-
strOut: str = "c"
2826-
if strIn in typeAlpha:
2827-
reveal_type(strIn) # N: Revealed type is "Literal['a'] | Literal['b'] | Literal['c']"
2828-
else:
2829-
reveal_type(strIn) # N: Revealed type is "builtins.str"
2830-
if strOut in typeAlpha:
2831-
reveal_type(strOut) # N: Revealed type is "Literal['a'] | Literal['b'] | Literal['c']"
2832-
else:
2833-
reveal_type(strOut) # N: Revealed type is "builtins.str"
2834-
[builtins fixtures/primitives.pyi]
2835-
[typing fixtures/typing-medium.pyi]
2814+
def narrow_tuple(x: str, t: tuple[Literal['a', 'b']]):
2815+
if x in t:
2816+
reveal_type(x) # N: Revealed type is "Literal['a'] | Literal['b']"
2817+
else:
2818+
reveal_type(x) # N: Revealed type is "builtins.str"
28362819

2837-
[case testNarrowingStringNotInLiteralUnion]
2838-
from typing import Literal, Tuple
2839-
typeAlpha: Tuple[Literal['a', 'b', 'c'],...] = ('a', 'b', 'c')
2840-
strIn: str = "c"
2841-
strOut: str = "d"
2842-
if strIn not in typeAlpha:
2843-
reveal_type(strIn) # N: Revealed type is "builtins.str"
2844-
else:
2845-
reveal_type(strIn) # N: Revealed type is "Literal['a'] | Literal['b'] | Literal['c']"
2846-
if strOut in typeAlpha:
2847-
reveal_type(strOut) # N: Revealed type is "Literal['a'] | Literal['b'] | Literal['c']"
2848-
else:
2849-
reveal_type(strOut) # N: Revealed type is "builtins.str"
2850-
[builtins fixtures/primitives.pyi]
2851-
[typing fixtures/typing-medium.pyi]
2820+
if x not in t:
2821+
reveal_type(x) # N: Revealed type is "builtins.str"
2822+
else:
2823+
reveal_type(x) # N: Revealed type is "Literal['a'] | Literal['b']"
28522824

2853-
[case testNarrowingStringInLiteralUnionDontExpand]
2854-
from typing import Literal, Tuple
2855-
typeAlpha: Tuple[Literal['a', 'b', 'c'], ...] = ('a', 'b', 'c')
2856-
strIn: Literal['c'] = "c"
2857-
reveal_type(strIn) # N: Revealed type is "Literal['c']"
2858-
#Check we don't expand a Literal into the Union type
2859-
if strIn not in typeAlpha:
2860-
reveal_type(strIn) # N: Revealed type is "Literal['c']"
2861-
else:
2862-
reveal_type(strIn) # N: Revealed type is "Literal['c']"
2825+
def narrow_homo_tuple(x: str, t: tuple[Literal['a', 'b'], ...]):
2826+
if x in t:
2827+
reveal_type(x) # N: Revealed type is "Literal['a'] | Literal['b']"
2828+
else:
2829+
reveal_type(x) # N: Revealed type is "builtins.str"
2830+
2831+
def narrow_list(x: str, t: list[Literal['a', 'b']]):
2832+
if x in t:
2833+
reveal_type(x) # N: Revealed type is "Literal['a'] | Literal['b']"
2834+
else:
2835+
reveal_type(x) # N: Revealed type is "builtins.str"
2836+
2837+
def narrow_set(x: str, t: set[Literal['a', 'b']]):
2838+
if x in t:
2839+
reveal_type(x) # N: Revealed type is "Literal['a'] | Literal['b']"
2840+
else:
2841+
reveal_type(x) # N: Revealed type is "builtins.str"
28632842
[builtins fixtures/primitives.pyi]
2864-
[typing fixtures/typing-medium.pyi]
28652843

2866-
[case testTypeNarrowingStringInMixedUnion]
2867-
from typing import Literal, Tuple
2868-
typ: Tuple[Literal['a', 'b'], ...] = ('a', 'b')
2869-
x: str = "hi!"
2870-
if x in typ:
2871-
reveal_type(x) # N: Revealed type is "Literal['a'] | Literal['b']"
2872-
else:
2873-
reveal_type(x) # N: Revealed type is "builtins.str"
2844+
2845+
[case testNarrowingLiteralInLiteralContainer]
2846+
# flags: --strict-equality --warn-unreachable
2847+
from typing import Literal
2848+
2849+
def narrow_tuple(x: Literal['c'], overlap: list[Literal['b', 'c']], no_overlap: list[Literal['a', 'b']]):
2850+
if x in overlap:
2851+
reveal_type(x) # N: Revealed type is "Literal['c']"
2852+
else:
2853+
reveal_type(x) # N: Revealed type is "Literal['c']"
2854+
2855+
if x in no_overlap:
2856+
reveal_type(x) # N: Revealed type is "Literal['c']"
2857+
else:
2858+
reveal_type(x) # N: Revealed type is "Literal['c']"
28742859
[builtins fixtures/tuple.pyi]
2875-
[typing fixtures/typing-medium.pyi]
28762860

2877-
[case testTypeNarrowingStringInSet]
2878-
from typing import Literal, Set
2879-
typ: Set[Literal['a', 'b']] = {'a', 'b'}
2880-
x: str = "hi!"
2881-
if x in typ:
2882-
reveal_type(x) # N: Revealed type is "Literal['a'] | Literal['b']"
2883-
else:
2884-
reveal_type(x) # N: Revealed type is "builtins.str"
2885-
if x not in typ:
2886-
reveal_type(x) # N: Revealed type is "builtins.str"
2887-
else:
2888-
reveal_type(x) # N: Revealed type is "Literal['a'] | Literal['b']"
2889-
[builtins fixtures/narrowing.pyi]
2890-
[typing fixtures/typing-medium.pyi]
2861+
[case testTypeNarrowingUnionInContainer]
2862+
# flags: --strict-equality --warn-unreachable
2863+
from typing import Union, Literal
28912864

2892-
[case testTypeNarrowingStringInList]
2893-
from typing import Literal, List
2894-
typ: List[Literal['a', 'b']] = ['a', 'b']
2895-
x: str = "hi!"
2896-
if x in typ:
2897-
reveal_type(x) # N: Revealed type is "Literal['a'] | Literal['b']"
2898-
else:
2899-
reveal_type(x) # N: Revealed type is "builtins.str"
2900-
if x not in typ:
2901-
reveal_type(x) # N: Revealed type is "builtins.str"
2902-
else:
2903-
reveal_type(x) # N: Revealed type is "Literal['a'] | Literal['b']"
2904-
[builtins fixtures/narrowing.pyi]
2905-
[typing fixtures/typing-medium.pyi]
2865+
def f1(x: Union[str, float], t1: list[Literal['a', 'b']], t2: list[str]):
2866+
if x in t1:
2867+
reveal_type(x) # N: Revealed type is "Literal['a'] | Literal['b']"
2868+
else:
2869+
reveal_type(x) # N: Revealed type is "builtins.str | builtins.float"
29062870

2907-
[case testTypeNarrowingUnionStringFloat]
2908-
from typing import Union
2909-
def foobar(foo: Union[str, float]):
2910-
if foo in ['a', 'b']:
2911-
reveal_type(foo) # N: Revealed type is "builtins.str"
2871+
if x in t2:
2872+
reveal_type(x) # N: Revealed type is "builtins.str"
29122873
else:
2913-
reveal_type(foo) # N: Revealed type is "builtins.str | builtins.float"
2874+
reveal_type(x) # N: Revealed type is "builtins.str | builtins.float"
29142875
[builtins fixtures/primitives.pyi]
2915-
[typing fixtures/typing-medium.pyi]
29162876

29172877
[case testNarrowAnyWithEqualityOrContainment]
29182878
# https://github.com/python/mypy/issues/17841

test-data/unit/fixtures/primitives.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class dict(Mapping[T, V]):
6363
def __iter__(self) -> Iterator[T]: pass
6464
class set(Iterable[T]):
6565
def __iter__(self) -> Iterator[T]: pass
66+
def __contains__(self, o: object, /) -> bool: ...
6667
class frozenset(Iterable[T]):
6768
def __iter__(self) -> Iterator[T]: pass
6869
class function: pass

0 commit comments

Comments
 (0)