Gowee / json2pyi

Generate Python type definitions from a JSON sample (both Pydantic BaseModel and TypedDict are supported)
https://json2pyi.pages.dev
46 stars 1 forks source link

Use `NotRequired[]` instead of `Optional[]` when a key can be missing in a `TypedDict` #7

Closed MaT-PT closed 1 year ago

MaT-PT commented 1 year ago

Currently, when a dictionary has a key that can be missing, json2pyi produces a TypedDict with the key marked as Optional[].

For example, the following JSON:

[
    {"id": 1, "value": "foo", "comment": "bar"},
    {"id": 2, "value": null}
]

Produces this code:

from __future__ import annotations

from typing import TypedDict, List, Optional

class UnnammedType160B90(TypedDict):
    id: int
    value: Optional[str]
    comment: Optional[str]

This is a problem, as Optional[] means the value is required, but can be None (null in JSON). This is different from not being required. In the previous example, the comment field is either missing, or is a str, but it cannot be null.

Python 3.11 introduced the NotRequired[] type qualifier to mark such potentially-missing keys (cf. PEP 655).

The code produced by json2pyi should then be:

from __future__ import annotations

from typing import TypedDict, List, Optional, NotRequired

class UnnammedType160B90(TypedDict):
    id: int
    value: Optional[str]
    comment: NotRequired[str]

Which correctly marks value as nullable, and comment as potentially-missing.

Gowee commented 1 year ago

Great thanks for bringing that up! Updated and deployed.