mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Narrowing str | dict[str, str] with isinstance and .get()

Open Akuli opened this issue 3 years ago • 0 comments

Bug Report

To Reproduce

def first() -> None:
    tag_name: str | dict[str, str]
    if isinstance(tag_name, dict):
        tag_name = tag_name.get("foo", "bar")
    reveal_type(tag_name)

def second() -> None:
    tag_name: str | dict[str, str]
    if isinstance(tag_name, dict):
        tag_name = tag_name["foo"]
    reveal_type(tag_name)

Expected Behavior

str str

Actual Behavior

Union[str, dict[str, str]] str

Your Environment

latest mypy

Akuli avatar Aug 07 '22 16:08 Akuli