Skip to content

Inner function does not preserve narrowed type #1114

@mflova

Description

@mflova

Summary

Mypy and pyright apparently do not have this problem. In my example below, obj from inner_func is guaranteed to be a type string. This can also be seen in the first reveal_type. However, it seems that ty is taking the type of obj to be the input argument one (int | str) instead of the narrowed down (str)

from collections.abc import Mapping
from typing import Any
from typing_extensions import reveal_type


def func(obj: int | str) -> None:
    obj = str(obj) if isinstance(obj, int) else obj
    reveal_type(obj)  #  str

    def inner_func():
        reveal_type(obj)  # int | str

Version

0.0.1-alpha.19

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions