Skip to content

Remove WithJsonSchema from ExtraTemplateDataType#2868

Merged
koxudaxi merged 6 commits intomainfrom
fix/preserve-defaultdict-in-typeddict
Dec 30, 2025
Merged

Remove WithJsonSchema from ExtraTemplateDataType#2868
koxudaxi merged 6 commits intomainfrom
fix/preserve-defaultdict-in-typeddict

Conversation

@koxudaxi
Copy link
Copy Markdown
Owner

@koxudaxi koxudaxi commented Dec 30, 2025

Summary by CodeRabbit

  • Bug Fixes

    • More accurate handling and serialization of generic/parameterized types for improved type representations.
  • Refactor

    • Added support for additional collection types (defaultdict, OrderedDict, Counter, deque, ChainMap) in type resolution.
    • Adjusted typing strategy to vary by typing context and library version, and improved import detection for complex type annotations.

✏️ 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 7 minutes and 53 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 69d1a49 and 80fa0d7.

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

Walkthrough

Improves generic-type string serialization in _simple_type_name, makes ExtraTemplateDataType conditional on TYPE_CHECKING and Pydantic v2 presence, and extends jsonschema parser overrides to include several lowercase collection types with corresponding compatibility and import handling.

Changes

Cohort / File(s) Summary
Generic type serialization
src/datamodel_code_generator/__main__.py
_simple_type_name now uses typing.get_origin() for parameterized/generic types and returns the full type string (with typing. stripped) instead of the simple name.
Pydantic version–aware template type
src/datamodel_code_generator/config.py
Removed WithJsonSchema import. Replaced single ExtraTemplateDataType alias with conditional definitions: plain type under TYPE_CHECKING, an Annotated[dict, Field(default_factory=dict)] variant when is_pydantic_v2() is true, otherwise plain type.
JSON Schema parser: lowercase collection overrides
src/datamodel_code_generator/parser/jsonschema.py
Added lowercase collection names to PYTHON_TYPE_OVERRIDE_ALWAYS (defaultdict, OrderedDict, Counter, deque, ChainMap). _is_compatible_python_type rejects matches containing these lowercase names; _get_python_type_override collects and emits required imports (including nested/qualified names) for them.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested labels

breaking-change-analyzed, breaking-change

Poem

🐰 I hopped through types both new and old,
Generics now shown in silver and gold,
Defaultdicts and counters join the play,
Pydantic knows which way to sway,
Code-rabbit dances, type-safe all day!

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 accurately describes the main change: removing WithJsonSchema from the ExtraTemplateDataType definition in config.py. This is the primary objective evident across all three modified files.
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-2868.datamodel-code-generator.pages.dev

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Dec 30, 2025

CodSpeed Performance Report

Merging #2868 will not alter performance

Comparing fix/preserve-defaultdict-in-typeddict (80fa0d7) with main (23605d4)

⚠️ 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 force-pushed the fix/preserve-defaultdict-in-typeddict branch from cca2978 to 98d7f02 Compare December 30, 2025 11:14
@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.38%. Comparing base (12fc77c) to head (80fa0d7).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2868      +/-   ##
==========================================
- Coverage   99.52%   99.38%   -0.14%     
==========================================
  Files          90       91       +1     
  Lines       14958    15555     +597     
  Branches     1791     1827      +36     
==========================================
+ Hits        14887    15460     +573     
- Misses         38       50      +12     
- Partials       33       45      +12     
Flag Coverage Δ
unittests 99.38% <100.00%> (-0.14%) ⬇️

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.

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: 1

📜 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 23605d4 and 98d7f02.

📒 Files selected for processing (3)
  • src/datamodel_code_generator/__main__.py
  • src/datamodel_code_generator/config.py
  • src/datamodel_code_generator/parser/jsonschema.py
🧰 Additional context used
🧬 Code graph analysis (2)
src/datamodel_code_generator/config.py (1)
src/datamodel_code_generator/util.py (1)
  • is_pydantic_v2 (52-57)
src/datamodel_code_generator/parser/jsonschema.py (2)
src/datamodel_code_generator/imports.py (1)
  • append (74-89)
src/datamodel_code_generator/parser/base.py (1)
  • data_type (1094-1096)
🪛 Ruff (0.14.10)
src/datamodel_code_generator/__main__.py

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

918-924: LGTM! Correctly handles generic type serialization.

The addition of get_origin check properly handles parameterized types like dict[str, Any], ensuring the full type representation is returned instead of just the base type name.

Minor note: Line 918 has an unused noqa directive for PLC0415 since that rule isn't enabled in the project's linter configuration. However, keeping it for consistency with similar patterns throughout the file is acceptable.

src/datamodel_code_generator/parser/jsonschema.py (2)

632-637: LGTM! Well-chosen Python-specific collection types.

These lowercase collection types are correctly identified as requiring x-python-type override since they have no direct JSON Schema equivalents. The corresponding imports are properly defined in PYTHON_TYPE_IMPORTS (lines 584-588).


1484-1490: LGTM! Import collection logic is consistent.

The logic correctly mirrors the compatibility check at lines 1400-1403, ensuring that lowercase override types found in type strings get their imports added. The condition override_type != base_type appropriately prevents duplicate imports when the base type itself is a lowercase override type.

src/datamodel_code_generator/config.py (2)

10-10: LGTM!

The removal of WithJsonSchema from imports aligns with the PR objective and simplifies the dependencies.


51-56: This pattern is correct and intentional for Pydantic v2.

The nested Annotated[dict[str, Any], Field(default_factory=dict)] is valid metadata for Pydantic v2's schema generation. The commit that introduced this explicitly removed the outer WithJsonSchema annotation, stating "automatic handling works"—meaning Pydantic v2 processes the nested Field metadata without needing explicit schema hints.

Since extra_template_data is defined as ExtraTemplateDataType | None = None (lines 83, 216), the nested Field metadata correctly does not affect field initialization; it exists solely for schema generation and validation purposes. This is the intended behavior.

Comment thread src/datamodel_code_generator/parser/jsonschema.py Outdated
@koxudaxi koxudaxi merged commit 602c45a into main Dec 30, 2025
37 of 38 checks passed
@koxudaxi koxudaxi deleted the fix/preserve-defaultdict-in-typeddict branch December 30, 2025 12:22
@github-actions
Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: PR #2868 makes internal implementation changes that don't affect the public API or generated code output in breaking ways:

  1. ExtraTemplateDataType change: Removes WithJsonSchema annotation from ExtraTemplateDataType type alias. This was only used for JSON Schema metadata during TypedDict generation, an internal feature. The actual runtime type remains defaultdict[str, dict[str, Any]]. Users passing extra_template_data to generate() or config objects will not notice any difference.

  2. PYTHON_TYPE_OVERRIDE_ALWAYS expansion: Adding defaultdict, OrderedDict, Counter, deque, and ChainMap to the override list is a bug fix. These collection types have no JSON Schema equivalent, so they should always use the x-python-type override. This improves accuracy rather than breaking existing behavior.

  3. _extract_all_type_names refactoring: Switching from regex to AST parsing for type name extraction is an internal implementation detail. The fallback regex pattern change only affects the SyntaxError fallback path and improves matching.

  4. _simple_type_name enhancement: Adding get_origin() check for generic types is purely an internal improvement for TypedDict generation accuracy.

No CLI options, Python API signatures, generated code format, or custom template requirements were changed.


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