Skip to content

Fix --strict-equality for iteratively visited code #19635

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

tyralla
Copy link
Collaborator

@tyralla tyralla commented Aug 10, 2025

Fixes #19328

The logic is very similar to what we did to report different revealed types that were discovered in multiple iteration steps in one line. I think this fix is the last one needed before I can implement #19256.

This comment has been minimized.

@tyralla
Copy link
Collaborator Author

tyralla commented Aug 10, 2025

The primer results are a little mysterious. I could simplify to:

for y in [1.0]:
    if y is not None or y != "None":  # E: Non-overlapping equality check (left operand type: "float", right operand type: "Literal['None']")
        ...

This obvious error is only reported with my change, but I do not know why current master misses it and therefore have no idea what in this PR contributes to detecting it.

For whatever reason this happens, I will add a corresponding test case.

This comment has been minimized.

@tyralla
Copy link
Collaborator Author

tyralla commented Aug 10, 2025

Ah, I got it. It is because optuna enables --strict-equality but not unreachable. y is not None is always true, so that the y != "None" mismatch is not reported.

I could invest some time so that

for y in [1.0]:
    if y is not None or y != "None":
        ...

and

y = 1.0
if y is not None or y != "None":
    ...

would be handled identically again (no --strict-equality warnings). However, this is a good example that failing to raise warnings in unreachable code can be confusing, and there is already a PR that tries to improve the situation (#18707). Hence, maybe no further action is required here.

@tyralla tyralla requested review from JukkaL and A5rocks August 10, 2025 21:18
Copy link
Contributor

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

core (https://github.com/home-assistant/core)
+ homeassistant/components/homekit/__init__.py:982: error: Unused "type: ignore" comment  [unused-ignore]

urllib3 (https://github.com/urllib3/urllib3)
+ src/urllib3/connectionpool.py:1056: error: Unused "type: ignore" comment  [unused-ignore]

werkzeug (https://github.com/pallets/werkzeug)
+ src/werkzeug/datastructures/accept.py:259: error: Unused "type: ignore" comment  [unused-ignore]
+ src/werkzeug/datastructures/accept.py:264: error: Unused "type: ignore" comment  [unused-ignore]

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.

--strict-equality too strict in iteratively visited code
1 participant