Skip to content

Commit 0e30718

Browse files
committed
.
1 parent 655a8a2 commit 0e30718

11 files changed

Lines changed: 71 additions & 57 deletions

File tree

.github/workflows/mypy_primer.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ jobs:
6565
--new $GITHUB_SHA --old base_commit \
6666
--num-shards 6 --shard-index ${{ matrix.shard-index }} \
6767
--debug \
68-
--additional-flags="--debug-serialize" \
68+
--additional-flags="--debug-serialize --warn-unreachable" \
6969
--output concise \
7070
--mypy-install-librt \
7171
| tee diff_${{ matrix.shard-index }}.txt

mypy/checker.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6699,9 +6699,7 @@ def narrow_type_by_identity_equality(
66996699
# However, for non-value targets, we cannot do this narrowing,
67006700
# and so we ignore else_map
67016701
# e.g. if (x: str | None) != (y: str), we cannot narrow x to None
6702-
# TODO: this reachability gate is incorrect and should be removed
6703-
if not is_unreachable_map(if_map):
6704-
all_if_maps.append(if_map)
6702+
all_if_maps.append(if_map)
67056703

67066704
# Handle narrowing for operands with custom __eq__ methods specially
67076705
# In most cases, we won't be able to do any narrowing

mypy/server/aststrip.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,9 @@ def visit_class_def(self, node: ClassDef) -> None:
137137
node.base_type_exprs.extend(node.removed_base_type_exprs)
138138
node.removed_base_type_exprs = []
139139
node.defs.body = [
140-
s for s in node.defs.body if s not in to_delete # type: ignore[comparison-overlap]
140+
s
141+
for s in node.defs.body
142+
if s not in to_delete # type: ignore[comparison-overlap, redundant-expr]
141143
]
142144
with self.enter_class(node.info):
143145
super().visit_class_def(node)

mypy/typeshed/stubs/mypy-extensions/mypy_extensions.pyi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ class i64:
111111
def __ge__(self, x: i64) -> bool: ...
112112
def __gt__(self, x: i64) -> bool: ...
113113
def __index__(self) -> int: ...
114+
def __eq__(self, x: object) -> bool: ...
114115

115116
class i32:
116117
@overload
@@ -146,6 +147,7 @@ class i32:
146147
def __ge__(self, x: i32) -> bool: ...
147148
def __gt__(self, x: i32) -> bool: ...
148149
def __index__(self) -> int: ...
150+
def __eq__(self, x: object) -> bool: ...
149151

150152
class i16:
151153
@overload
@@ -181,6 +183,7 @@ class i16:
181183
def __ge__(self, x: i16) -> bool: ...
182184
def __gt__(self, x: i16) -> bool: ...
183185
def __index__(self) -> int: ...
186+
def __eq__(self, x: object) -> bool: ...
184187

185188
class u8:
186189
@overload
@@ -216,3 +219,4 @@ class u8:
216219
def __ge__(self, x: u8) -> bool: ...
217220
def __gt__(self, x: u8) -> bool: ...
218221
def __index__(self) -> int: ...
222+
def __eq__(self, x: object) -> bool: ...

test-data/unit/check-isinstance.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2138,7 +2138,7 @@ x: List[str]
21382138
y: Optional[int]
21392139

21402140
if y in x:
2141-
reveal_type(y) # N: Revealed type is "builtins.int | None"
2141+
reveal_type(y) # E: Statement is unreachable
21422142
else:
21432143
reveal_type(y) # N: Revealed type is "builtins.int | None"
21442144
[builtins fixtures/list.pyi]

test-data/unit/check-narrowing.test

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2722,7 +2722,7 @@ while True:
27222722
break
27232723
x = str()
27242724
if x == int(): # E: Non-overlapping equality check (left operand type: "str", right operand type: "int")
2725-
break
2725+
break # E: Statement is unreachable
27262726
[builtins fixtures/primitives.pyi]
27272727

27282728
[case testAvoidFalseNonOverlappingEqualityCheckInLoop2]
@@ -2735,7 +2735,7 @@ class C: ...
27352735
x = A()
27362736
while True:
27372737
if x == C(): # E: Non-overlapping equality check (left operand type: "A | B", right operand type: "C")
2738-
break
2738+
break # E: Statement is unreachable
27392739
x = B()
27402740
[builtins fixtures/primitives.pyi]
27412741

@@ -3127,7 +3127,7 @@ def narrow_tuple(x: Literal['c'], overlap: list[Literal['b', 'c']], no_overlap:
31273127
reveal_type(x) # N: Revealed type is "Literal['c']"
31283128

31293129
if x in no_overlap:
3130-
reveal_type(x) # N: Revealed type is "Literal['c']"
3130+
reveal_type(x) # E: Statement is unreachable
31313131
else:
31323132
reveal_type(x) # N: Revealed type is "Literal['c']"
31333133
[builtins fixtures/tuple.pyi]
@@ -3169,8 +3169,8 @@ def f2(x: Any) -> None:
31693169

31703170
def bad_compare_has_key(has_key: bool, key: str, s: tuple[str, ...]) -> None:
31713171
if has_key == key in s: # E: Non-overlapping equality check (left operand type: "bool", right operand type: "str")
3172-
reveal_type(has_key) # N: Revealed type is "builtins.bool"
3173-
reveal_type(key) # N: Revealed type is "builtins.str"
3172+
reveal_type(has_key) # E: Statement is unreachable
3173+
reveal_type(key)
31743174

31753175
def bad_but_should_pass(has_key: bool, key: bool, s: tuple[bool, ...]) -> None:
31763176
if has_key == key in s:
@@ -3537,6 +3537,7 @@ def bar(y: Any):
35373537
reveal_type(y) # N: Revealed type is "Any"
35383538
[builtins fixtures/dict-full.pyi]
35393539

3540+
35403541
[case testNarrowTypeVarType]
35413542
# flags: --strict-equality --warn-unreachable
35423543
from typing import TypeVar
@@ -3567,14 +3568,14 @@ TargetType = TypeVar("TargetType", int, float, str)
35673568
# TODO: this behaviour is incorrect, it will be fixed by improving reachability
35683569
def convert_type(target_type: Type[TargetType]) -> TargetType:
35693570
if target_type == str:
3570-
return str() # E: Incompatible return value type (got "str", expected "int") \
3571-
# E: Incompatible return value type (got "str", expected "float")
3571+
return str()
35723572
if target_type == int:
3573-
return int() # E: Incompatible return value type (got "int", expected "str")
3573+
return int()
35743574
if target_type == float:
3575-
return float() # E: Incompatible return value type (got "float", expected "int") \
3576-
# E: Incompatible return value type (got "float", expected "str")
3575+
return float() # E: Incompatible return value type (got "float", expected "int")
35773576
raise
3577+
3578+
35783579
[builtins fixtures/primitives.pyi]
35793580

35803581

test-data/unit/check-optional.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -493,11 +493,11 @@ from typing import Optional
493493

494494
def main(x: Optional[str]):
495495
if x == 0:
496-
reveal_type(x) # N: Revealed type is "builtins.str | None"
496+
reveal_type(x) # E: Statement is unreachable
497497
else:
498498
reveal_type(x) # N: Revealed type is "builtins.str | None"
499499
if x is 0:
500-
reveal_type(x) # N: Revealed type is "builtins.str | None"
500+
reveal_type(x) # E: Statement is unreachable
501501
else:
502502
reveal_type(x) # N: Revealed type is "builtins.str | None"
503503
[builtins fixtures/ops.pyi]

test-data/unit/check-python310.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ m: A
9191

9292
match m:
9393
case b.b:
94-
reveal_type(m) # N: Revealed type is "__main__.A"
94+
reveal_type(m) # E: Statement is unreachable
9595
[file b.py]
9696
class B: ...
9797
b: B
@@ -104,7 +104,7 @@ m: int
104104

105105
match m:
106106
case b.b:
107-
reveal_type(m) # N: Revealed type is "builtins.int"
107+
reveal_type(m) # E: Statement is unreachable
108108
[file b.py]
109109
b: str
110110
[builtins fixtures/primitives.pyi]
@@ -2849,7 +2849,7 @@ def x() -> tuple[Literal["test"]]: ...
28492849

28502850
match x():
28512851
case (x,) if x == "test": # E: Incompatible types in capture pattern (pattern captures type "Literal['test']", variable has type "Callable[[], tuple[Literal['test']]]")
2852-
reveal_type(x) # N: Revealed type is "def () -> tuple[Literal['test']]"
2852+
reveal_type(x) # E: Statement is unreachable
28532853
case foo:
28542854
foo
28552855

test-data/unit/check-tuples.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1540,7 +1540,7 @@ class B: pass
15401540

15411541
def f1(possibles: Tuple[int, Tuple[A]], x: Optional[Tuple[B]]):
15421542
if x in possibles:
1543-
reveal_type(x) # N: Revealed type is "tuple[__main__.B]"
1543+
reveal_type(x) # E: Statement is unreachable
15441544
else:
15451545
reveal_type(x) # N: Revealed type is "tuple[__main__.B] | None"
15461546

test-data/unit/check-unreachable-code.test

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -424,24 +424,22 @@ x = 1
424424
[case testCustomSysVersionInfo]
425425
# flags: --python-version 3.11
426426
import sys
427-
if sys.version_info == (3, 11):
427+
if sys.version_info >= (3, 11):
428428
x = "foo"
429429
else:
430430
x = 3
431431
reveal_type(x) # N: Revealed type is "builtins.str"
432432
[builtins fixtures/ops.pyi]
433-
[out]
434433

435434
[case testCustomSysVersionInfo2]
436435
# flags: --python-version 3.11
437436
import sys
438-
if sys.version_info == (3, 6):
437+
if sys.version_info >= (3, 12):
439438
x = "foo"
440439
else:
441440
x = 3
442441
reveal_type(x) # N: Revealed type is "builtins.int"
443442
[builtins fixtures/ops.pyi]
444-
[out]
445443

446444
[case testCustomSysPlatform]
447445
# flags: --platform linux
@@ -894,42 +892,53 @@ def baz(x: int) -> int:
894892
import sys
895893
from typing import TYPE_CHECKING
896894

897-
x: int
898-
if TYPE_CHECKING:
899-
reveal_type(x) # N: Revealed type is "builtins.int"
900-
else:
901-
reveal_type(x)
895+
def f1(x: int) -> None:
896+
if TYPE_CHECKING:
897+
reveal_type(x) # N: Revealed type is "builtins.int"
898+
else:
899+
reveal_type(x)
902900

903-
if not TYPE_CHECKING:
904-
reveal_type(x)
905-
else:
906-
reveal_type(x) # N: Revealed type is "builtins.int"
901+
def f2(x: int) -> None:
902+
if not TYPE_CHECKING:
903+
reveal_type(x)
904+
else:
905+
reveal_type(x) # N: Revealed type is "builtins.int"
907906

908-
if sys.platform == 'darwin':
909-
reveal_type(x)
910-
else:
911-
reveal_type(x) # N: Revealed type is "builtins.int"
907+
def f3(x: int) -> None:
908+
if sys.platform == 'darwin':
909+
reveal_type(x)
910+
else:
911+
reveal_type(x) # N: Revealed type is "builtins.int"
912912

913-
if sys.platform == 'win32':
914-
reveal_type(x) # N: Revealed type is "builtins.int"
915-
else:
916-
reveal_type(x)
913+
def f4(x: int) -> None:
914+
if sys.platform == 'win32':
915+
reveal_type(x) # N: Revealed type is "builtins.int"
916+
else:
917+
reveal_type(x)
917918

918-
if sys.version_info == (2, 7):
919-
reveal_type(x)
920-
else:
921-
reveal_type(x) # N: Revealed type is "builtins.int"
919+
def f5(x: int) -> None:
920+
if sys.version_info == (2, 7):
921+
reveal_type(x)
922+
else:
923+
reveal_type(x) # N: Revealed type is "builtins.int"
922924

923-
if sys.version_info == (3, 9):
924-
reveal_type(x) # N: Revealed type is "builtins.int"
925-
else:
926-
reveal_type(x)
925+
def f6(x: int) -> None:
926+
if sys.version_info >= (3, 9):
927+
reveal_type(x) # N: Revealed type is "builtins.int"
928+
else:
929+
reveal_type(x)
927930

928-
FOOBAR = ""
929-
if FOOBAR:
930-
reveal_type(x)
931-
else:
932-
reveal_type(x) # N: Revealed type is "builtins.int"
931+
if sys.version_info >= (3, 10):
932+
reveal_type(x)
933+
else:
934+
reveal_type(x) # N: Revealed type is "builtins.int"
935+
936+
def f7(x: int) -> None:
937+
FOOBAR = ""
938+
if FOOBAR:
939+
reveal_type(x)
940+
else:
941+
reveal_type(x) # N: Revealed type is "builtins.int"
933942
[builtins fixtures/ops.pyi]
934943
[typing fixtures/typing-medium.pyi]
935944

0 commit comments

Comments
 (0)