Skip to content

Can't call get on an empty dict #17684

Open

Description

@0e4ef622

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

Activity

  1. kstauffer commented on Aug 16, 2025

    @kstauffer
    Contributor

    The argument of dict[K,V].get is constrained: only arguments of type K are allowed. However {} is untyped, which is an error. Even something as simple as this is a problem:

    x = {} #  =>  error: Need type annotation for "x" (hint: "x: dict[<type>, <type>] = ...")  [var-annotated]
    

    Note that x = dict[str,str]().get("x") works fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions