Closed
Description
Bug Report
When using dict.get(value, default)
the returned type is not inferred correctly if the value was initially declared as Optional
.
To Reproduce
mapping: Dict[str, str] = {"a": "x"}
val: Optional[str] = None
val = "a"
reveal_type(val) # Revealed type is "builtins.str"
val = mapping[val] if val in mapping else val
reveal_type(val) # Revealed type is "builtins.str"
val = mapping.get(val, val)
reveal_type(val) # Revealed type is "Union[builtins.str, None]"
Expected Behavior
Revealed type is builtins.str
.
Actual Behavior
Revealed type is Union[builtins.str, None]
.
Your Environment
- Mypy version used: mypy 0.910
- Mypy command-line flags: --
- Mypy configuration options from
mypy.ini
(and other config files): -- - Python version used: 3.8.10
- Operating system and version: Ubuntu 20.04.2