GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add support for TypedDict extra_items= and closed= (PEP 728) · Issue #18176 · python/mypy

Add support for TypedDict extra_items= and closed= (PEP 728) #18176

New issue
Copy link
New issue
Copy link
Open
Open
Add support for TypedDict extra_items= and closed= (PEP 728)#18176
Copy link
Labels
featurepriority-0-hightopic-typed-dict

Description

@mharding-hpe
mharding-hpe
opened on Nov 21, 2024
Issue body actions

Edit from maintainers: mypy supports closed TypedDicts since version 2.2


Feature

PEP 728 proposes changes to TypedDict, including the ability to specify the types of unspecified keys.

This has been implemented in typing_extensions with the __extra_items__ keyword since version 4.10.

I would like to see support for this added to mypy.

Pitch

I am working on using mypy to validate Python code associated with rest API services. It is not uncommon for the OpenAPI specs we use to include objects whose additionalProperties values map to schemas. This is how OpenAPI handles the case of objects whose keywords are not known in advance, but whose values must be of a particular type.

I want to be able to type annotate these schemas in Python, but currently there is no good mechanism for doing this. That is one of the problems that PEP 728 attempts to solve.

Currently with mypy I have found a few ways to handle this (in the absence of adding the requested support):

  1. As noted in the PEP, one can always suppress the typeddict-unknown-key errors. But of course that means no type checking is happening for the additional keys being used.
  2. I haven't coded it up yet, but I suspect with overloading I could probably come up with something that works for this. Basically, I could overload the getitem/setitem methods in a Protocol, specifying the correct return types for the various pre-defined keys (as Literals), and then add "generic" fallback get/set item methods for the "extra key type".
  3. It's possible I could come up with some Frankensteining of TypedDicts and defaultdicts that accomplishes this. Although that has the downside that the dict would always return a value regardless of the key, and that's not desirable.

For both 2 and 3, it would definitely be less clean and much lengthier than using the solution in typing_extensions.

From my research when trying to figure out how to handle this problem, I get the impression I am not the only one trying to handle this case. That is also indicated by the fact that the current JSON schema and OpenAPI spec includes this ability. And of course, that is part of the motivation for the PEP being opened in the first place.

Reacted by Mason Daugherty, Dylan Boudro, Brian Vander Schaaf, SN, Brace Sproul, Quentin Brosse, ccurme, Marco Perini, Josh Rogers, e-robin-leveque and 21 more

Activity

  1. mharding-hpe
    added
    feature
    on Nov 21, 2024
  2. brianschubert
    added
    topic-typed-dict
    on Nov 21, 2024
  3. layday commented on Nov 22, 2024

    @layday
    layday
    on Nov 22, 2024
    More actions

    The PEP has been updated and no longer specifies the __extra_items__ property. typing_extensions does not yet support the new parametrised form.

    Reacted by Mitch Harding
  4. mharding-hpe commented on Nov 22, 2024

    @mharding-hpe
    mharding-hpe
    on Nov 22, 2024
    Author
    More actions

    Good catch -- I missed that. In that case, I think it makes sense to wait for typing_extensions to catch up with the PEP before supporting it in mypy.

    I have opened a typing_extensions issue for this, as I do not see one currently open.

  5. mharding-hpe commented on Mar 21, 2025

    @mharding-hpe
    mharding-hpe
    on Mar 21, 2025
    Author
    More actions

    The typing_extensions work just got completed for this

  6. JukkaL
    added
    priority-0-high
    on Jun 5, 2025
  7. JukkaL commented on Jun 5, 2025

    @JukkaL
    JukkaL
    on Jun 5, 2025
    Collaborator
    More actions

    I think all the prerequisites are ready to start work on the implementation -- we just need somebody who has time and interest to work on this.

    Reacted by Shahriar Heidrich and Aleksandr Vishniakov
    Reacted by Brian Schubert, Mason Daugherty, Ivan Smirnov, iutlu, Edgar Ramírez Mondragón, Victor Kolegov, Kar Petrosyan and Morwenn
  8. JukkaL
    changed the title [-]Add support for TypedDict __extra_items__ (PEP 728)[/-] [+]Add support for TypedDict extra_items= and closed= (PEP 728)[/+] on Jun 5, 2025
  9. JelleZijlstra
    marked Subclassing TypedDict with closed=True causes error: Unexpected keyword argument "closed" for "__init_subclass__" of "TypedDict" [call-arg] typing_extensions#686 as a duplicate of this issue on Oct 10, 2025
  10. peacefulotter commented on Jan 7, 2026

    @peacefulotter
    peacefulotter
    on Jan 7, 2026
    More actions

    Any news on this? Thanks !

    Reacted by e-robin-leveque, Leonardo Santiago, Grzegorz Pociejewski, Aleksandr Vishniakov, Sławomir Ehlert and Billy Daly
    Reacted by e-robin-leveque, Leonardo Santiago, Allison Karlitskaya, Mitch Harding and Sławomir Ehlert
  11. ilevkivskyi
    added a commit that references this issue on Jun 2, 2026

    Implement support for closed TypedDicts (PEP 728) (#21382)

    0ce09a1
  12. heffernane
    added a commit that references this issue on Jul 15, 2026

    Ignore mypy errors for TypedDict extra_items

    954421d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurepriority-0-hightopic-typed-dict

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.