|
10 | 10 | | [`--enum-field-as-literal`](#enum-field-as-literal) | Convert all enum fields to Literal types instead of Enum cla... | |
11 | 11 | | [`--enum-field-as-literal-map`](#enum-field-as-literal-map) | Override enum/literal generation per-field via JSON mapping.... | |
12 | 12 | | [`--ignore-enum-constraints`](#ignore-enum-constraints) | Ignore enum constraints and use base string type instead of ... | |
| 13 | +| [`--no-use-closed-typed-dict`](#no-use-closed-typed-dict) | Disable PEP 728 TypedDict closed/extra_items generation. | |
13 | 14 | | [`--no-use-specialized-enum`](#no-use-specialized-enum) | Disable specialized Enum classes for Python 3.11+ code gener... | |
14 | 15 | | [`--no-use-standard-collections`](#no-use-standard-collections) | Use typing.Dict/List instead of built-in dict/list for conta... | |
15 | 16 | | [`--no-use-union-operator`](#no-use-union-operator) | Use Union[X, Y] / Optional[X] instead of X | Y union operato... | |
|
19 | 20 | | [`--type-mappings`](#type-mappings) | Override default type mappings for schema formats. | |
20 | 21 | | [`--type-overrides`](#type-overrides) | Replace schema model types with custom Python types via JSON... | |
21 | 22 | | [`--use-annotated`](#use-annotated) | Use typing.Annotated for Field() with constraints. | |
| 23 | +| [`--use-closed-typed-dict`](#use-closed-typed-dict) | Generate TypedDict with PEP 728 closed/extra_items (default:... | |
22 | 24 | | [`--use-decimal-for-multiple-of`](#use-decimal-for-multiple-of) | Generate Decimal types for fields with multipleOf constraint... | |
23 | 25 | | [`--use-generic-container-types`](#use-generic-container-types) | Use generic container types (Sequence, Mapping) for type hin... | |
24 | 26 | | [`--use-non-positive-negative-number-constrained-types`](#use-non-positive-negative-number-constrained-types) | Use NonPositive/NonNegative types for number constraints. | |
@@ -1744,6 +1746,60 @@ defined enum members. |
1744 | 1746 |
|
1745 | 1747 | --- |
1746 | 1748 |
|
| 1749 | +## `--no-use-closed-typed-dict` {#no-use-closed-typed-dict} |
| 1750 | + |
| 1751 | +Disable PEP 728 TypedDict closed/extra_items generation. |
| 1752 | + |
| 1753 | +Use this option for compatibility with type checkers that don't yet support PEP 728 |
| 1754 | +(e.g., mypy). TypedDict will be generated without `closed=True` or `extra_items`. |
| 1755 | + |
| 1756 | +!!! tip "Usage" |
| 1757 | + |
| 1758 | + ```bash |
| 1759 | + datamodel-codegen --input schema.json --output-model-type typing.TypedDict --no-use-closed-typed-dict # (1)! |
| 1760 | + ``` |
| 1761 | + |
| 1762 | + 1. :material-arrow-left: `--no-use-closed-typed-dict` - the option documented here |
| 1763 | + |
| 1764 | +??? example "Examples" |
| 1765 | + |
| 1766 | + **Input Schema:** |
| 1767 | + |
| 1768 | + ```json |
| 1769 | + { |
| 1770 | + "$schema": "http://json-schema.org/draft-07/schema#", |
| 1771 | + "type": "object", |
| 1772 | + "title": "ClosedModel", |
| 1773 | + "properties": { |
| 1774 | + "name": {"type": "string"}, |
| 1775 | + "age": {"type": "integer"} |
| 1776 | + }, |
| 1777 | + "required": ["name"], |
| 1778 | + "additionalProperties": false |
| 1779 | + } |
| 1780 | + ``` |
| 1781 | + |
| 1782 | + **Output:** |
| 1783 | + |
| 1784 | + ```python |
| 1785 | + # generated by datamodel-codegen: |
| 1786 | + # filename: typed_dict_closed.json |
| 1787 | + # timestamp: 2019-07-26T00:00:00+00:00 |
| 1788 | + |
| 1789 | + from __future__ import annotations |
| 1790 | + |
| 1791 | + from typing import TypedDict |
| 1792 | + |
| 1793 | + from typing_extensions import NotRequired |
| 1794 | + |
| 1795 | + |
| 1796 | + class ClosedModel(TypedDict): |
| 1797 | + name: str |
| 1798 | + age: NotRequired[int] |
| 1799 | + ``` |
| 1800 | + |
| 1801 | +--- |
| 1802 | + |
1747 | 1803 | ## `--no-use-specialized-enum` {#no-use-specialized-enum} |
1748 | 1804 |
|
1749 | 1805 | Disable specialized Enum classes for Python 3.11+ code generation. |
@@ -3217,6 +3273,58 @@ syntax instead of default values. This also enables `--field-constraints`. |
3217 | 3273 |
|
3218 | 3274 | --- |
3219 | 3275 |
|
| 3276 | +## `--use-closed-typed-dict` {#use-closed-typed-dict} |
| 3277 | + |
| 3278 | +Generate TypedDict with PEP 728 closed/extra_items (default: enabled). |
| 3279 | + |
| 3280 | +When enabled (default), generates TypedDict with PEP 728 `closed=True` or `extra_items` |
| 3281 | +parameters based on `additionalProperties` constraints in the schema. |
| 3282 | + |
| 3283 | +!!! tip "Usage" |
| 3284 | + |
| 3285 | + ```bash |
| 3286 | + datamodel-codegen --input schema.json --output-model-type typing.TypedDict --use-closed-typed-dict # (1)! |
| 3287 | + ``` |
| 3288 | + |
| 3289 | + 1. :material-arrow-left: `--use-closed-typed-dict` - the option documented here |
| 3290 | + |
| 3291 | +??? example "Examples" |
| 3292 | + |
| 3293 | + **Input Schema:** |
| 3294 | + |
| 3295 | + ```json |
| 3296 | + { |
| 3297 | + "$schema": "http://json-schema.org/draft-07/schema#", |
| 3298 | + "type": "object", |
| 3299 | + "title": "ClosedModel", |
| 3300 | + "properties": { |
| 3301 | + "name": {"type": "string"}, |
| 3302 | + "age": {"type": "integer"} |
| 3303 | + }, |
| 3304 | + "required": ["name"], |
| 3305 | + "additionalProperties": false |
| 3306 | + } |
| 3307 | + ``` |
| 3308 | + |
| 3309 | + **Output:** |
| 3310 | + |
| 3311 | + ```python |
| 3312 | + # generated by datamodel-codegen: |
| 3313 | + # filename: typed_dict_closed.json |
| 3314 | + # timestamp: 2019-07-26T00:00:00+00:00 |
| 3315 | + |
| 3316 | + from __future__ import annotations |
| 3317 | + |
| 3318 | + from typing_extensions import NotRequired, TypedDict |
| 3319 | + |
| 3320 | + |
| 3321 | + class ClosedModel(TypedDict, closed=True): |
| 3322 | + name: str |
| 3323 | + age: NotRequired[int] |
| 3324 | + ``` |
| 3325 | + |
| 3326 | +--- |
| 3327 | + |
3220 | 3328 | ## `--use-decimal-for-multiple-of` {#use-decimal-for-multiple-of} |
3221 | 3329 |
|
3222 | 3330 | Generate Decimal types for fields with multipleOf constraint. |
|
0 commit comments