Add defaultdict to preserved type origins for TypedDict generation#2866
Add defaultdict to preserved type origins for TypedDict generation#2866
Conversation
|
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 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. 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughThe 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
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
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. Comment |
|
📚 Docs Preview: https://pr-2866.datamodel-code-generator.pages.dev |
CodSpeed Performance ReportMerging #2866 will degrade performance by 16.07%Comparing
|
| 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
-
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. ↩
31057b8 to
8a2f769
Compare
8a2f769 to
d0b1a5a
Compare
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
src/datamodel_code_generator/__main__.py (1)
826-826: Static analysis note: Unusednoqadirective.The static analysis tool reports that the
noqa: PLC0415directive is unused becausePLC0415(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, dequeBased on static analysis hints.
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 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, andChainMapto 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
collectionsmodule (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
originis not None
Breaking Change AnalysisResult: 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 |
|
🎉 Released in 0.51.0 This PR is now available in the latest release. See the release notes for details. |
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.