Skip to content

Nested Function incorrect type inference #4399

Closed
@alanhdu

Description

@alanhdu

Running mypy --strict-optional on the following code:

from typing import Optional, Sequence

def f(timestamps: Optional[Sequence[float]] = None):
    if timestamps is None:
        timestamps = [0]

    reveal_type(timestamps)
    def index(i: int) -> float:
        return timestamps[i]
    return index(0)

fails with

test.py:7: error: Revealed type is 'typing.Sequence[builtins.float]'
test.py:9: error: Value of type "Optional[Sequence[float]]" is not indexable

This is pretty weird -- MyPy knows that timestamps isn't None at that point, but complains in the nested function.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions