mashumaro icon indicating copy to clipboard operation
mashumaro copied to clipboard

Add support for PEP 728 – TypedDict with Typed Extra Items

Open tokarenko opened this issue 1 year ago • 1 comments

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"]}

tokarenko avatar Dec 20 '24 12:12 tokarenko

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?

Fatal1ty avatar Dec 20 '24 15:12 Fatal1ty