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
9 changes: 8 additions & 1 deletion src/datamodel_code_generator/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,9 +689,16 @@ def _serialize_callable(args: tuple[type, ...]) -> str:


def _get_origin_name(origin: type) -> str:
"""Get the name of a generic origin."""
"""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.
"""
name = getattr(origin, "__name__", None)
if name:
module = getattr(origin, "__module__", "")
if module and module not in {"builtins", "typing", "collections.abc"}:
return f"{module}.{name}"
return name

# Fallback for origins without __name__ (rare edge case)
Expand Down
21 changes: 20 additions & 1 deletion tests/data/python/input_model/pydantic_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,22 @@

from __future__ import annotations

from collections import UserDict
from collections.abc import Callable, Mapping, Sequence
from typing import Any, FrozenSet, Optional, Set, Type, Union
from typing import Any, FrozenSet, Generic, Optional, Set, Type, TypeVar, Union

from pydantic import BaseModel

# Custom generic type for testing generic type import
TK = TypeVar("TK")
TV = TypeVar("TV")


class CustomGenericDict(UserDict[TK, TV], Generic[TK, TV]):
"""Custom generic dict for testing generic type import."""

pass


class User(BaseModel):
"""User model with basic fields."""
Expand Down Expand Up @@ -86,3 +97,11 @@ class ModelWithUnionCallable(BaseModel):

union_callback: Union[Callable[[str], str], int]
raw_callable: Callable # Callable without type args


class ModelWithCustomGeneric(BaseModel):
"""Model with custom generic type that requires module import."""

model_config = {"arbitrary_types_allowed": True}
custom_dict: CustomGenericDict[str, int]
optional_custom_dict: CustomGenericDict[str, str] | None
14 changes: 14 additions & 0 deletions tests/test_input_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,20 @@ def test_input_model_union_callable(tmp_path: Path) -> None:
)


@SKIP_PYDANTIC_V1
def test_input_model_custom_generic_type_import(tmp_path: Path) -> None:
"""Test that custom generic types are properly imported with full module path."""
run_input_model_and_assert(
input_model="tests.data.python.input_model.pydantic_models:ModelWithCustomGeneric",
output_path=tmp_path / "output.py",
expected_output_contains=[
"from tests.data.python.input_model.pydantic_models import CustomGenericDict",
"CustomGenericDict[str, int]",
"CustomGenericDict[str, str] | None",
],
)


# ============================================================================
# --input-model-ref-strategy tests
# ============================================================================
Expand Down
Loading