Skip to content

Commit 0610118

Browse files
authored
Simplify generate() function signature using Unpack[GenerateConfigDict] (#2874)
* Simplify generate() function signature using Unpack[GenerateConfigDict] * Fix aliases type to support list values (Mapping[str, str | list[str]]) * Add type and default value checks to test_generate_signature_matches_baseline
1 parent 72f3300 commit 0610118

11 files changed

Lines changed: 235 additions & 349 deletions

File tree

src/datamodel_code_generator/__init__.py

Lines changed: 168 additions & 317 deletions
Large diffs are not rendered by default.

src/datamodel_code_generator/_types/generate_config_dict.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class GenerateConfigDict(TypedDict):
5353
field_constraints: NotRequired[bool]
5454
snake_case_field: NotRequired[bool]
5555
strip_default_none: NotRequired[bool]
56-
aliases: NotRequired[Mapping[str, str] | None]
56+
aliases: NotRequired[Mapping[str, str | list[str]] | None]
5757
disable_timestamp: NotRequired[bool]
5858
enable_version_header: NotRequired[bool]
5959
enable_command_header: NotRequired[bool]

src/datamodel_code_generator/_types/parser_config_dict.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class ParserConfigDict(TypedDict):
4747
field_constraints: NotRequired[bool]
4848
snake_case_field: NotRequired[bool]
4949
strip_default_none: NotRequired[bool]
50-
aliases: NotRequired[Mapping[str, str] | None]
50+
aliases: NotRequired[Mapping[str, str | list[str]] | None]
5151
allow_population_by_field_name: NotRequired[bool]
5252
apply_default_values_for_required_fields: NotRequired[bool]
5353
allow_extra_fields: NotRequired[bool]

src/datamodel_code_generator/config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class Config:
8686
field_constraints: bool = False
8787
snake_case_field: bool = False
8888
strip_default_none: bool = False
89-
aliases: Mapping[str, str] | None = None
89+
aliases: Mapping[str, str | list[str]] | None = None
9090
disable_timestamp: bool = False
9191
enable_version_header: bool = False
9292
enable_command_header: bool = False
@@ -222,7 +222,7 @@ class Config:
222222
field_constraints: bool = False
223223
snake_case_field: bool = False
224224
strip_default_none: bool = False
225-
aliases: Mapping[str, str] | None = None
225+
aliases: Mapping[str, str | list[str]] | None = None
226226
allow_population_by_field_name: bool = False
227227
apply_default_values_for_required_fields: bool = False
228228
allow_extra_fields: bool = False

src/datamodel_code_generator/parser/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ def __init__( # noqa: PLR0912, PLR0913, PLR0915
707707
field_constraints: bool = False,
708708
snake_case_field: bool = False,
709709
strip_default_none: bool = False,
710-
aliases: Mapping[str, str] | None = None,
710+
aliases: Mapping[str, str | list[str]] | None = None,
711711
allow_population_by_field_name: bool = False,
712712
apply_default_values_for_required_fields: bool = False,
713713
allow_extra_fields: bool = False,

src/datamodel_code_generator/parser/graphql.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def __init__( # noqa: PLR0913
122122
field_constraints: bool = False,
123123
snake_case_field: bool = False,
124124
strip_default_none: bool = False,
125-
aliases: Mapping[str, str] | None = None,
125+
aliases: Mapping[str, str | list[str]] | None = None,
126126
allow_population_by_field_name: bool = False,
127127
apply_default_values_for_required_fields: bool = False,
128128
allow_extra_fields: bool = False,

src/datamodel_code_generator/parser/jsonschema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ def __init__( # noqa: PLR0913
658658
field_constraints: bool = False,
659659
snake_case_field: bool = False,
660660
strip_default_none: bool = False,
661-
aliases: Mapping[str, str] | None = None,
661+
aliases: Mapping[str, str | list[str]] | None = None,
662662
allow_population_by_field_name: bool = False,
663663
apply_default_values_for_required_fields: bool = False,
664664
allow_extra_fields: bool = False,

src/datamodel_code_generator/parser/openapi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ def __init__( # noqa: PLR0913
202202
field_constraints: bool = False,
203203
snake_case_field: bool = False,
204204
strip_default_none: bool = False,
205-
aliases: Mapping[str, str] | None = None,
205+
aliases: Mapping[str, str | list[str]] | None = None,
206206
allow_population_by_field_name: bool = False,
207207
allow_extra_fields: bool = False,
208208
extra_fields: str | None = None,

src/datamodel_code_generator/reference.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ def __init__( # noqa: PLR0913, PLR0917
526526
duplicate_name_suffix: str | None = None,
527527
base_url: str | None = None,
528528
singular_name_suffix: str | None = None,
529-
aliases: Mapping[str, str] | None = None,
529+
aliases: Mapping[str, str | list[str]] | None = None,
530530
snake_case_field: bool = False, # noqa: FBT001, FBT002
531531
empty_field_name: str | None = None,
532532
custom_class_name_generator: Callable[[str], str] | None = None,

tests/main/test_main_general.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1978,6 +1978,7 @@ def test_generate_with_config_object(output_file: Path) -> None:
19781978
GenerateConfig.model_rebuild(_types_namespace={"StrictTypes": StrictTypes, "UnionMode": UnionMode})
19791979
config = GenerateConfig(
19801980
input_filename="test.json",
1981+
output=output_file,
19811982
output_model_type=DataModelType.PydanticV2BaseModel,
19821983
use_schema_description=True,
19831984
snake_case_field=True,
@@ -1986,7 +1987,6 @@ def test_generate_with_config_object(output_file: Path) -> None:
19861987
)
19871988
generate(
19881989
input_='{"type": "object", "properties": {"userName": {"type": "string"}}}',
1989-
output=output_file,
19901990
config=config,
19911991
)
19921992
content = output_file.read_text(encoding="utf-8")
@@ -1995,25 +1995,20 @@ def test_generate_with_config_object(output_file: Path) -> None:
19951995

19961996

19971997
@pytest.mark.skipif(pydantic.VERSION < "2.0.0", reason="GenerateConfig requires Pydantic v2")
1998-
def test_generate_with_config_object_extra_template_data_override(output_file: Path) -> None:
1999-
"""Test generate() with extra_template_data passed directly, overriding config."""
2000-
from collections import defaultdict
2001-
1998+
def test_generate_with_config_and_kwargs_raises_error(output_file: Path) -> None:
1999+
"""Test generate() raises error when both config and kwargs are provided."""
20022000
from datamodel_code_generator.model.pydantic_v2 import UnionMode
20032001
from datamodel_code_generator.types import StrictTypes
20042002

20052003
GenerateConfig.model_rebuild(_types_namespace={"StrictTypes": StrictTypes, "UnionMode": UnionMode})
20062004
config = GenerateConfig(
20072005
input_filename="test.json",
20082006
output_model_type=DataModelType.PydanticV2BaseModel,
2009-
extra_template_data={"Model": {"config_key": "config_value"}},
2010-
)
2011-
# Pass extra_template_data directly - this should override config value
2012-
generate(
2013-
input_='{"type": "object", "properties": {"name": {"type": "string"}}}',
2014-
output=output_file,
2015-
config=config,
2016-
extra_template_data=defaultdict(dict, {"Model": {"direct_key": "direct_value"}}),
20172007
)
2018-
content = output_file.read_text(encoding="utf-8")
2019-
assert "class Model" in content
2008+
# Passing both config and kwargs should raise ValueError
2009+
with pytest.raises(ValueError, match="Cannot specify both 'config' and keyword arguments"):
2010+
generate(
2011+
input_='{"type": "object", "properties": {"name": {"type": "string"}}}',
2012+
output=output_file,
2013+
config=config,
2014+
)

0 commit comments

Comments
 (0)