Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
.
  • Loading branch information
hauntsaninja committed Feb 7, 2026
commit 0e307181ef0c4821eb399bda88d73056179a0e11
2 changes: 1 addition & 1 deletion .github/workflows/mypy_primer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ jobs:
--new $GITHUB_SHA --old base_commit \
--num-shards 6 --shard-index ${{ matrix.shard-index }} \
--debug \
--additional-flags="--debug-serialize" \
--additional-flags="--debug-serialize --warn-unreachable" \
--output concise \
--mypy-install-librt \
| tee diff_${{ matrix.shard-index }}.txt
Expand Down
4 changes: 1 addition & 3 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -6699,9 +6699,7 @@ def narrow_type_by_identity_equality(
# However, for non-value targets, we cannot do this narrowing,
# and so we ignore else_map
# e.g. if (x: str | None) != (y: str), we cannot narrow x to None
# TODO: this reachability gate is incorrect and should be removed
if not is_unreachable_map(if_map):
all_if_maps.append(if_map)
all_if_maps.append(if_map)

# Handle narrowing for operands with custom __eq__ methods specially
# In most cases, we won't be able to do any narrowing
Expand Down
4 changes: 3 additions & 1 deletion mypy/server/aststrip.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ def visit_class_def(self, node: ClassDef) -> None:
node.base_type_exprs.extend(node.removed_base_type_exprs)
node.removed_base_type_exprs = []
node.defs.body = [
s for s in node.defs.body if s not in to_delete # type: ignore[comparison-overlap]
s
for s in node.defs.body
if s not in to_delete # type: ignore[comparison-overlap, redundant-expr]
]
with self.enter_class(node.info):
super().visit_class_def(node)
Expand Down
4 changes: 4 additions & 0 deletions mypy/typeshed/stubs/mypy-extensions/mypy_extensions.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class i64:
def __ge__(self, x: i64) -> bool: ...
def __gt__(self, x: i64) -> bool: ...
def __index__(self) -> int: ...
def __eq__(self, x: object) -> bool: ...
Copy link
Copy Markdown
Collaborator Author

@hauntsaninja hauntsaninja Jan 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without (correctly) annotating the custom equality here, we would consider otherwise reachable code to be unreachable. Specifically the right side of this or clause:

if meta[0] != cache_version() or meta[1] != CACHE_VERSION:


class i32:
@overload
Expand Down Expand Up @@ -146,6 +147,7 @@ class i32:
def __ge__(self, x: i32) -> bool: ...
def __gt__(self, x: i32) -> bool: ...
def __index__(self) -> int: ...
def __eq__(self, x: object) -> bool: ...

class i16:
@overload
Expand Down Expand Up @@ -181,6 +183,7 @@ class i16:
def __ge__(self, x: i16) -> bool: ...
def __gt__(self, x: i16) -> bool: ...
def __index__(self) -> int: ...
def __eq__(self, x: object) -> bool: ...

class u8:
@overload
Expand Down Expand Up @@ -216,3 +219,4 @@ class u8:
def __ge__(self, x: u8) -> bool: ...
def __gt__(self, x: u8) -> bool: ...
def __index__(self) -> int: ...
def __eq__(self, x: object) -> bool: ...
2 changes: 1 addition & 1 deletion test-data/unit/check-isinstance.test
Original file line number Diff line number Diff line change
Expand Up @@ -2138,7 +2138,7 @@ x: List[str]
y: Optional[int]

if y in x:
reveal_type(y) # N: Revealed type is "builtins.int | None"
reveal_type(y) # E: Statement is unreachable
else:
reveal_type(y) # N: Revealed type is "builtins.int | None"
[builtins fixtures/list.pyi]
Expand Down
21 changes: 11 additions & 10 deletions test-data/unit/check-narrowing.test
Original file line number Diff line number Diff line change
Expand Up @@ -2722,7 +2722,7 @@ while True:
break
x = str()
if x == int(): # E: Non-overlapping equality check (left operand type: "str", right operand type: "int")
break
break # E: Statement is unreachable
[builtins fixtures/primitives.pyi]

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

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

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

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

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


[case testNarrowTypeVarType]
# flags: --strict-equality --warn-unreachable
from typing import TypeVar
Expand Down Expand Up @@ -3567,14 +3568,14 @@ TargetType = TypeVar("TargetType", int, float, str)
# TODO: this behaviour is incorrect, it will be fixed by improving reachability
def convert_type(target_type: Type[TargetType]) -> TargetType:
if target_type == str:
return str() # E: Incompatible return value type (got "str", expected "int") \
# E: Incompatible return value type (got "str", expected "float")
return str()
if target_type == int:
return int() # E: Incompatible return value type (got "int", expected "str")
return int()
if target_type == float:
return float() # E: Incompatible return value type (got "float", expected "int") \
# E: Incompatible return value type (got "float", expected "str")
return float() # E: Incompatible return value type (got "float", expected "int")
raise


[builtins fixtures/primitives.pyi]


Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-optional.test
Original file line number Diff line number Diff line change
Expand Up @@ -493,11 +493,11 @@ from typing import Optional

def main(x: Optional[str]):
if x == 0:
reveal_type(x) # N: Revealed type is "builtins.str | None"
reveal_type(x) # E: Statement is unreachable
else:
reveal_type(x) # N: Revealed type is "builtins.str | None"
if x is 0:
reveal_type(x) # N: Revealed type is "builtins.str | None"
reveal_type(x) # E: Statement is unreachable
else:
reveal_type(x) # N: Revealed type is "builtins.str | None"
[builtins fixtures/ops.pyi]
Expand Down
6 changes: 3 additions & 3 deletions test-data/unit/check-python310.test
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ m: A

match m:
case b.b:
reveal_type(m) # N: Revealed type is "__main__.A"
reveal_type(m) # E: Statement is unreachable
[file b.py]
class B: ...
b: B
Expand All @@ -104,7 +104,7 @@ m: int

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

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

Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-tuples.test
Original file line number Diff line number Diff line change
Expand Up @@ -1540,7 +1540,7 @@ class B: pass

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

Expand Down
77 changes: 43 additions & 34 deletions test-data/unit/check-unreachable-code.test
Original file line number Diff line number Diff line change
Expand Up @@ -424,24 +424,22 @@ x = 1
[case testCustomSysVersionInfo]
# flags: --python-version 3.11
import sys
if sys.version_info == (3, 11):
if sys.version_info >= (3, 11):
x = "foo"
else:
x = 3
reveal_type(x) # N: Revealed type is "builtins.str"
[builtins fixtures/ops.pyi]
[out]

[case testCustomSysVersionInfo2]
# flags: --python-version 3.11
import sys
if sys.version_info == (3, 6):
if sys.version_info >= (3, 12):
x = "foo"
else:
x = 3
reveal_type(x) # N: Revealed type is "builtins.int"
[builtins fixtures/ops.pyi]
[out]

[case testCustomSysPlatform]
# flags: --platform linux
Expand Down Expand Up @@ -894,42 +892,53 @@ def baz(x: int) -> int:
import sys
from typing import TYPE_CHECKING

x: int
if TYPE_CHECKING:
reveal_type(x) # N: Revealed type is "builtins.int"
else:
reveal_type(x)
def f1(x: int) -> None:
if TYPE_CHECKING:
reveal_type(x) # N: Revealed type is "builtins.int"
else:
reveal_type(x)

if not TYPE_CHECKING:
reveal_type(x)
else:
reveal_type(x) # N: Revealed type is "builtins.int"
def f2(x: int) -> None:
if not TYPE_CHECKING:
reveal_type(x)
else:
reveal_type(x) # N: Revealed type is "builtins.int"

if sys.platform == 'darwin':
reveal_type(x)
else:
reveal_type(x) # N: Revealed type is "builtins.int"
def f3(x: int) -> None:
if sys.platform == 'darwin':
reveal_type(x)
else:
reveal_type(x) # N: Revealed type is "builtins.int"

if sys.platform == 'win32':
reveal_type(x) # N: Revealed type is "builtins.int"
else:
reveal_type(x)
def f4(x: int) -> None:
if sys.platform == 'win32':
reveal_type(x) # N: Revealed type is "builtins.int"
else:
reveal_type(x)

if sys.version_info == (2, 7):
reveal_type(x)
else:
reveal_type(x) # N: Revealed type is "builtins.int"
def f5(x: int) -> None:
if sys.version_info == (2, 7):
reveal_type(x)
else:
reveal_type(x) # N: Revealed type is "builtins.int"

if sys.version_info == (3, 9):
reveal_type(x) # N: Revealed type is "builtins.int"
else:
reveal_type(x)
def f6(x: int) -> None:
if sys.version_info >= (3, 9):
reveal_type(x) # N: Revealed type is "builtins.int"
else:
reveal_type(x)

FOOBAR = ""
if FOOBAR:
reveal_type(x)
else:
reveal_type(x) # N: Revealed type is "builtins.int"
if sys.version_info >= (3, 10):
reveal_type(x)
else:
reveal_type(x) # N: Revealed type is "builtins.int"

def f7(x: int) -> None:
FOOBAR = ""
if FOOBAR:
reveal_type(x)
else:
reveal_type(x) # N: Revealed type is "builtins.int"
[builtins fixtures/ops.pyi]
[typing fixtures/typing-medium.pyi]

Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/cmdline.test
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ python_version = 3.14
# cmd: mypy main.py
[file main.py]
import sys
if sys.version_info == (3, 9): # Update here when bumping the min Python version!
if sys.version_info < (3, 10): # Update here when bumping the min Python version!
reveal_type("good")
[file mypy.ini]
\[mypy]
Expand Down