Skip to content

Commit 253bd7c

Browse files
authored
Fix aliasing of builtin type field names (#2738)
1 parent 522101a commit 253bd7c

4 files changed

Lines changed: 65 additions & 8 deletions

File tree

src/datamodel_code_generator/parser/base.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2049,16 +2049,20 @@ def __alias_shadowed_imports( # noqa: PLR6301
20492049
all_model_field_names: set[str],
20502050
) -> None:
20512051
for _, model_field, data_type in iter_models_field_data_types(models):
2052-
if data_type and data_type.type in all_model_field_names and data_type.type == model_field.name:
2052+
if (
2053+
data_type
2054+
and data_type.import_
2055+
and data_type.type in all_model_field_names
2056+
and data_type.type == model_field.name
2057+
):
20532058
alias = data_type.type + "_aliased"
20542059
data_type.type = alias
2055-
if data_type.import_: # pragma: no cover
2056-
data_type.import_ = Import(
2057-
from_=data_type.import_.from_,
2058-
import_=data_type.import_.import_,
2059-
alias=alias,
2060-
reference_path=data_type.import_.reference_path,
2061-
)
2060+
data_type.import_ = Import(
2061+
from_=data_type.import_.from_,
2062+
import_=data_type.import_.import_,
2063+
alias=alias,
2064+
reference_path=data_type.import_.reference_path,
2065+
)
20622066

20632067
def __apply_generic_base_class( # noqa: PLR0912, PLR0914, PLR0915
20642068
self,
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# generated by datamodel-codegen:
2+
# filename: builtin_field_names.json
3+
# timestamp: 2019-07-26T00:00:00+00:00
4+
5+
from __future__ import annotations
6+
7+
from pydantic import BaseModel
8+
9+
10+
class Model(BaseModel):
11+
int: int | None = None
12+
float: float | None = None
13+
bool: bool | None = None
14+
str: str | None = None
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"$schema": "http://json-schema.org/draft/2019-09/schema#",
3+
"type": "object",
4+
"properties": {
5+
"int": {
6+
"type": "integer"
7+
},
8+
"float": {
9+
"type": "number"
10+
},
11+
"bool": {
12+
"type": "boolean"
13+
},
14+
"str": {
15+
"type": "string"
16+
}
17+
}
18+
}

tests/main/jsonschema/test_main_jsonschema.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5254,3 +5254,24 @@ def test_main_nullable_array_items_strict_nullable(output_file: Path) -> None:
52545254
"pydantic_v2.BaseModel",
52555255
],
52565256
)
5257+
5258+
5259+
@pytest.mark.benchmark
5260+
def test_main_builtin_field_names(output_file: Path) -> None:
5261+
"""Test that builtin type names as field names don't break code generation (issue #2431).
5262+
5263+
When a field has a name that matches a Python builtin (int, float, bool, str),
5264+
the generated code should still use the builtin type directly without aliasing,
5265+
since builtin types don't require imports.
5266+
"""
5267+
run_main_and_assert(
5268+
input_path=JSON_SCHEMA_DATA_PATH / "builtin_field_names.json",
5269+
output_path=output_file,
5270+
input_file_type="jsonschema",
5271+
assert_func=assert_file_content,
5272+
expected_file="builtin_field_names.py",
5273+
extra_args=[
5274+
"--output-model-type",
5275+
"pydantic_v2.BaseModel",
5276+
],
5277+
)

0 commit comments

Comments
 (0)