python / python/mypy

Cannot turn existing dictionary into TypedDict

Open
#8,890 4 comments 40 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I expected any of these forms to be allowed by MyPy, but they aren't:

# typed_dict_test.py
from typing import TypedDict

class D(TypedDict):
    a: str
    b: int


d_untyped = {"a": "foo", "b": 1}

d0 = D({"a": "foo", "b": 1})  # The only one that works
print(d0)

d1 = D(d_untyped)
print(d1)

d2 = D(**d_untyped)
print(d2)

d3 = D(dict(d_untyped))
print(d3)

d4 = D(dict(**d_untyped))
print(d4)
$ python typed_dict_test.py
{'a': 'foo', 'b': 1}
{'a': 'foo', 'b': 1}
{'a': 'foo', 'b': 1}
{'a': 'foo', 'b': 1}
{'a': 'foo', 'b': 1}
$ python -V
Python 3.8.3
$ mypy typed_dict_test.py 
typed_dict_test.py:13: error: Expected keyword arguments, {...}, or dict(...) in TypedDict constructor  [misc]
    d1 = D(d_untyped)
         ^
typed_dict_test.py:16: error: Expected keyword arguments, {...}, or dict(...) in TypedDict constructor  [misc]
    d2 = D(**d_untyped)
         ^
typed_dict_test.py:19: error: Expected keyword arguments, {...}, or dict(...) in TypedDict constructor  [misc]
    d3 = D(dict(d_untyped))
         ^
typed_dict_test.py:22: error: Expected keyword arguments, {...}, or dict(...) in TypedDict constructor  [misc]
    d4 = D(dict(**d_untyped))
         ^
Found 4 errors in 1 file (checked 1 source file)
$ cat mypy.ini 
[mypy]
warn_redundant_casts = True
warn_unused_configs = True
pretty = True
show_error_codes = True

disallow_any_generics = True
disallow_subclassing_any = True
disallow_untyped_calls = True
disallow_incomplete_defs = True
check_untyped_defs = True
disallow_untyped_decorators = True
no_implicit_optional = True
warn_unused_ignores = True
warn_return_any = True
no_implicit_reexport = True

I checked that cast(D, ...) works, but I wonder if this is the right way to proceed, or a workaround.

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 reproducing the diagnostic with the TypedDict example in the issue and inspect mypy's TypedDict constructor checking. Compare the accepted literal and dict(...) forms with the rejected existing-dictionary forms; done means the intended supported conversions are type-checked consistently with runtime behavior and covered by tests.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.