Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for generic TypedDict #3863

Closed
dgoldstein0 opened this issue Aug 22, 2017 · 11 comments · Fixed by #13389
Closed

Support for generic TypedDict #3863

dgoldstein0 opened this issue Aug 22, 2017 · 11 comments · Fixed by #13389

Comments

@dgoldstein0
Copy link

R = TypeVar("R")
FileCachedData = TypedDict('FileCachedData', {'mtime': float, 'size': int, 'value': R})

mypy complains about R in the TypedDict declaration. I would expect that I could declare a generic TypedDict like this.

@gvanrossum
Copy link
Member

Generic TypedDicts are not supported yet. (Though if there were, this would be a reasonable notations -- after all this is how you declare generic aliases.)

@gvanrossum gvanrossum changed the title [mypy_extensions] TypedDict dislikes nested TypeVars [mypy_extensions] Support for generic TypedDict Aug 23, 2017
@dgoldstein0
Copy link
Author

dgoldstein0 commented Aug 23, 2017 via email

@gwax
Copy link

gwax commented Jan 27, 2021

To add a justification note, this feature would be useful for handling polymorphic JSON responses, such as a shared pager:

class Item(TypedDict):
    type: str

class ValueItem(Item):
    value: int

T = TypeVar('T', bound=Item)

class ItemPager(TypedDict, Generic[T]):
    next_page: Optional[str]
    items: List[T]

def get_value_items() -> List[ValueItem]:
    value_items: List[ValueItem] = []
    url = "https://example.com/value_items"
    while url:
        response = requests.get(url)
        pager: ItemPager[ValueItem] = response.json()
        url = pager['next_page']
        value_items += pager['items']
    return value_items

@pawelrubin
Copy link

Hi!
Are Generic TypedDicts/NamedTuples planned to be supported in the future versions of Python/mypy?
As @gwax mentioned, they would be super useful for annotating polymorphic JSON responses.

@gvanrossum
Copy link
Member

gvanrossum commented Mar 26, 2021 via email

@peterschutt
Copy link

@pawelrubin was a bpo issue created for this?

@AlexWaygood
Copy link
Member

There's now a BPO issue for this here

@graingert
Copy link
Contributor

See also python/typing_extensions#7

@AlexWaygood
Copy link
Member

AlexWaygood commented May 3, 2022

See also python/typing_extensions#7

To expand on this: generic TypedDicts are now supported by the runtime in Python 3.11, and this new functionality will hopefully soon be backported to earlier Python versions via typing_extensions. mypy support for generic TypedDicts is still missing, however.

@JelleZijlstra JelleZijlstra changed the title [mypy_extensions] Support for generic TypedDict Support for generic TypedDict May 3, 2022
@JelleZijlstra
Copy link
Member

We likely won't add this support to mypy-extensions. Retitling this to reflect the request that mypy adds support for generic TypedDicts in general.

@AlexWaygood
Copy link
Member

@JelleZijlstra, could you add a link to this issue in #11753?

ilevkivskyi added a commit that referenced this issue Aug 15, 2022
Fixes #3863

This builds on top of some infra I added for recursive types (Ref #13297). Implementation is quite straightforward. The only non-trivial thing is that when extending/merging TypedDicts, the item types need to me mapped to supertype during semantic analysis. This means we can't call `is_subtype()` etc., and can in theory get types like `Union[int, int]`. But OTOH this equally applies to type aliases, and doesn't seem to cause problems.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants