-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Closed
Labels
bugmypy got something wrongmypy got something wrong
Description
Bug Report
Mypy wrongly infers the type of the expression x.get("a") or x.get("b", "c") to be str | None, even when x is explicitly a dict[str, str].
To Reproduce
def works(x: dict[str, str]) -> str:
a = x.get("a")
b = x.get("b", "c")
return a or b
def also_works(x: dict[str, str]) -> str: return x.get("a", x.get("b", "c"))
def also_fine(x: dict[str, str]) -> str: return x.get("a") or x.get("b") or "c"
def breaks(x: dict[str, str]) -> str: return x.get("a") or x.get("b", "c")https://mypy-play.net/?mypy=latest&python=3.11&gist=33335d3d1d19e5858beb76ed65ce8470
I realise the examples are not logically equivalent, but they should all evaluate to strings
Expected Behavior
No errors.
Actual Behavior
Mypy returns main.py:8: error: Incompatible return value type (got "str | None", expected "str") [return-value]
Your Environment
- Mypy version used: 1.4.0
- Mypy command-line flags: N/A
- Mypy configuration options from
mypy.ini(and other config files): N/A - Python version used: 3.11
Metadata
Metadata
Assignees
Labels
bugmypy got something wrongmypy got something wrong