python / python/mypy

More specific inline dict type inference can fail on 1.17.0

Open
#19,474 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Report

More specific inline dict type inference can fail on 1.17.0.

To Reproduce

The following fails on 1.17.0 but is fine on 1.16.1 (the playground link uses master as 1.17.0 isn't available there):
https://mypy-play.net/?mypy=master&python=3.13&gist=7e77b89fe5f9cb7e548c4ef83b3783c1

Revealing the type for the inline dict gives:

  • 1.16.1: builtins.dict[builtins.str, Union[typing.Collection[builtins.str], None]]
  • 1.17.0: builtins.dict[builtins.str, builtins.dict[builtins.str, builtins.list[Any] | None] | builtins.dict[builtins.str, builtins.str] | builtins.str | None]

Expected Behavior

No mypy errors.

Actual Behavior

main.py:11: error: Dict entry 0 has incompatible type "str": "None"; expected "str": "str"  [dict-item]
main.py:12: error: Dict entry 1 has incompatible type "str": "list[Never]"; expected "str": "str"  [dict-item]
Found 2 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.17.0
  • Mypy command-line flags: N/A
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.13.5

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 linked mypy playground reproduction and compare its behavior on 1.16.1, 1.17.0, and master. The issue names no repository file or test, so trace the inline dictionary type-inference path from the reproduced errors; done means the example produces no errors while preserving the expected inferred type.

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
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.