From 54ab6e1a93b6ea5479601fbb14d7ffc9b2029724 Mon Sep 17 00:00:00 2001 From: Koudai Aono Date: Tue, 30 Dec 2025 05:53:53 +0000 Subject: [PATCH] Use __qualname__ for nested class support and add DefaultPutDict test --- src/datamodel_code_generator/__main__.py | 4 ++-- tests/data/python/input_model/pydantic_models.py | 12 ++++++++++++ tests/test_input_model.py | 14 ++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/datamodel_code_generator/__main__.py b/src/datamodel_code_generator/__main__.py index 70f3fba0c..9258f052d 100644 --- a/src/datamodel_code_generator/__main__.py +++ b/src/datamodel_code_generator/__main__.py @@ -692,9 +692,9 @@ def _get_origin_name(origin: type) -> str: """Get the fully qualified name of a generic origin. For types from builtins, typing, or collections.abc, returns just the name. - For other types (custom generics), returns module.name format. + For other types (custom generics), returns module.qualname format. """ - name = getattr(origin, "__name__", None) + name = getattr(origin, "__qualname__", None) or getattr(origin, "__name__", None) if name: module = getattr(origin, "__module__", "") if module and module not in {"builtins", "typing", "collections.abc"}: diff --git a/tests/data/python/input_model/pydantic_models.py b/tests/data/python/input_model/pydantic_models.py index 24e3b4dc2..5c7754795 100644 --- a/tests/data/python/input_model/pydantic_models.py +++ b/tests/data/python/input_model/pydantic_models.py @@ -105,3 +105,15 @@ class ModelWithCustomGeneric(BaseModel): model_config = {"arbitrary_types_allowed": True} custom_dict: CustomGenericDict[str, int] optional_custom_dict: CustomGenericDict[str, str] | None + + +# Import DefaultPutDict for testing real-world generic type import +from datamodel_code_generator.parser import DefaultPutDict # noqa: E402 + + +class ModelWithDefaultPutDict(BaseModel): + """Model with DefaultPutDict to test generic type import from parser module.""" + + model_config = {"arbitrary_types_allowed": True} + cache: DefaultPutDict[str, str] + optional_cache: DefaultPutDict[str, int] | None diff --git a/tests/test_input_model.py b/tests/test_input_model.py index ca6be617d..ada8ee317 100644 --- a/tests/test_input_model.py +++ b/tests/test_input_model.py @@ -741,6 +741,20 @@ def test_input_model_custom_generic_type_import(tmp_path: Path) -> None: ) +@SKIP_PYDANTIC_V1 +def test_input_model_default_put_dict_import(tmp_path: Path) -> None: + """Test that DefaultPutDict generic type is properly imported from parser module.""" + run_input_model_and_assert( + input_model="tests.data.python.input_model.pydantic_models:ModelWithDefaultPutDict", + output_path=tmp_path / "output.py", + expected_output_contains=[ + "from datamodel_code_generator.parser import DefaultPutDict", + "DefaultPutDict[str, str]", + "DefaultPutDict[str, int] | None", + ], + ) + + # ============================================================================ # --input-model-ref-strategy tests # ============================================================================