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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

(馃悶) false narrowing with reassignment in loop #11612

Open
KotlinIsland opened this issue Nov 25, 2021 · 3 comments
Open

(馃悶) false narrowing with reassignment in loop #11612

KotlinIsland opened this issue Nov 25, 2021 · 3 comments
Labels
bug mypy got something wrong topic-reachability Detecting unreachable code

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 Oct 31, 2023
KotlinIsland added a commit to KotlinIsland/basedmypy that referenced this issue Oct 31, 2023
KotlinIsland added a commit to KotlinIsland/basedmypy that referenced this issue Nov 1, 2023
KotlinIsland added a commit to KotlinIsland/basedmypy that referenced this issue Nov 4, 2023
KotlinIsland added a commit to KotlinIsland/basedmypy that referenced this issue Nov 10, 2023
KotlinIsland added a commit to KotlinIsland/basedmypy that referenced this issue Nov 10, 2023
KotlinIsland added a commit to KotlinIsland/basedmypy that referenced this issue Nov 11, 2023
KotlinIsland added a commit to KotlinIsland/basedmypy that referenced this issue Nov 12, 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 13, 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
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
Projects
None yet
Development

No branches or pull requests

3 participants