Skip to content

Nested Function incorrect type inference #4399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
alanhdu opened this issue Dec 21, 2017 · 2 comments
Closed

Nested Function incorrect type inference #4399

alanhdu opened this issue Dec 21, 2017 · 2 comments

Comments

@alanhdu
Copy link
Contributor

alanhdu commented Dec 21, 2017

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.

@alanhdu alanhdu changed the title Nested Functions Nested Function incorrect type inference Dec 21, 2017
@emmatyping
Copy link
Member

It seems type narrowing is not being propagated to closures, but only in function defs (I checked the above with a lamba and the narrowing is propagated). The narrowing stays after the function definition, just not within.

@ilevkivskyi
Copy link
Member

This is a duplicate of #2608

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants