Skip to content

Type is not infered from default value of dict.get() #10890

Closed
@qi55wyqu

Description

@qi55wyqu

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-type-contextType context / bidirectional inference

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions