Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/datamodel_code_generator/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}:
Expand Down
12 changes: 12 additions & 0 deletions tests/data/python/input_model/pydantic_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 14 additions & 0 deletions tests/test_input_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
# ============================================================================
Expand Down
Loading