mypy
mypy copied to clipboard
Narrowing str | dict[str, str] with isinstance and .get()
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