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

Add support for PEP 728 – TypedDict with Typed Extra Items #271

Open
tokarenko opened this issue Dec 20, 2024 · 1 comment
Open

Add support for PEP 728 – TypedDict with Typed Extra Items #271

tokarenko opened this issue Dec 20, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@tokarenko
Copy link

Is your feature request related to a problem? Please describe.
I can't find a way to get "additionalProperties": true in JSON schema for a TypedDict.

Describe the solution you'd like
I suggest the following:

  1. Add support for PEP 728 closed class parameter as a way to enable "additionalProperties": true in JSON schema for TypedDict.
  2. Set "additionalProperties": true by default for TypedDict as the PEP states:

Passing closed=False explicitly requests the default TypedDict behavior, where arbitrary other keys may be present ...

Describe alternatives you've considered
None

Additional context

from typing_extensions import TypedDict, NotRequired

class SoftwareVersion(TypedDict, closed=False):
    NAME: str
    VERSION: str
    BUILD: NotRequired[str]

>>> print(build_json_schema(SoftwareVersion).to_json())
{"type": "object", "properties": {"NAME": {"type": "string"}, "VERSION": {"type": "string"}, "BUILD": {"type": "string"}}, "additionalProperties": false, "required": ["NAME", "VERSION"]}
@Fatal1ty Fatal1ty added the enhancement New feature or request label Dec 20, 2024
@Fatal1ty
Copy link
Owner

Fatal1ty commented Dec 20, 2024

It would be a great addition to the library. However, I will make a suggestion - in addition to true/false, we can use the additional value type, because this is exactly what PEP 728 suggests with extra_items and matches the JSON Schema specification.

Would you be willing to make a PR for that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants