mypy icon indicating copy to clipboard operation
mypy copied to clipboard

Combining an `or` and `dict.get` results in the wrong inferred type.

Open ojii opened this issue 3 years ago • 1 comments

Bug Report

Combining an or and dict.get results in the wrong inferred type.

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.11&gist=8ac5004cd4094219a0f090ebd0d34777

Expected Behavior

The or_dict method in the gist above should type check as valid.

Actual Behavior

mypy thinks that the return type should be str | None

Your Environment

  • Mypy version used: 0.991
  • Mypy command-line flags: tried with both --strict and without
  • Mypy configuration options from mypy.ini (and other config files): none, see mypy-play link
  • Python version used: 3.11

ojii avatar Dec 22 '22 02:12 ojii

return bar if bar else d.get("bar", "baz") correct return bar or d.get("bar", "baz") wrong

I wonder why the short circuit evaluation is treated differently

dimaqq avatar Dec 22 '22 06:12 dimaqq