Skip to content

Add defaultdict to preserved type origins for TypedDict generation#2866

Merged
koxudaxi merged 2 commits intomainfrom
fix/add-defaultdict-to-preserved-type-origins
Dec 30, 2025
Merged

Add defaultdict to preserved type origins for TypedDict generation#2866
koxudaxi merged 2 commits intomainfrom
fix/add-defaultdict-to-preserved-type-origins

Conversation

@koxudaxi
Copy link
Copy Markdown
Owner

@koxudaxi koxudaxi commented Dec 30, 2025

Summary by CodeRabbit

  • New Features
    • Enhanced handling of Python collection types (defaultdict, OrderedDict, Counter, deque, ChainMap) with improved type preservation during serialization.

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

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Dec 30, 2025

Warning

Rate limit exceeded

@koxudaxi has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 14 minutes and 9 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between d0b1a5a and da71d87.

📒 Files selected for processing (1)
  • src/datamodel_code_generator/__main__.py
📝 Walkthrough

Walkthrough

The changes expand Python type preservation to include additional collection types (defaultdict, OrderedDict, Counter, deque, ChainMap) and enhance type serialization with fallback logic to recognize collection-based types, improving JSON schema annotations for unserializable types.

Changes

Cohort / File(s) Summary
Collection Type Preservation & Serialization
src/datamodel_code_generator/__main__.py
Added imports for defaultdict, OrderedDict, Counter, deque, and ChainMap from collections module; expanded _init_preserved_type_origins return dictionary with mappings for these five types; enhanced _serialize_python_type to implement fallback logic that checks if origin belongs to collections module and uses simplified type name when not found in predefined preserved mappings

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Poem

🐰 Five collections now preserved with care,
defaultdict, OrderedDict—a matching pair!
Counter, deque, ChainMap join the fold,
With fallback logic, their stories are told.
Type annotations shine, more precise and fair! ✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Title check ⚠️ Warning The title mentions only 'defaultdict' but the changes expand preserved type origins to include five collection types (defaultdict, OrderedDict, Counter, deque, and ChainMap), making the title incomplete. Update the title to reflect all types being added, such as 'Add defaultdict and other collection types to preserved type origins' or use a more general description.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

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-2866.datamodel-code-generator.pages.dev

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Dec 30, 2025

CodSpeed Performance Report

Merging #2866 will degrade performance by 16.07%

Comparing fix/add-defaultdict-to-preserved-type-origins (da71d87) with main (83776ca)

⚠️ 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 regressions
⏩ 98 skipped1

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Mode Benchmark BASE HEAD Efficiency
WallTime test_perf_graphql_style_pydantic_v2 721.2 ms 841.5 ms -14.29%
WallTime test_perf_deep_nested 5.3 s 6.2 s -14.52%
WallTime test_perf_kubernetes_style_pydantic_v2 2.3 s 2.7 s -15.4%
WallTime test_perf_large_models_pydantic_v2 3.1 s 3.7 s -16.07%
WallTime test_perf_all_options_enabled 5.9 s 6.8 s -13.67%
WallTime test_perf_stripe_style_pydantic_v2 1.8 s 2.1 s -13.72%
WallTime test_perf_duplicate_names 871.9 ms 1,026 ms -15.03%
WallTime test_perf_openapi_large 2.5 s 3 s -14.52%
WallTime test_perf_multiple_files_input 3.2 s 3.7 s -13.66%
WallTime test_perf_complex_refs 1.8 s 2.1 s -15.07%
WallTime test_perf_aws_style_openapi_pydantic_v2 1.7 s 2 s -14.68%

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 force-pushed the fix/add-defaultdict-to-preserved-type-origins branch from 31057b8 to 8a2f769 Compare December 30, 2025 09:24
@koxudaxi koxudaxi force-pushed the fix/add-defaultdict-to-preserved-type-origins branch from 8a2f769 to d0b1a5a Compare December 30, 2025 09:26
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)

826-826: Static analysis note: Unused noqa directive.

The static analysis tool reports that the noqa: PLC0415 directive is unused because PLC0415 (import-outside-top-level) is not enabled in your ruff configuration.

🔎 Optional cleanup
-    from collections import ChainMap, Counter, OrderedDict, defaultdict, deque  # noqa: PLC0415
+    from collections import ChainMap, Counter, OrderedDict, defaultdict, deque

Based on static analysis hints.

📜 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 83776ca and d0b1a5a.

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

826-826: 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). (12)
  • GitHub Check: benchmarks
  • GitHub Check: py312-black22 on Ubuntu
  • GitHub Check: 3.12 on Ubuntu
  • GitHub Check: py312-pydantic1 on Ubuntu
  • GitHub Check: py312-black24 on Ubuntu
  • GitHub Check: 3.12 on Windows
  • GitHub Check: 3.10 on Windows
  • GitHub Check: py312-isort6 on Ubuntu
  • GitHub Check: 3.11 on Windows
  • GitHub Check: 3.14 on Windows
  • GitHub Check: 3.13 on Windows
  • GitHub Check: Analyze (python)
🔇 Additional comments (2)
src/datamodel_code_generator/__main__.py (2)

837-841: LGTM! Well-structured expansion of preserved types.

The addition of defaultdict, OrderedDict, Counter, deque, and ChainMap to the preserved type origins mapping is correct and follows the established pattern. This will properly preserve these collection types in JSON schema annotations.


887-892: LGTM! Smart fallback logic for collections types.

The fallback mechanism that checks if an origin belongs to the collections module (even when not explicitly mapped) is an excellent addition. This provides future-proofing and ensures that any collection type can be properly serialized with explicit type names, not just the predefined ones.

The logic correctly:

  • First attempts a direct lookup in preserved_origins
  • Falls back to checking the module origin for collections types
  • Only applies the fallback when origin is not None

@koxudaxi koxudaxi merged commit 359a8a9 into main Dec 30, 2025
33 of 35 checks passed
@koxudaxi koxudaxi deleted the fix/add-defaultdict-to-preserved-type-origins branch December 30, 2025 09:51
@github-actions
Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: This PR adds support for additional Python collection types (defaultdict, OrderedDict, Counter, deque, ChainMap) to the internal type serialization used with --input-model. The change is purely additive - it enhances type preservation for collection types that were previously not explicitly handled. The affected function (_serialize_python_type) is internal and not part of the public API. No CLI options, templates, defaults, or error handling changed. Generated output may include more accurate x-python-type values for these collection types, but this is an improvement, not a breaking change.


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