Skip to content

Commit aa088d6

Browse files
Add --use-closed-typed-dict option to control PEP 728 TypedDict generation (#2956)
* Add --use-closed-typed-dict option to control PEP 728 TypedDict generation * docs: update llms.txt files Generated by GitHub Actions * docs: update CLI reference documentation and prompt data 🤖 Generated by GitHub Actions * Use assert helper function for test_main_typed_dict_no_closed * Split cli_doc markers for --use-closed-typed-dict and --no-use-closed-typed-dict * docs: update llms.txt files Generated by GitHub Actions * docs: update CLI reference documentation and prompt data 🤖 Generated by GitHub Actions --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 98f3a48 commit aa088d6

17 files changed

Lines changed: 312 additions & 2 deletions

File tree

docs/cli-reference/index.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ This documentation is auto-generated from test cases.
99
| Category | Options | Description |
1010
|----------|---------|-------------|
1111
| 📁 [Base Options](base-options.md) | 9 | Input/output configuration |
12-
| 🔧 [Typing Customization](typing-customization.md) | 27 | Type annotation and import behavior |
12+
| 🔧 [Typing Customization](typing-customization.md) | 29 | Type annotation and import behavior |
1313
| 🏷️ [Field Customization](field-customization.md) | 24 | Field naming and docstring behavior |
1414
| 🏗️ [Model Customization](model-customization.md) | 39 | Model generation behavior |
1515
| 🎨 [Template Customization](template-customization.md) | 19 | Output formatting and custom rendering |
@@ -133,6 +133,7 @@ This documentation is auto-generated from test cases.
133133
- [`--no-alias`](field-customization.md#no-alias)
134134
- [`--no-color`](utility-options.md#no-color)
135135
- [`--no-treat-dot-as-module`](template-customization.md#no-treat-dot-as-module)
136+
- [`--no-use-closed-typed-dict`](typing-customization.md#no-use-closed-typed-dict)
136137
- [`--no-use-specialized-enum`](typing-customization.md#no-use-specialized-enum)
137138
- [`--no-use-standard-collections`](typing-customization.md#no-use-standard-collections)
138139
- [`--no-use-union-operator`](typing-customization.md#no-use-union-operator)
@@ -186,6 +187,7 @@ This documentation is auto-generated from test cases.
186187
- [`--url`](base-options.md#url)
187188
- [`--use-annotated`](typing-customization.md#use-annotated)
188189
- [`--use-attribute-docstrings`](field-customization.md#use-attribute-docstrings)
190+
- [`--use-closed-typed-dict`](typing-customization.md#use-closed-typed-dict)
189191
- [`--use-decimal-for-multiple-of`](typing-customization.md#use-decimal-for-multiple-of)
190192
- [`--use-default`](model-customization.md#use-default)
191193
- [`--use-default-factory-for-optional-nested-models`](model-customization.md#use-default-factory-for-optional-nested-models)

docs/cli-reference/quick-reference.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ datamodel-codegen [OPTIONS]
3636
| [`--enum-field-as-literal`](typing-customization.md#enum-field-as-literal) | Convert all enum fields to Literal types instead of Enum classes. |
3737
| [`--enum-field-as-literal-map`](typing-customization.md#enum-field-as-literal-map) | Override enum/literal generation per-field via JSON mapping. |
3838
| [`--ignore-enum-constraints`](typing-customization.md#ignore-enum-constraints) | Ignore enum constraints and use base string type instead of Enum classes. |
39+
| [`--no-use-closed-typed-dict`](typing-customization.md#no-use-closed-typed-dict) | Disable PEP 728 TypedDict closed/extra_items generation. |
3940
| [`--no-use-specialized-enum`](typing-customization.md#no-use-specialized-enum) | Disable specialized Enum classes for Python 3.11+ code generation. |
4041
| [`--no-use-standard-collections`](typing-customization.md#no-use-standard-collections) | Use typing.Dict/List instead of built-in dict/list for container types. |
4142
| [`--no-use-union-operator`](typing-customization.md#no-use-union-operator) | Use Union[X, Y] / Optional[X] instead of X | Y union operator. |
@@ -45,6 +46,7 @@ datamodel-codegen [OPTIONS]
4546
| [`--type-mappings`](typing-customization.md#type-mappings) | Override default type mappings for schema formats. |
4647
| [`--type-overrides`](typing-customization.md#type-overrides) | Replace schema model types with custom Python types via JSON mapping. |
4748
| [`--use-annotated`](typing-customization.md#use-annotated) | Use typing.Annotated for Field() with constraints. |
49+
| [`--use-closed-typed-dict`](typing-customization.md#use-closed-typed-dict) | Generate TypedDict with PEP 728 closed/extra_items (default: enabled). |
4850
| [`--use-decimal-for-multiple-of`](typing-customization.md#use-decimal-for-multiple-of) | Generate Decimal types for fields with multipleOf constraint. |
4951
| [`--use-generic-container-types`](typing-customization.md#use-generic-container-types) | Use generic container types (Sequence, Mapping) for type hinting. |
5052
| [`--use-non-positive-negative-number-constrained-types`](typing-customization.md#use-non-positive-negative-number-constrained-types) | Use NonPositive/NonNegative types for number constraints. |
@@ -285,6 +287,7 @@ All options sorted alphabetically:
285287
- [`--no-alias`](field-customization.md#no-alias) - Disable Field alias generation for non-Python-safe property ...
286288
- [`--no-color`](utility-options.md#no-color) - Disable colorized output
287289
- [`--no-treat-dot-as-module`](template-customization.md#no-treat-dot-as-module) - Keep dots in schema names as underscores for flat output.
290+
- [`--no-use-closed-typed-dict`](typing-customization.md#no-use-closed-typed-dict) - Disable PEP 728 TypedDict closed/extra_items generation.
288291
- [`--no-use-specialized-enum`](typing-customization.md#no-use-specialized-enum) - Disable specialized Enum classes for Python 3.11+ code gener...
289292
- [`--no-use-standard-collections`](typing-customization.md#no-use-standard-collections) - Use typing.Dict/List instead of built-in dict/list for conta...
290293
- [`--no-use-union-operator`](typing-customization.md#no-use-union-operator) - Use Union[X, Y] / Optional[X] instead of X | Y union operato...
@@ -320,6 +323,7 @@ All options sorted alphabetically:
320323
- [`--url`](base-options.md#url) - Fetch schema from URL with custom HTTP headers.
321324
- [`--use-annotated`](typing-customization.md#use-annotated) - Use typing.Annotated for Field() with constraints.
322325
- [`--use-attribute-docstrings`](field-customization.md#use-attribute-docstrings) - Generate field descriptions as attribute docstrings instead ...
326+
- [`--use-closed-typed-dict`](typing-customization.md#use-closed-typed-dict) - Generate TypedDict with PEP 728 closed/extra_items (default:...
323327
- [`--use-decimal-for-multiple-of`](typing-customization.md#use-decimal-for-multiple-of) - Generate Decimal types for fields with multipleOf constraint...
324328
- [`--use-default`](model-customization.md#use-default) - Use default values from schema in generated models.
325329
- [`--use-default-factory-for-optional-nested-models`](model-customization.md#use-default-factory-for-optional-nested-models) - Generate default_factory for optional nested model fields.

docs/cli-reference/typing-customization.md

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
| [`--enum-field-as-literal`](#enum-field-as-literal) | Convert all enum fields to Literal types instead of Enum cla... |
1111
| [`--enum-field-as-literal-map`](#enum-field-as-literal-map) | Override enum/literal generation per-field via JSON mapping.... |
1212
| [`--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. |
1314
| [`--no-use-specialized-enum`](#no-use-specialized-enum) | Disable specialized Enum classes for Python 3.11+ code gener... |
1415
| [`--no-use-standard-collections`](#no-use-standard-collections) | Use typing.Dict/List instead of built-in dict/list for conta... |
1516
| [`--no-use-union-operator`](#no-use-union-operator) | Use Union[X, Y] / Optional[X] instead of X | Y union operato... |
@@ -19,6 +20,7 @@
1920
| [`--type-mappings`](#type-mappings) | Override default type mappings for schema formats. |
2021
| [`--type-overrides`](#type-overrides) | Replace schema model types with custom Python types via JSON... |
2122
| [`--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:... |
2224
| [`--use-decimal-for-multiple-of`](#use-decimal-for-multiple-of) | Generate Decimal types for fields with multipleOf constraint... |
2325
| [`--use-generic-container-types`](#use-generic-container-types) | Use generic container types (Sequence, Mapping) for type hin... |
2426
| [`--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.
17441746

17451747
---
17461748

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+
17471803
## `--no-use-specialized-enum` {#no-use-specialized-enum}
17481804

17491805
Disable specialized Enum classes for Python 3.11+ code generation.
@@ -3217,6 +3273,58 @@ syntax instead of default values. This also enables `--field-constraints`.
32173273

32183274
---
32193275

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+
32203328
## `--use-decimal-for-multiple-of` {#use-decimal-for-multiple-of}
32213329

32223330
Generate Decimal types for fields with multipleOf constraint.

0 commit comments

Comments
 (0)