Skip to content

pyrefly disagrees with mypy & pyright on type of captured variable in nested function #768

@jepler

Description

@jepler

Describe the Bug

This is another false positive adapted and minimized from my project https://github.com/jepler/wwvbpy which type checks without errors under mypy --strict (and a WIP version also type checks without errors in pyright)

Broadly, and with my guessing hat on, the problem seems to be that the inferred type of the captured table_start has not been narrowed by assert table_start is not None in the outer scope.

from typing import TYPE_CHECKING

def f(rows: list[int]) -> int:
    table_start: int|None = None
    for r in rows:
        if table_start is None:
            table_start = r

    assert table_start is not None

    def inner() -> int:
        return table_start - 1

    return inner()

print(f([2024]))

Sandbox Link

https://pyrefly.org/sandbox/?code=GYJw9gtgBALgngBwJYDsDmUkQWEMoAqAmgAoCiA+gMIASZVA0gJIByA4gFAcAmApsFGAAKcAHcAzgC4oAGyTiYAbVQwAugEooAWgB8mFDEkcoJ2AEMARjN4UFZvNJUAfFmBS8oAXiiv3x08C4UCD6wWASRqZRmAIwlta2cXiY4j5uvJHRWXFWNnbJ3iBcUWbi4rzJOQn5+PJQKGD4vrzFpnwCqO4gQpq6+ob+WSC8MACuICjmuYn2+FpQAIytJsNjE-pdPVwIICpCwooATAAMhwAsGupAA

(Only applicable for extension issues) IDE Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions