Skip to content

Narrowing: Variable type gets changed in a loop where it is only read from #773

@kilroy42

Description

@kilroy42

Describe the Bug

def mySplit(s: str) -> None:
    inQuote: bool | str = False

    res: list[str] = []
    for c in s:
        if c == inQuote:
            ...
        elif inQuote:
            inQuote = c
            res[-1] += c
ERROR 10:13-25: `+=` is not supported between `str` and `bool` [[unsupported-operation](https://pyrefly.org/en/docs/error-kinds/#unsupported-operation)]

Although c is pretty clearly a "str", and never modified except in the for c in s, the type of inQuote is copied into the type of c (bool | str).

I have reported similiar bugs, but they came with another exception, had a slightly different reason and this one is hopefully easier to debug, so I reported this, too.

Sandbox Link

https://pyrefly.org/sandbox/?code=CYUwZgBAtgngygBwDYEsAuAKAzgLgltAJwEoIBaAPggDkB7AOxBwCgI2IV6BFAV1rSYQARrVpIIAH3xEIAXggAxAIZIsIZq3aEQuCKgIBtAoQC6ciAZOa2YWoQgBjDvXwt27jpCez5nXvyZrD3cAOjCg9xBUSD8+ATdgj1iA8wcIxO0sAzIARjMAank0oA

(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

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions