Skip to content

TypedDict keys reuse? #4128

@jstasiak

Description

@jstasiak

I may be in a need of the following - two TypeDicts with the same keys, one of them created with total=False:

from mypy_extensions import TypedDict

Details = TypedDict('Details', {'name': str, 'age': int, 'address': str})
DetailsSubset = TypedDict('DetailsSubset', {'name': str, 'age': int, 'address': str}, total=False)

Is there a way to reuse the keys instead of having to repeat the {'name': str, 'age': int, address: str} fragment twice?

I can't do

from mypy_extensions import TypedDict

keys = {'name': str, 'age': int, 'address': str}
Details = TypedDict('Details', keys)
DetailsSubset = TypedDict('DetailsSubset', keys, total=False)

Because

% mypy code.py
code.py:4: error: TypedDict() expects a dictionary literal as the second argument
code.py:5: error: TypedDict() expects a dictionary literal as the second argument

(which is quite reasonable, tried that on the off chance it'd work)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions