Rework narrowing logic for equality and identity#20492
Merged
hauntsaninja merged 14 commits intopython:masterfrom Jan 16, 2026
Merged
Rework narrowing logic for equality and identity#20492hauntsaninja merged 14 commits intopython:masterfrom
hauntsaninja merged 14 commits intopython:masterfrom
Conversation
This comment has been minimized.
This comment has been minimized.
6e89ffd to
9e351d1
Compare
This comment has been minimized.
This comment has been minimized.
b29984a to
4dbd107
Compare
This comment has been minimized.
This comment has been minimized.
f574fef to
4a552d5
Compare
This comment has been minimized.
This comment has been minimized.
d143ddc to
ca5120d
Compare
This comment has been minimized.
This comment has been minimized.
bdfc626 to
586d2a1
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
0b15de0 to
7bc5dac
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This was referenced Jan 16, 2026
hauntsaninja
added a commit
that referenced
this pull request
Jan 16, 2026
This is a follow up to #20492 that is pure refactoring. I separated it out to make that change easier to review. We inline `narrow_identity_equality_comparison`, improve comments, etc. There is some further refactoring later in my commit stack too.
hauntsaninja
added a commit
that referenced
this pull request
Jan 16, 2026
Requested in #20492 (comment) Regression for the Never's in #18524 (comment)
hauntsaninja
added a commit
to hauntsaninja/mypy
that referenced
this pull request
Jan 17, 2026
This relates to changes made in python#20492
Collaborator
|
Noticed a minor regression in Home Assistant with # mypy: warn-unreachable
from enum import IntFlag
class WeatherFeature(IntFlag):
FORECAST_DAILY = 1
FORECAST_HOURLY = 2
FORECAST_TWICE_DAILY = 4
def func(supported_features: int) -> None:
reveal_type(supported_features)
if (supported_features & WeatherFeature.FORECAST_DAILY) == 0:
print("Hello World") # <-- This should work fine |
Collaborator
Author
|
Thanks, I have a follow up commit that will fix this case |
hauntsaninja
added a commit
that referenced
this pull request
Jan 21, 2026
Fixes some of the primer regressions from #20492 Fixes #19753 This should also happen to fix the case mentioned by cdce8p in #20492 (comment)
hauntsaninja
added a commit
to hauntsaninja/mypy
that referenced
this pull request
Jan 21, 2026
This relates to changes made in python#20492
hauntsaninja
added a commit
to hauntsaninja/mypy
that referenced
this pull request
Jan 22, 2026
This relates to changes made in python#20492
hauntsaninja
added a commit
to hauntsaninja/mypy
that referenced
this pull request
Jan 22, 2026
This relates to changes made in python#20492
hauntsaninja
added a commit
to hauntsaninja/mypy
that referenced
this pull request
Jan 22, 2026
This relates to changes made in python#20492
hauntsaninja
added a commit
to hauntsaninja/mypy
that referenced
this pull request
Jan 28, 2026
This relates to changes made in python#20492
hauntsaninja
added a commit
to hauntsaninja/mypy
that referenced
this pull request
Jan 28, 2026
This relates to changes made in python#20492
hauntsaninja
added a commit
to hauntsaninja/mypy
that referenced
this pull request
Jan 28, 2026
This relates to changes made in python#20492
JukkaL
pushed a commit
that referenced
this pull request
Jan 28, 2026
This was referenced Jan 30, 2026
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.
Mypy does not narrow as much as it could, which results in false positives.
We would also like to narrow based on containment. The PR for that was previously reverted due to inconsistencies between narrowing via equality and via containment. This fixes the inconsistency on the equality side and paves the road for adding narrowing via containment. That is, we lay groundwork for fixing #17864 and fixing #17841
Fixes #18524
Fixes #20041
Fixes #17162
Fixes #16830
Fixes #13704
Fixes #7642
Fixes #3964