Visitar URL original
Avoid eager AsyncAPI converter imports by koxudaxi · Pull Request #3389 · datamodel-code-generator/datamodel-code-generator · GitHub
Skip to content

Avoid eager AsyncAPI converter imports - #3389

Merged
Koudai Aono (koxudaxi) merged 1 commit into
mainfrom
lazy-asyncapi-converter-imports
Jun 13, 2026
Merged

Koudai Aono (koxudaxi) merged 1 commit into
mainfrom
lazy-asyncapi-converter-imports

Conversation

@koxudaxi

@koxudaxi Koudai Aono (koxudaxi) commented Jun 13, 2026 •

Copy link
Copy Markdown
Member

No description provided.

@coderabbitai

coderabbitai Bot commented Jun 13, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: c0492e36-2933-42b8-8277-44650b6ff18c

📥 Commits

Reviewing files that changed from the base of the PR and between af3316a and 251322b.

📒 Files selected for processing (2)
  • src/datamodel_code_generator/parser/asyncapi.py
  • tests/main/asyncapi/test_main_asyncapi.py

📝 Walkthrough

Walkthrough

The PR implements lazy loading for AsyncAPI schema converters by introducing module-level __getattr__ to defer imports and moving converter imports into the schema parsing method where they are actually used, then verifies this behavior with subprocess-based tests.

Changes

Lazy Loading of AsyncAPI Schema Converters

Layer / File(s) Summary
AsyncAPI lazy loading infrastructure
src/datamodel_code_generator/parser/asyncapi.py
Module import header is updated to include create_module_getattr while removing eager converter imports. A module-level __getattr__ created via create_module_getattr exposes convert_avro_schema_data, convert_protobuf_schema_data, and convert_xml_schema_data lazily. Within _iter_schema_format_schemas, local imports are added to the Avro, Protobuf, and XML schema match cases so converters load on-demand during parsing.
Lazy loading behavior verification
tests/main/asyncapi/test_main_asyncapi.py
Test module adds json, subprocess, and sys imports alongside constants for import-probe timeout and expected converter module paths. A subprocess-based test verifies that importing the AsyncAPI parser does not populate converter modules in sys.modules. A second test confirms that converter compatibility attributes remain importable with __module__ attributes pointing to their underlying parser modules, indicating successful lazy wiring.

Possibly related PRs

  • koxudaxi/datamodel-code-generator#3388: Both PRs implement module-level lazy exports via __getattr__ to prevent eager loading of parser compatibility/converter helpers and add subprocess-based import-probing tests to assert those modules aren't loaded at import time.

Poem

🐰 A parser grows lean, deferring what's unseen,
Converters await their call, no eager load at all,
Tests verify the dance—imports come when needed once,
Lazy attributes bloom, dispelling loading gloom!


🎯 3 (Moderate) | ⏱️ ~25 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly and clearly summarizes the main change: implementing lazy loading for AsyncAPI converter imports to avoid eager imports at module load time.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch lazy-asyncapi-converter-imports

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

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

@codspeed

codspeed Bot commented Jun 13, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

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

✅ 11 untouched benchmarks
⏩ 98 skipped benchmarks1


Comparing lazy-asyncapi-converter-imports (251322b) with main (af3316a)

Open in CodSpeed

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

codecov Bot commented Jun 13, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (af3316a) to head (251322b).

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #3389   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          138       138           
  Lines        29241     29258   +17     
  Branches      3504      3504           
=========================================
+ Hits         29241     29258   +17     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

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

☔ View full report in Codecov by Harness.
📢 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
Koudai Aono (koxudaxi) merged commit 0322482 into main Jun 13, 2026
52 checks passed
@koxudaxi
Koudai Aono (koxudaxi) deleted the lazy-asyncapi-converter-imports branch June 13, 2026 09:12
@github-actions

Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: PR #3389 ("Avoid eager AsyncAPI converter imports") is an internal lazy-import refactor. It adds a create_module_getattr helper and uses a module-level __getattr__ in parser/asyncapi.py to import the converter functions (convert_avro_schema_data, convert_protobuf_schema_data, convert_xml_schema_data) on first access instead of eagerly at module load. Behavior is fully preserved: the converter symbols remain importable through getattr (nothing removed from the public API), no CLI options or Python API signatures changed, no default behavior changed, no generated code output or template changes, no Python version dropped, and unknown-attribute error handling is unchanged (still raises AttributeError). The only effect is deferring optional avro/protobuf/xmlschema imports until needed — a performance/import-structure improvement, not a breaking change. (Note: the prepared diff files under /home/runner/work/_temp were not readable from the sandboxed working directory, so this analysis is based on the merged commit 0322482, which is exactly PR #3389, and its resulting source code.)


This analysis was performed by Claude Code Action

@github-actions

Copy link
Copy Markdown
Contributor

🎉 Released in 0.64.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