Skip to content

Better narrowing for enums and other types with known equality#21281

Merged
hauntsaninja merged 2 commits intopython:masterfrom
hauntsaninja:enumident2
Apr 24, 2026
Merged

Better narrowing for enums and other types with known equality#21281
hauntsaninja merged 2 commits intopython:masterfrom
hauntsaninja:enumident2

Conversation

@hauntsaninja
Copy link
Copy Markdown
Collaborator

@hauntsaninja hauntsaninja commented Apr 21, 2026

Fixes #21187, fully fixes this comment #9003 (comment) (previously improved on in 1.20 narrowing changes)

This diff adds general functionality that replaces a few different pieces of ad hoc logic:

  • The conservative enum handling is now generalised
  • We can get rid of the special cased logic for None when narrowing contains, it is fully subsumed by more general logic
  • We no longer rely on promotion behaviour to narrow numeric types correctly
  • Similarly, we now narrow bytes consistently, regardless of --strict-bytes. We also no longer have to mark memoryview and bytearray as having custom equality implementations. That is, it is a long term fix for Optional bytes type not narrowed after asserting equality #20701

Co-authored-by Codex

@github-actions

This comment has been minimized.

@hauntsaninja hauntsaninja marked this pull request as ready for review April 21, 2026 07:59
@hauntsaninja hauntsaninja changed the title Better narrowing for enums and other known types with equality Better narrowing for enums and other types with known equality Apr 21, 2026
Copy link
Copy Markdown
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

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

Thanks, a few minor comments, otherwise looks good.

Comment thread mypy/checker.py Outdated
Comment thread mypy/checker.py Outdated
if v_object == v_bytes:
reveal_type(v_object) # N: Revealed type is "builtins.bytes"
if v_object == b"asdf":
reveal_type(v_object) # N: Revealed type is "Literal[b'asdf']?"
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.

The above two reveal types aren't strictly consistent, but it's probably fine since mixing bytearray or memoryview objects with bytes is very rare (though it happens occasionally).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Agreed. This PR doesn't actually affect that behaviour, but I added this to the test case because I wanted to document it for this reason

@github-actions
Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

rich (https://github.com/Textualize/rich)
+ rich/console.py:1542: error: Redundant cast to "Literal['left', 'center', 'right']"  [redundant-cast]

cryptography (https://github.com/pyca/cryptography)
+ src/cryptography/hazmat/primitives/serialization/ssh.py:1120: error: Argument 16 to "SSHCertificate" has incompatible type "bytes | Any | memoryview[int]"; expected "bytes"  [arg-type]

@hauntsaninja hauntsaninja merged commit 1e96fb7 into python:master Apr 24, 2026
24 checks passed
@hauntsaninja hauntsaninja deleted the enumident2 branch April 24, 2026 22:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[1.20 regression] Type narrowing is order sensitive in match-case statement with narrowed enum type

2 participants