Skip to content

Commit 28fefe6

Browse files
committed
correct tests
1 parent da4796a commit 28fefe6

7 files changed

Lines changed: 59 additions & 40 deletions

File tree

test-data/unit/check-enum.test

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,7 +1032,7 @@ else:
10321032
reveal_type(z) # No output: this branch is unreachable
10331033
[builtins fixtures/bool.pyi]
10341034

1035-
[case testEnumReachabilityNoNarrowingForUnionMessiness]
1035+
[case testEnumReachabilityNarrowingForUnionMessiness]
10361036
from enum import Enum
10371037
from typing import Literal
10381038

@@ -1045,17 +1045,16 @@ x: Foo
10451045
y: Literal[Foo.A, Foo.B]
10461046
z: Literal[Foo.B, Foo.C]
10471047

1048-
# For the sake of simplicity, no narrowing is done when the narrower type is a Union.
10491048
if x is y:
1050-
reveal_type(x) # N: Revealed type is "__main__.Foo"
1049+
reveal_type(x) # N: Revealed type is "Literal[__main__.Foo.A] | Literal[__main__.Foo.B]"
10511050
reveal_type(y) # N: Revealed type is "Literal[__main__.Foo.A] | Literal[__main__.Foo.B]"
10521051
else:
10531052
reveal_type(x) # N: Revealed type is "__main__.Foo"
10541053
reveal_type(y) # N: Revealed type is "Literal[__main__.Foo.A] | Literal[__main__.Foo.B]"
10551054

10561055
if y is z:
1057-
reveal_type(y) # N: Revealed type is "Literal[__main__.Foo.A] | Literal[__main__.Foo.B]"
1058-
reveal_type(z) # N: Revealed type is "Literal[__main__.Foo.B] | Literal[__main__.Foo.C]"
1056+
reveal_type(y) # N: Revealed type is "Literal[__main__.Foo.B]"
1057+
reveal_type(z) # N: Revealed type is "Literal[__main__.Foo.B]"
10591058
else:
10601059
reveal_type(y) # N: Revealed type is "Literal[__main__.Foo.A] | Literal[__main__.Foo.B]"
10611060
reveal_type(z) # N: Revealed type is "Literal[__main__.Foo.B] | Literal[__main__.Foo.C]"

test-data/unit/check-flags.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2377,7 +2377,7 @@ x: int = "" # E: Incompatible types in assignment (expression has type "str", v
23772377
x: int = "" # E: Incompatible types in assignment (expression has type "str", variable has type "int")
23782378

23792379
[case testDisableBytearrayPromotion]
2380-
# flags: --disable-bytearray-promotion --strict-equality
2380+
# flags: --disable-bytearray-promotion --strict-equality --warn-unreachable
23812381
def f(x: bytes) -> None: ...
23822382
f(bytearray(b"asdf")) # E: Argument 1 to "f" has incompatible type "bytearray"; expected "bytes"
23832383
f(memoryview(b"asdf"))

test-data/unit/check-isinstance.test

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2760,13 +2760,14 @@ else:
27602760
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
27612761

27622762
[case testTypeEqualsMultipleTypesShouldntNarrow]
2763+
# flags: --warn-unreachable
27632764
# make sure we don't do any narrowing if there are multiple types being compared
27642765

27652766
from typing import Union
27662767

27672768
x: Union[int, str]
27682769
if type(x) == int == str:
2769-
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
2770+
reveal_type(x) # E: Statement is unreachable
27702771
else:
27712772
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"
27722773

test-data/unit/check-narrowing.test

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,13 +1364,13 @@ class A: ...
13641364
val: Optional[A]
13651365

13661366
if val == None:
1367-
reveal_type(val) # N: Revealed type is "__main__.A | None"
1367+
reveal_type(val) # N: Revealed type is "None"
13681368
else:
13691369
reveal_type(val) # N: Revealed type is "__main__.A"
13701370
if val != None:
13711371
reveal_type(val) # N: Revealed type is "__main__.A"
13721372
else:
1373-
reveal_type(val) # N: Revealed type is "__main__.A | None"
1373+
reveal_type(val) # N: Revealed type is "None"
13741374

13751375
if val in (None,):
13761376
reveal_type(val) # N: Revealed type is "__main__.A | None"
@@ -1380,6 +1380,19 @@ if val not in (None,):
13801380
reveal_type(val) # N: Revealed type is "__main__.A | None"
13811381
else:
13821382
reveal_type(val) # N: Revealed type is "__main__.A | None"
1383+
1384+
class Hmm:
1385+
def __eq__(self, other) -> bool: ...
1386+
1387+
hmm: Optional[Hmm]
1388+
if hmm == None:
1389+
reveal_type(hmm) # N: Revealed type is "__main__.Hmm | None"
1390+
else:
1391+
reveal_type(hmm) # N: Revealed type is "__main__.Hmm"
1392+
if hmm != None:
1393+
reveal_type(hmm) # N: Revealed type is "__main__.Hmm"
1394+
else:
1395+
reveal_type(hmm) # N: Revealed type is "__main__.Hmm | None"
13831396
[builtins fixtures/primitives.pyi]
13841397

13851398
[case testNarrowingWithTupleOfTypes]
@@ -2277,7 +2290,7 @@ def f4(x: SE) -> None:
22772290
# https://github.com/python/mypy/issues/17864
22782291
def f(x: str | int) -> None:
22792292
if x == "x":
2280-
reveal_type(x) # N: Revealed type is "builtins.str | builtins.int"
2293+
reveal_type(x) # N: Revealed type is "builtins.str"
22812294
y = x
22822295

22832296
if x in ["x"]:

test-data/unit/check-optional.test

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -445,28 +445,29 @@ foo([f]) # E: List item 0 has incompatible type "Callable[[], int]"; expected "
445445

446446
[case testInferEqualsNotOptional]
447447
from typing import Optional
448-
x = '' # type: Optional[str]
449-
if x == '<string>':
450-
reveal_type(x) # N: Revealed type is "builtins.str"
451-
else:
452-
reveal_type(x) # N: Revealed type is "builtins.str | None"
453-
if x is '<string>':
454-
reveal_type(x) # N: Revealed type is "builtins.str"
455-
else:
456-
reveal_type(x) # N: Revealed type is "builtins.str | None"
448+
449+
def main(x: Optional[str]):
450+
if x == '<string>':
451+
reveal_type(x) # N: Revealed type is "builtins.str"
452+
else:
453+
reveal_type(x) # N: Revealed type is "builtins.str | None"
454+
if x is '<string>':
455+
reveal_type(x) # N: Revealed type is "Literal['<string>']"
456+
else:
457+
reveal_type(x) # N: Revealed type is "builtins.str | None"
457458
[builtins fixtures/ops.pyi]
458459

459460
[case testInferEqualsNotOptionalWithUnion]
460461
from typing import Union
461-
x = '' # type: Union[str, int, None]
462-
if x == '<string>':
463-
reveal_type(x) # N: Revealed type is "builtins.str | builtins.int"
464-
else:
465-
reveal_type(x) # N: Revealed type is "builtins.str | builtins.int | None"
466-
if x is '<string>':
467-
reveal_type(x) # N: Revealed type is "builtins.str | builtins.int"
468-
else:
469-
reveal_type(x) # N: Revealed type is "builtins.str | builtins.int | None"
462+
def main(x: Union[str, int, None]):
463+
if x == '<string>':
464+
reveal_type(x) # N: Revealed type is "builtins.str"
465+
else:
466+
reveal_type(x) # N: Revealed type is "builtins.str | builtins.int | None"
467+
if x is '<string>':
468+
reveal_type(x) # N: Revealed type is "Literal['<string>']"
469+
else:
470+
reveal_type(x) # N: Revealed type is "builtins.str | builtins.int | None"
470471
[builtins fixtures/ops.pyi]
471472

472473
[case testInferEqualsNotOptionalWithOverlap]
@@ -483,28 +484,30 @@ else:
483484
[builtins fixtures/ops.pyi]
484485

485486
[case testInferEqualsStillOptionalWithNoOverlap]
487+
# flags: --warn-unreachable
486488
from typing import Optional
487-
x = '' # type: Optional[str]
488-
if x == 0:
489-
reveal_type(x) # N: Revealed type is "builtins.str | None"
490-
else:
491-
reveal_type(x) # N: Revealed type is "builtins.str | None"
492-
if x is 0:
493-
reveal_type(x) # N: Revealed type is "builtins.str | None"
494-
else:
495-
reveal_type(x) # N: Revealed type is "builtins.str | None"
489+
490+
def main(x: Optional[str]):
491+
if x == 0:
492+
reveal_type(x) # E: Statement is unreachable
493+
else:
494+
reveal_type(x) # N: Revealed type is "builtins.str | None"
495+
if x is 0:
496+
reveal_type(x) # N: Revealed type is "builtins.str | None"
497+
else:
498+
reveal_type(x) # N: Revealed type is "builtins.str | None"
496499
[builtins fixtures/ops.pyi]
497500

498501
[case testInferEqualsStillOptionalWithBothOptional]
499502
from typing import Union
500503
x = '' # type: Union[str, int, None]
501504
y = '' # type: Union[str, None]
502505
if x == y:
503-
reveal_type(x) # N: Revealed type is "builtins.str | builtins.int | None"
506+
reveal_type(x) # N: Revealed type is "builtins.str | None"
504507
else:
505508
reveal_type(x) # N: Revealed type is "builtins.str | builtins.int | None"
506509
if x is y:
507-
reveal_type(x) # N: Revealed type is "builtins.str | builtins.int | None"
510+
reveal_type(x) # N: Revealed type is "builtins.str | None"
508511
else:
509512
reveal_type(x) # N: Revealed type is "builtins.str | builtins.int | None"
510513
[builtins fixtures/ops.pyi]

test-data/unit/check-python310.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ m: object
6161

6262
match m:
6363
case b.b:
64-
reveal_type(m) # N: Revealed type is "builtins.object"
64+
reveal_type(m) # N: Revealed type is "builtins.int"
6565
[file b.py]
6666
b: int
6767

test-data/unit/fixtures/primitives.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,19 @@ class bytes(Sequence[int]):
3939
def __iter__(self) -> Iterator[int]: pass
4040
def __contains__(self, other: object) -> bool: pass
4141
def __getitem__(self, item: int) -> int: pass
42+
def __eq__(self, other: object) -> bool: pass
4243
class bytearray(Sequence[int]):
4344
def __init__(self, x: bytes) -> None: pass
4445
def __iter__(self) -> Iterator[int]: pass
4546
def __contains__(self, other: object) -> bool: pass
4647
def __getitem__(self, item: int) -> int: pass
48+
def __eq__(self, other: object) -> bool: pass
4749
class memoryview(Sequence[int]):
4850
def __init__(self, x: bytes) -> None: pass
4951
def __iter__(self) -> Iterator[int]: pass
5052
def __contains__(self, other: object) -> bool: pass
5153
def __getitem__(self, item: int) -> int: pass
54+
def __eq__(self, other: object) -> bool: pass
5255
class tuple(Generic[T]):
5356
def __contains__(self, other: object) -> bool: pass
5457
class list(Sequence[T]):

0 commit comments

Comments
 (0)