Skip to content

For loop assumed to execute at least once based on inferred type #18629

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

Closed
JukkaL opened this issue Feb 7, 2025 · 1 comment · Fixed by #18631
Closed

For loop assumed to execute at least once based on inferred type #18629

JukkaL opened this issue Feb 7, 2025 · 1 comment · Fixed by #18631
Assignees
Labels
bug mypy got something wrong topic-type-narrowing Conditional type narrowing / binder

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Feb 7, 2025

Bug Report

Mypy seems to assume that for loop body executes at least once when inferring index variable types.

To Reproduce

x: int | str
x = "abc"
for x in list[int]():
    pass
reveal_type(x) # int

Expected Behavior

Revealed type is int | str.

Actual Behavior

Revealed type is int.

Your Environment

Latest master.

@JukkaL JukkaL added bug mypy got something wrong topic-type-narrowing Conditional type narrowing / binder labels Feb 7, 2025
@JukkaL JukkaL self-assigned this Feb 7, 2025
@Hnasar
Copy link
Contributor

Hnasar commented Feb 7, 2025

Nice! thanks for the fix — this will also close #16321

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-type-narrowing Conditional type narrowing / binder
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants