Skip to content

Commit 69eda18

Browse files
butvinmclaudekoxudaxi
authored
Skip default_factory wrapping for non-callable type aliases (#3012)
* fix: skip default_factory wrapping for non-callable type aliases (#3009) When --use-type-alias + --use-annotated + --enum-field-as-literal all are combined, the generator produced `default_factory=lambda: Mode('default')` where Mode is a PEP 695 type alias (not callable). Add a TypeAliasBase guard in __wrap_root_model_default_values to skip wrapping for type aliases. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: add black 22 skipif and ruff format for type alias test Skip the enum_literal_type_alias_default test on black < 23 since PEP 695 `type` statement syntax is unsupported. Reformat long conditional in parser/base.py to satisfy ruff line-length. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * test: simplify docstring for type alias default test Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: Koudai Aono <koxudaxi@gmail.com>
1 parent 5a9cdec commit 69eda18

4 files changed

Lines changed: 61 additions & 1 deletion

File tree

src/datamodel_code_generator/parser/base.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2158,7 +2158,11 @@ def __wrap_root_model_default_values(
21582158
continue
21592159
if isinstance(model_field.default, list):
21602160
continue
2161-
if data_type.reference and isinstance(data_type.reference.source, self.data_model_root_type):
2161+
if (
2162+
data_type.reference
2163+
and isinstance(data_type.reference.source, self.data_model_root_type)
2164+
and not isinstance(data_type.reference.source, TypeAliasBase)
2165+
):
21622166
type_name = data_type.alias or data_type.reference.short_name
21632167
model_field.default = WrappedDefault(
21642168
value=model_field.default,
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# generated by datamodel-codegen:
2+
# filename: enum_literal_type_alias_default.json
3+
# timestamp: 2019-07-26T00:00:00+00:00
4+
5+
from __future__ import annotations
6+
7+
from typing import Literal
8+
9+
from pydantic import BaseModel
10+
11+
type Mode = Literal['default', 'plan']
12+
13+
14+
class Model(BaseModel):
15+
mode: Mode | None = 'default'
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"type": "object",
4+
"definitions": {
5+
"Mode": {
6+
"type": "string",
7+
"enum": ["default", "plan"]
8+
}
9+
},
10+
"properties": {
11+
"mode": {
12+
"$ref": "#/definitions/Mode",
13+
"default": "default"
14+
}
15+
}
16+
}

tests/main/jsonschema/test_main_jsonschema.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5741,6 +5741,31 @@ def test_main_jsonschema_type_alias_with_field_description_py312(output_file: Pa
57415741
)
57425742

57435743

5744+
@pytest.mark.skipif(
5745+
int(black.__version__.split(".")[0]) < 23,
5746+
reason="Installed black doesn't support the new 'type' statement",
5747+
)
5748+
def test_main_jsonschema_enum_literal_type_alias_default(output_file: Path) -> None:
5749+
"""Don't wrap type alias defaults in default_factory (TypeAliasType is not callable)."""
5750+
run_main_and_assert(
5751+
input_path=JSON_SCHEMA_DATA_PATH / "enum_literal_type_alias_default.json",
5752+
output_path=output_file,
5753+
input_file_type=None,
5754+
assert_func=assert_file_content,
5755+
expected_file="enum_literal_type_alias_default.py",
5756+
extra_args=[
5757+
"--use-type-alias",
5758+
"--use-annotated",
5759+
"--enum-field-as-literal",
5760+
"all",
5761+
"--target-python-version",
5762+
"3.12",
5763+
"--output-model-type",
5764+
"pydantic_v2.BaseModel",
5765+
],
5766+
)
5767+
5768+
57445769
@pytest.mark.cli_doc(
57455770
options=["--type-mappings"],
57465771
option_description="""Override default type mappings for schema formats.

0 commit comments

Comments
 (0)