python / python/mypy

Cannot call dict.get with a falsy second argument and an 'or' expression.

Open
#5,519 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug false-positive priority-1-normal
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

In mypy 0.610, as well as the latest version from master, the following throws an error under Python 3.6

def foo(a: str):
    pass


d = {'key': 'a'}
foo(d.get('key', None) or '26')

test.py:6: error: Argument 2 to "get" of "Mapping" has incompatible type "None"; expected "str"

However, no matter what, foo will still receive a str, given that None is falsy. Therefore, no error should be thrown. This occurs with just a simple call to mypy foo.py with no flags.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimal reproduction in test.py and run it with mypy foo.py under Python 3.6. Trace how the type checker handles Mapping.get when its result is used in the shown or expression. Done means the example no longer reports an incompatible None argument while preserving appropriate type checking for similar calls.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.