Skip to content

Add defaultdict and Any to PYTHON_TYPE_IMPORTS#2860

Merged
koxudaxi merged 5 commits intomainfrom
fix/defaultdict-any-import
Dec 30, 2025
Merged

Add defaultdict and Any to PYTHON_TYPE_IMPORTS#2860
koxudaxi merged 5 commits intomainfrom
fix/defaultdict-any-import

Conversation

@koxudaxi
Copy link
Copy Markdown
Owner

@koxudaxi koxudaxi commented Dec 30, 2025

Summary by CodeRabbit

  • New Features
    • Expanded Python type support in code generation to include defaultdict and Any types with automatic import handling.

✏️ 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 20 minutes and 2 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 e6eb1b5 and e7ab5cb.

⛔ Files ignored due to path filters (1)
  • tests/data/jsonschema/x_python_type_dynamic_resolve.json is excluded by !tests/data/**/*.json and included by none
📒 Files selected for processing (4)
  • docs/python-model.md
  • src/datamodel_code_generator/parser/jsonschema.py
  • tests/data/expected/main/jsonschema/x_python_type_dynamic_resolve.py
  • tests/main/jsonschema/test_main_jsonschema.py
📝 Walkthrough

Walkthrough

Two new Python type imports are added to the JsonSchemaParser.PYTHON_TYPE_IMPORTS mapping: defaultdict from the collections module and Any from the typing module. This expands the explicit type imports available for generated code.

Changes

Cohort / File(s) Summary
Python Type Imports Expansion
src/datamodel_code_generator/parser/jsonschema.py
Added two new entries to PYTHON_TYPE_IMPORTS class variable: "defaultdict"Import.from_full_path("collections.defaultdict") and "Any"Import.from_full_path("typing.Any")

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Possibly related PRs

Poem

🐰 A rabbit's ode to typing's grace,
Defaultdict and Any find their place,
In imports neat, so clean and bright,
The code hops on with perfect type!

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 title directly and concisely describes the main change: adding defaultdict and Any to PYTHON_TYPE_IMPORTS, which matches the actual modifications in the JsonSchemaParser class.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Dec 30, 2025

CodSpeed Performance Report

Merging #2860 will not alter performance

Comparing fix/defaultdict-any-import (e7ab5cb) with main (8c7a9b2)

⚠️ 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.

@codecov
Copy link
Copy Markdown

codecov Bot commented Dec 30, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.52%. Comparing base (63d8804) to head (e7ab5cb).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2860   +/-   ##
=======================================
  Coverage   99.52%   99.52%           
=======================================
  Files          90       90           
  Lines       14938    14958   +20     
  Branches     1788     1791    +3     
=======================================
+ Hits        14867    14887   +20     
  Misses         38       38           
  Partials       33       33           
Flag Coverage Δ
unittests 99.52% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@koxudaxi koxudaxi merged commit 12fc77c into main Dec 30, 2025
38 checks passed
@koxudaxi koxudaxi deleted the fix/defaultdict-any-import branch December 30, 2025 07:41
@github-actions
Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: This PR is purely additive - it expands the PYTHON_TYPE_IMPORTS dictionary with additional types (defaultdict, Any, Mapping, Sequence, Path, UUID, datetime, etc.) and adds a dynamic fallback mechanism for types not in the static mapping. All previously supported types continue to work identically. The changes only add support for additional types in x-python-type, meaning schemas that previously would have generated incomplete imports now generate correct imports. This is a feature enhancement/bug fix, 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