Skip to content

Inference on Union types is not propagated to local function closures. #12790

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
DerBiasto opened this issue May 14, 2022 · 2 comments
Closed

Comments

@DerBiasto
Copy link

from typing import Union

def foo(key: str, val: dict[str, int] = None) -> int:
    if val is None:
        return 0
    else:
        def bar() -> int:
            return val[key]  # error: Value of type "Optional[Dict[str, int]]" is not indexable  [index]
        return bar()


def foo(val: Union[list[int], int]) -> int:
    if isinstance(val, list):
        def bar() -> int:
            return val[0]  # error: Value of type "Union[List[int], int]" is not indexable  [index]
        return bar()


val: Union[list[int], int] = [1]
assert isinstance(val, list)

def foo() -> int:
    return val[0]  # error: Value of type "Union[List[int], int]" is not indexable  [index]

Type information established in the local scope in which a function is defined is not propagated to the closure of the function.

@erictraut
Copy link

This appears to be a duplicate of #2608.

@AlexWaygood
Copy link
Member

This appears to be a duplicate of #2608.

Yup.

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