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.
Currently, when a dictionary has a key that can be missing, json2pyi produces a
TypedDict
with the key marked asOptional[]
.For example, the following JSON:
Produces this code:
This is a problem, as
Optional[]
means the value is required, but can beNone
(null
in JSON). This is different from not being required. In the previous example, thecomment
field is either missing, or is astr
, but it cannot benull
.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:
Which correctly marks
value
as nullable, andcomment
as potentially-missing.