Skip to content

Wrong type resolved in a loop where the variables get moved around #714

@kilroy42

Description

@kilroy42

Describe the Bug

Minimal example, reduced from our application:

lineStart: int | None = None
lineno: int = 0

def needsInt(i: int):
    ...

for part in ['a', 'b', 'c', 'd']:
    if part == 'a':
        ...     # removing this "fixes" the error
    elif part == 'b':
        lineno = lineStart if lineStart is not None else 0      # same happens with "lineno = lineStart or 0"
    elif part == 'c':
        needsInt(lineno)        # somehow lineno is detected as "int | None" <-- this is the bug in line 13
    elif part == 'd':
        lineStart = lineno      # this seems to trigger the bug

Error:

13: ERROR Argument `int | None` is not assignable to parameter `i` with type `int` in function `needsInt` [bad-argument-type]

Tested against current version 3d28b54 (2025-07-20).

Removing the part 'a' lines fixes the bug. Removing the part 'd' also fixes the bug. No idea what is happening here.

Sandbox Link

https://pyrefly.org/sandbox/?code=DYSwdgpgygLghgJxgLgAThqgPqgcge0lQF49CIAoUSMfNDE1ABgooBMIAzVSCNgZwCSYGAAoQ9EQEpkFVPNQA6Za074EqAA6JM4VAG0A5HEMAaVIYBGZiwGMbhtoYC6shem7akJUscNv3BWVFQIBiVAQIAFt8ADdwAHNUGAALEH5UACJOEAAPCH5M5JSIVAgEBHU5BQhQTx0fC2sAwNRqCFpGdtgGkG5u+G90nnxMAiJa-lKmVvD+OCjSlLhNTQ6MgHcQVKz2ztIBhvVmTOr5Wr6tBuJfexbA3gFhMT38KVb5OfxFlPwNtvAHXw6AyHBgEFs4LYqDgGUyDBw4wgp3cF3q3huFic93chwxAJowLCxWGU2iGRgwJgCBACQS5WKpUsAFcEhQgA

(Only applicable for extension issues) IDE Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    scoping-control-flowissues related to scoping and control flow

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions