Uh oh!
There was an error while loading. Please reload this page.
Better narrowing for enums and other types with known equality - #21281
Conversation
This comment has been minimized.
This comment has been minimized.
JukkaL
left a comment
There was a problem hiding this comment.
Thanks, a few minor comments, otherwise looks good.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
| 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']?" |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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
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] |
Uh oh!
There was an error while loading. Please reload this page.
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:
--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 #20701Co-authored-by Codex