Skip to content

Commit 23605d4

Browse files
authored
Handle Annotated types in _serialize_python_type for TypedDict generation (#2867)
1 parent 359a8a9 commit 23605d4

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

src/datamodel_code_generator/__main__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ def _get_preserved_type_origins() -> dict[type, str]:
856856
return _PRESERVED_TYPE_ORIGINS
857857

858858

859-
def _serialize_python_type(tp: type) -> str | None:
859+
def _serialize_python_type(tp: type) -> str | None: # noqa: PLR0911
860860
"""Serialize Python type to a string for x-python-type field.
861861
862862
Returns None if the type doesn't need to be preserved (e.g., standard dict, list).
@@ -884,6 +884,14 @@ def _serialize_python_type(tp: type) -> str | None:
884884
return " | ".join(n or _simple_type_name(a) for n, a in zip(nested, args, strict=False))
885885
return None # pragma: no cover
886886

887+
# Handle Annotated types - extract the base type and ignore metadata
888+
from typing import Annotated # noqa: PLC0415
889+
890+
if origin is Annotated:
891+
if args:
892+
return _serialize_python_type(args[0]) or _simple_type_name(args[0])
893+
return None # pragma: no cover
894+
887895
type_name: str | None = None
888896
if origin is not None:
889897
type_name = preserved_origins.get(origin)

0 commit comments

Comments
 (0)