Skip to content

Handle Annotated types in _serialize_python_type for TypedDict generation#2867

Merged
koxudaxi merged 1 commit intomainfrom
fix/preserve-defaultdict-in-typeddict
Dec 30, 2025
Merged

Handle Annotated types in _serialize_python_type for TypedDict generation#2867
koxudaxi merged 1 commit intomainfrom
fix/preserve-defaultdict-in-typeddict

Conversation

@koxudaxi
Copy link
Copy Markdown
Owner

@koxudaxi koxudaxi commented Dec 30, 2025

Summary by CodeRabbit

  • New Features
    • Added support for Python's Annotated types, enabling the tool to correctly handle complex type definitions with metadata during code generation.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Dec 30, 2025

📝 Walkthrough

Walkthrough

The _serialize_python_type function in src/datamodel_code_generator/__main__.py is enhanced to recognize and handle typing.Annotated types by extracting the underlying base type and serializing it instead of metadata. If Annotated contains arguments, the first argument's serialized form is returned; otherwise, None is returned.

Changes

Cohort / File(s) Summary
Type Serialization Enhancement
src/datamodel_code_generator/__main__.py
Added handling for typing.Annotated types in _serialize_python_type() to extract and serialize the underlying base type instead of metadata annotations

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

Suggested labels

breaking-change-analyzed

Poem

🐰 Annotations bound so tight,
Extract the base—a cleaner sight!
Metadata melts away with care,
Pure types float through the data air. 🌙

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The pull request title accurately describes the main change: handling Annotated types in the _serialize_python_type function for TypedDict generation, which is clearly reflected in the changeset.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Dec 30, 2025

📚 Docs Preview: https://pr-2867.datamodel-code-generator.pages.dev

@koxudaxi koxudaxi force-pushed the fix/preserve-defaultdict-in-typeddict branch from a8e357e to e6bedb1 Compare December 30, 2025 10:40
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/datamodel_code_generator/__main__.py (1)

859-859: Consider cleaning up potentially unused noqa directives.

Static analysis suggests the noqa directives on lines 859 and 888 may be unnecessary. You could remove them and verify whether warnings actually appear.

Also applies to: 888-888

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 359a8a9 and e6bedb1.

📒 Files selected for processing (1)
  • src/datamodel_code_generator/__main__.py
🧰 Additional context used
🪛 Ruff (0.14.10)
src/datamodel_code_generator/__main__.py

859-859: Unused noqa directive (non-enabled: PLR0911)

Remove unused noqa directive

(RUF100)


888-888: Unused noqa directive (non-enabled: PLC0415)

Remove unused noqa directive

(RUF100)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (14)
  • GitHub Check: py312-isort7 on Ubuntu
  • GitHub Check: 3.10 on Windows
  • GitHub Check: 3.12 on Ubuntu
  • GitHub Check: py312-black22 on Ubuntu
  • GitHub Check: py312-black23 on Ubuntu
  • GitHub Check: py312-black24 on Ubuntu
  • GitHub Check: 3.14 on Ubuntu
  • GitHub Check: 3.11 on Windows
  • GitHub Check: 3.13 on Windows
  • GitHub Check: 3.12 on Windows
  • GitHub Check: 3.14 on Windows
  • GitHub Check: 3.13 on macOS
  • GitHub Check: benchmarks
  • GitHub Check: Analyze (python)
🔇 Additional comments (1)
src/datamodel_code_generator/__main__.py (1)

887-893: LGTM! Correctly handles Annotated types.

The logic properly extracts the base type from Annotated wrappers by taking args[0] and recursively serializing it. This ensures that container types like defaultdict are preserved when wrapped in Annotated[...]. The fallback to _simple_type_name and edge case handling are appropriate.

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Dec 30, 2025

CodSpeed Performance Report

Merging #2867 will not alter performance

Comparing fix/preserve-defaultdict-in-typeddict (e6bedb1) with main (359a8a9)

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

Summary

✅ 11 untouched
⏩ 98 skipped1

Footnotes

  1. 98 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@koxudaxi koxudaxi merged commit 23605d4 into main Dec 30, 2025
34 of 35 checks passed
@koxudaxi koxudaxi deleted the fix/preserve-defaultdict-in-typeddict branch December 30, 2025 10:44
@github-actions
Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: This PR adds handling for Annotated types in the internal _serialize_python_type function. The change extracts the base type from Annotated[T, ...] types when generating x-python-type values for TypedDict generation. This is a bug fix that improves correctness - previously Annotated types were not handled specially and would fall through to generic handling. The change is purely additive: it adds a new code block that specifically handles Annotated types before the existing logic, ensuring the base type is properly extracted. No existing behavior is broken; the function now correctly handles a case that was previously unhandled.


This analysis was performed by Claude Code Action

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jan 1, 2026

🎉 Released in 0.51.0

This PR is now available in the latest release. See the release notes for details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant