Skip to content

Commit 7a4709c

Browse files
authored
Fix reuse-model generating type aliases instead of class inheritance (#2853)
1 parent c46b64f commit 7a4709c

3 files changed

Lines changed: 6 additions & 13 deletions

File tree

src/datamodel_code_generator/model/template/pydantic_v2/BaseModel.jinja2

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
{% if base_class != "BaseModel" and "," not in base_class and not fields and not config and not description -%}
2-
3-
{# if this is just going to be `class Foo(Bar): pass`, then might as well just make Foo
4-
an alias for Bar: every pydantic model class consumes considerable memory. #}
5-
{{ class_name }} = {{ base_class }}
6-
7-
{% else -%}
8-
91
{% for decorator in decorators -%}
102
{{ decorator }}
113
{% endfor -%}
@@ -53,5 +45,3 @@ class {{ class_name }}({{ base_class }}):{% if comment is defined %} # {{ comme
5345
{{ method }}
5446
{%- endfor -%}
5547
{%- endfor -%}
56-
57-
{%- endif %}

tests/data/expected/main/json/json_reuse_model_pydantic2.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ class ArmRight(BaseModel):
1313
Joint_3: int = Field(..., alias='Joint 3')
1414

1515

16-
ArmLeft = ArmRight
16+
class ArmLeft(ArmRight):
17+
pass
1718

1819

1920
class Head(BaseModel):

tests/data/expected/main/jsonschema/allof_root_model_constraints_merge_pydantic_v2.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ class ObjectNoPropsDatatype(BaseModel):
9494
pass
9595

9696

97-
RefToObjectNoPropsAllOf = ObjectNoPropsDatatype
97+
class RefToObjectNoPropsAllOf(ObjectNoPropsDatatype):
98+
pass
9899

99100

100101
class PatternPropsDatatype(RootModel[dict[constr(pattern=r'^S_'), str]]):
@@ -109,7 +110,8 @@ class NestedAllOfDatatype(BaseModel):
109110
pass
110111

111112

112-
RefToNestedAllOfAllOf = NestedAllOfDatatype
113+
class RefToNestedAllOfAllOf(NestedAllOfDatatype):
114+
pass
113115

114116

115117
class ConstraintsOnlyDatatype(RootModel[Any]):

0 commit comments

Comments
 (0)