python / python/mypy

Way to support attributes for TypedDict

Open
#15,598 0 comments 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

Currently i am working with a typeddict that is transforming to AttributeDict and i dont have a way to preserve typings. I am implementing a custom plugin for this but the type is only available for get_type_analyze_hook.

To Reproduce

from typing import TypedDict

Test = TypedDict(
    "Test", 
    {
        "from": str,
        "test": str
    }
)

my_test: Test = {
    "from": "lorem",
    "test": "11"
}

my_test.test = "22"

Expected Behavior

There wont be any errors.

Actual Behavior

test.py:17: error: "Test" has no attribute "test"  [attr-defined]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.0.1
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.10.9

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 provided TypedDict example with mypy 1.0.1 and reading the get_type_analyze_hook path used by a custom plugin. Done means defining how TypedDict keys transformed into attributes should preserve their types and making the example pass without the attr-defined error.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers, devtools
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.