diff --git a/mypy/checker.py b/mypy/checker.py index 0fb37450a015..b356dba0e06b 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -7072,6 +7072,8 @@ def refine_away_none_in_comparison( non_optional_types = [] for i in chain_indices: typ = operand_types[i] + if isinstance(get_proper_type(typ), AnyType): + continue if not is_overlapping_none(typ): non_optional_types.append(typ) diff --git a/test-data/unit/check-unreachable-code.test b/test-data/unit/check-unreachable-code.test index 98c676dbf42b..82f79269e4ad 100644 --- a/test-data/unit/check-unreachable-code.test +++ b/test-data/unit/check-unreachable-code.test @@ -1650,3 +1650,10 @@ def x() -> None: main:4: error: Statement is unreachable if 5: ^~~~~ + +[case testNoFalseUnreachableWithAnyEqualityNarrowing] +# flags: --warn-unreachable +from typing import Any, Optional +def main(contents: Any, commit: Optional[str]) -> None: + if contents.get("commit") == commit: + reveal_type(commit) # N: Revealed type is "builtins.str | None"