python / python/mypy

Incorrect precision calculation for TypedDict classes.

Open
#12,512 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Report

When defining a TypedDict class as in the example below, the lines defining the members of the dict are considered to be imprecise by the reports.

To Reproduce

# Code with imprecise lines
from typing import TypedDict

class UserDict(TypedDict):
  user_id: int  # Marked imprecise
  email: str  # Marked imprecise

user:UserDict = { 'user_id': 1, 'email': 'you@somewhere.com' }

The functionally equivalent alternative way shows all lines as precise:

# All lines are marked precise
from typing import TypedDict

UserDict = TypedDict('UserDict', {'user_id': int, 'email': str})

user:UserDict = { 'user_id': 1, 'email': 'you@somewhere.com' }

Expected Behavior

The member definitions in a TypedDict class should be marked precise.

Actual Behavior

Only 5 / 7 lines of code are considered precise with the user_id: int and email: str lines being marked imprecise. In the HTML report those lines have the title Any Types on this line: Error (x1).

Your Environment

  • Python 3.10.3
  • mypy 0.942
  • Clean venv, run with `mypy --html-report report --txt-report . .``
  • Ubuntu 20.04.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

Start by running the reported mypy command on the TypedDict reproduction and compare the HTML and text reports for class syntax versus the functional form. Done means the user_id and email member lines are marked precise rather than reporting Any types.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.