Skip to content
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

(🐞) false narrowing with reassignment in loop #11612

Closed
KotlinIsland opened this issue Nov 25, 2021 · 3 comments · Fixed by #18433
Closed

(🐞) false narrowing with reassignment in loop #11612

KotlinIsland opened this issue Nov 25, 2021 · 3 comments · Fixed by #18433
Labels
bug mypy got something wrong topic-reachability Detecting unreachable code topic-type-narrowing Conditional type narrowing / binder

Comments

@KotlinIsland
Copy link
Contributor

KotlinIsland commented Nov 25, 2021

a: int | None
a = None
for i in range(10):
    reveal_type(a)  # "None" and also "int | None"
    if a:
        print(a)  # error: unreachable
    a = i

Also:

a = None
for b in [1, 2]:
    if a is None:
        a = b
        continue
    print("hi")  # false unreachable error
@KotlinIsland KotlinIsland added the bug mypy got something wrong label Nov 25, 2021
@sobolevn
Copy link
Member

sobolevn commented Dec 6, 2021

This can be fixed by:

a: int | None = None
for b in [1, 2]:
    if a is None:
        a = b
        continue
    print("hi")  # ok

The other question is: why a is not infered to be None | int?

@KotlinIsland
Copy link
Contributor Author

KotlinIsland commented Dec 6, 2021

It is, if you put reveal_type after the loop it reveals as int | None

Perhaps it is inferring as int | None, but only past line 4, not considering the loop.

@AlexWaygood AlexWaygood added the topic-reachability Detecting unreachable code label Mar 30, 2022
@KotlinIsland KotlinIsland changed the title false unreachable with conditional reassignment then continue in loop (🐞) false unreachable with conditional reassignment then continue in loop Oct 31, 2023
@KotlinIsland KotlinIsland changed the title (🐞) false unreachable with conditional reassignment then continue in loop (🐞) false unreachable with reassignment in loop Oct 31, 2023
@KotlinIsland
Copy link
Contributor Author

@sobolevn It's not just the inference, I've updated the OP with a much better example.

@KotlinIsland KotlinIsland changed the title (🐞) false unreachable with reassignment in loop (🐞) false narrowing with reassignment in loop Oct 31, 2023
KotlinIsland added a commit to KotlinIsland/basedmypy that referenced this issue Nov 13, 2023
KotlinIsland added a commit to KotlinIsland/basedmypy that referenced this issue Nov 14, 2023
@hauntsaninja hauntsaninja added the topic-type-narrowing Conditional type narrowing / binder label Nov 18, 2024
tyralla added a commit to tyralla/mypy that referenced this issue Jan 9, 2025
Fixes python#18348
Fixes python#13973
Fixes python#11612
Fixes python#8721
Fixes python#8865
Fixes python#7204

I manually checked all the listed issues.  Some of them were already partly fixed by python#18180.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-reachability Detecting unreachable code topic-type-narrowing Conditional type narrowing / binder
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants