python / python/mypy

Can't call get on an empty dict

Open
#17,684 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

Mypy complains when I try to call get on an empty dict.

To Reproduce

x = {}.get("x")

Expected Behavior

No error.

Actual Behavior

a.py:1: error: No overload variant of "get" of "dict" matches argument type "str"  [call-overload]
a.py:1: note: Possible overload variants:
a.py:1: note:     def get(self, Never, /) -> None
a.py:1: note:     def get(self, Never, Never, /) -> Never
a.py:1: note:     def [_T] get(self, Never, _T, /) -> _T
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.11.1
  • Python version used: 3.12.4

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

Reproduce the report using the a.py snippet and the stated Mypy and Python versions. Trace how the empty dictionary expression is inferred and how dict.get overloads are selected; done means the example produces no error and has a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
43/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.