Skip to content
Closed
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
4 changes: 4 additions & 0 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -7072,6 +7072,10 @@ def refine_away_none_in_comparison(
non_optional_types = []
for i in chain_indices:
typ = operand_types[i]
# Skip Any types - they could be None, so comparing with them
# shouldn't narrow away None from other operands.
if isinstance(get_proper_type(typ), AnyType):
continue
Copy link
Copy Markdown
Collaborator

@A5rocks A5rocks Jan 6, 2026

Choose a reason for hiding this comment

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

-snip-

Shouldn't is_overlapping_none(Any) conceptually work? What's the fallout of making that work? (and Any in a union, of course)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yeah, that's good point, updated to fix is_overlapping_none() instead

if not is_overlapping_none(typ):
non_optional_types.append(typ)

Expand Down
12 changes: 12 additions & 0 deletions test-data/unit/check-unreachable-code.test
Original file line number Diff line number Diff line change
Expand Up @@ -1650,3 +1650,15 @@ def x() -> None:
main:4: error: Statement is unreachable
if 5:
^~~~~

[case testNoFalseUnreachableWithAnyEqualityNarrowing]
# flags: --warn-unreachable
# Regression test for https://github.com/python/mypy/issues/20532
from typing import Any, Optional

def main(contents: Any, commit: Optional[str]) -> None:
if (
contents.get("commit") == commit
and (commit is not None or 1)
):
pass
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Could you add a reveal_type(commit) here? On master that will reveal str, whereas I think it should be str | None now?

Loading