[wip] Better handling of generics when narrowing#20673
Draft
hauntsaninja wants to merge 3 commits intopython:masterfrom
Draft
[wip] Better handling of generics when narrowing#20673hauntsaninja wants to merge 3 commits intopython:masterfrom
hauntsaninja wants to merge 3 commits intopython:masterfrom
Conversation
hauntsaninja
commented
Jan 28, 2026
| self.x["asdf"] | ||
|
|
||
| if self.x == cast(dict[int, int], {}): | ||
| if self.x == cast(dict[int, int], {}): # E: Non-overlapping equality check (left operand type: "dict[str, str]", right operand type: "dict[int, int]") |
Collaborator
Author
There was a problem hiding this comment.
These changes come from adding --strict-equality to the test, not from the diff
hauntsaninja
commented
Jan 28, 2026
| # See testMatchEnumSingleChoice | ||
| target_type = coerce_to_literal(target_type) | ||
| if isinstance(target_type, Instance): | ||
| target_type = erase_type(target_type) |
Collaborator
Author
There was a problem hiding this comment.
We might want to distribute this over unions, but we do want to avoid erasure for several different types. Let's see if there is primer...
67e6322 to
1e598b3
Compare
This comment has been minimized.
This comment has been minimized.
1e598b3 to
98ed008
Compare
This comment has been minimized.
This comment has been minimized.
Contributor
|
Diff from mypy_primer, showing the effect of this PR on open source code: graphql-core (https://github.com/graphql-python/graphql-core)
- tests/execution/test_resolve.py:322: error: Value of type "list[SourceLocation] | None" is not indexable [index]
core (https://github.com/home-assistant/core)
+ homeassistant/components/philips_js/media_player.py:203: error: Argument 1 to "get_browse_image_url" of "MediaPlayerEntity" has incompatible type "str | None"; expected "str" [arg-type]
+ homeassistant/components/isy994/sensor.py:206: error: Argument 2 to "temperature" of "UnitSystem" has incompatible type "str | None"; expected "str" [arg-type]
rich (https://github.com/Textualize/rich)
+ rich/console.py:1540: error: Redundant cast to "Literal['left', 'center', 'right']" [redundant-cast]
discord.py (https://github.com/Rapptz/discord.py)
- discord/channel.py:3048: error: Argument "stickers" to "handle_message_parameters" has incompatible type "Sequence[GuildSticker | StickerItem]"; expected "list[str | int] | None" [arg-type]
+ discord/channel.py:3048: error: Argument "stickers" to "handle_message_parameters" has incompatible type "Sequence[Any]"; expected "list[str | int] | None" [arg-type]
dulwich (https://github.com/dulwich/dulwich)
+ dulwich/server.py:949: error: Argument 1 to "unread_proto_line" of "_ProtocolGraphWalker" has incompatible type "bytes | None"; expected "bytes" [arg-type]
rotki (https://github.com/rotki/rotki)
+ rotkehlchen/tests/integration/test_premium.py:820: error: Statement is unreachable [unreachable]
+ rotkehlchen/tests/integration/test_premium.py:834: error: Statement is unreachable [unreachable]
xarray (https://github.com/pydata/xarray)
+ xarray/tests/test_dataarray.py:291: error: Unused "type: ignore" comment [unused-ignore]
|
JukkaL
pushed a commit
that referenced
this pull request
Feb 2, 2026
Fixes #20701 / see discussion there Background: - In #20492 I added code to treat bytes, bytearray and memoryview as having custom `__eq__`, since they can compare equal to values that are of other types. I did this based on primer hits on draft versions of that PR - In #20643 I rewrote the handling of custom equality when narrowing. That fixed many issues, but regressed the equivalent of `testNarrowingOptionalBytes` because we're now more principled about custom `__eq__`. Perhaps interestingly the primer on that PR is small and not controversial, so I didn't feel the need to prioritise unsound things However, we only need one of the operands to have custom equality to model these things as reachable, so I think we can probably just remove `builtins.bytes` from here. This behaviour still isn't ideal. For instance, narrowing here shouldn't depend on whether we promote or not (we shouldn't narrow `v_all` to bytes) The real fix is adding more dedicated special casing so that we have an in-between option between "narrow assuming we can only compare equal to values of the same type" and "assume `__eq__` means we can't conclude anything about the positive case" Other relevant PRs: - #20673 attempts to remove the other custom eq logic for list, dict, set I added in #20492 . It should be close to mergeable - #20660 improves reachability and unfortunately to land it I have to do a rethink for how int, float, complex narrowing works. I actually think the current version has pretty good semantics and the primer is valid (but scary). But I want to think about it more
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.