Skip to content

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

Closed
@JukkaL

Description

@JukkaL

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.

Metadata

Metadata

Assignees

Labels

bugmypy got something wrongtopic-type-narrowingConditional type narrowing / binder

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions