Skip to content

Hoist the local imports of modules that are already loaded - #7265

Merged
Koudai Aono (koxudaxi) merged 7 commits into
mainfrom
hoist-loaded-local-imports
Nov 21, 2026
Merged

Koudai Aono (koxudaxi) merged 7 commits into
mainfrom
hoist-loaded-local-imports

Conversation

@koxudaxi

@koxudaxi Koudai Aono (koxudaxi) commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

Summary by CodeRabbit

  • Refactor
    • Reorganized internal imports across code generation, parsing, formatting, and related components.
    • Existing behavior and public interfaces remain unchanged. No user-facing features or fixes are included in this update.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-21T02:24:33.294937Z 7f85e34 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@coderabbitai

coderabbitai Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Explain Change →

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review

No actionable comments were generated in the recent review.

Walkthrough

The pull request moves imports from function-local scopes to module scope across source loading, parsing, model handling, and generation modules. The supplied summaries report no changes to runtime behavior or public declarations.

Changes: Import Consolidation

Layer / File(s) Summary
Source and input loading
src/datamodel_code_generator/_json_output.py, src/datamodel_code_generator/_json_schema.py, src/datamodel_code_generator/_paths.py, src/datamodel_code_generator/_recommend/*, src/datamodel_code_generator/_source.py, src/datamodel_code_generator/input_model.py, src/datamodel_code_generator/util.py
Imports used by source decoding, recommendation setup, input-model loading, and document utilities move to module scope. Existing function logic remains unchanged.
Parser and schema processing
src/datamodel_code_generator/parser/_passes/*, src/datamodel_code_generator/parser/_schema_object.py, src/datamodel_code_generator/parser/_stdout_repair.py, src/datamodel_code_generator/parser/_tag_plan.py, src/datamodel_code_generator/parser/_type_overrides.py, src/datamodel_code_generator/parser/avro.py, src/datamodel_code_generator/parser/base.py, src/datamodel_code_generator/parser/generation.py, src/datamodel_code_generator/parser/graphql.py, src/datamodel_code_generator/parser/jsonschema.py, src/datamodel_code_generator/parser/openapi.py, src/datamodel_code_generator/validators.py
Imports used by parser passes, schema validation, and format-specific parsing move to module scope. The summaries report unchanged validation, normalization, and conversion behavior.
Model and target handling
src/datamodel_code_generator/_target_binding.py, src/datamodel_code_generator/_target_templates.py, src/datamodel_code_generator/model/__init__.py, src/datamodel_code_generator/model/base.py, src/datamodel_code_generator/model/enum.py, src/datamodel_code_generator/model/pydantic_v2/base_model.py, src/datamodel_code_generator/python_literal.py
Imports used by model construction, target binding, template handling, and literal rendering move to module scope. Existing call-site behavior remains unchanged.
Generation and runtime integration
src/datamodel_code_generator/_agent_skill_cli.py, src/datamodel_code_generator/_api_generation.py, src/datamodel_code_generator/_client/render.py, src/datamodel_code_generator/_debug.py, src/datamodel_code_generator/_generation.py, src/datamodel_code_generator/_project_config.py, src/datamodel_code_generator/base_config.py, src/datamodel_code_generator/format.py, src/datamodel_code_generator/watch.py
Imports used by generation, API planning, configuration, formatting, CLI, client rendering, debugging, and watch modules move to module scope. The summaries report no public declaration or runtime behavior changes.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Refactor

Merge Risk: Minimal · previous review 7f85e

This is an import-organization refactor with no intended behavior change. No concrete merge-blocking risk was identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "Hoist the local imports of modules that are already loaded" matches the main change. The raw summary shows many function-local imports moved to module scope across parser, model, client, an…
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.
Full details: Title check

Explanation

The title "Hoist the local imports of modules that are already loaded" matches the main change. The raw summary shows many function-local imports moved to module scope across parser, model, client, and root modules. The PR objectives describe the same goal. The title is specific and describes the primary change. The title does not mention the retained test-patched and public-API-recorded imports, but that detail is not required.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between a78a8cb and 7f85e34.

Files selected for processing: 37

  • src/datamodel_code_generator/_agent_skill_cli.py
  • src/datamodel_code_generator/_api_generation.py
  • src/datamodel_code_generator/_client/render.py
  • src/datamodel_code_generator/_debug.py
  • src/datamodel_code_generator/_generation.py
  • src/datamodel_code_generator/_json_output.py
  • src/datamodel_code_generator/_json_schema.py
  • src/datamodel_code_generator/_paths.py
  • src/datamodel_code_generator/_project_config.py
  • src/datamodel_code_generator/_recommend/__init__.py
  • src/datamodel_code_generator/_recommend/_signals.py
  • src/datamodel_code_generator/_source.py
  • src/datamodel_code_generator/_target_binding.py
  • src/datamodel_code_generator/_target_templates.py
  • src/datamodel_code_generator/base_config.py
  • src/datamodel_code_generator/format.py
  • src/datamodel_code_generator/input_model.py
  • src/datamodel_code_generator/model/__init__.py
  • src/datamodel_code_generator/model/base.py
  • src/datamodel_code_generator/model/enum.py
  • src/datamodel_code_generator/model/pydantic_v2/base_model.py
  • src/datamodel_code_generator/parser/_passes/_shared.py
  • src/datamodel_code_generator/parser/_passes/render_prep.py
  • src/datamodel_code_generator/parser/_schema_object.py
  • src/datamodel_code_generator/parser/_stdout_repair.py
  • src/datamodel_code_generator/parser/_tag_plan.py
  • src/datamodel_code_generator/parser/_type_overrides.py
  • src/datamodel_code_generator/parser/avro.py
  • src/datamodel_code_generator/parser/base.py
  • src/datamodel_code_generator/parser/generation.py
  • src/datamodel_code_generator/parser/graphql.py
  • src/datamodel_code_generator/parser/jsonschema.py
  • src/datamodel_code_generator/parser/openapi.py
  • src/datamodel_code_generator/python_literal.py
  • src/datamodel_code_generator/util.py
  • src/datamodel_code_generator/validators.py
  • src/datamodel_code_generator/watch.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 5 remain after this review.

@github-actions

Copy link
Copy Markdown
Contributor

@codspeed

codspeed Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Merging this PR will improve performance by 15.1%

1 warning

⚠️ Walltime results may be unreliable

Some benchmarks used the Walltime instrument outside CodSpeed Macro Runners, which can make the results inconsistent.

Learn more about Macro Runners

See affected benchmarks

⚡ 3 improved benchmarks
✅ 50 untouched benchmarks
⏩ 98 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_perf_simple_pydantic_v2_field_construction 13.7 ms 11.1 ms +23.97%
⚡ test_perf_load_yaml_openapi_large 38.1 ms 34.2 ms +11.2%
⚡ test_perf_target_render[fastapi-small] 28.6 ms 25.9 ms +10.61%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing hoist-loaded-local-imports (ce7c889) with main (2ce70d2)

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 Nov 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@koxudaxi
Koudai Aono (koxudaxi) merged commit f4216c7 into main Nov 21, 2026
118 checks passed
@koxudaxi
Koudai Aono (koxudaxi) deleted the hoist-loaded-local-imports branch November 21, 2026 03:29
@github-actions

Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: The PR is a mechanical refactor that moves function-local imports, previously marked noqa PLC0415, and some TYPE_CHECKING-only imports up to module-level imports across 37 source files, and swaps from-importlib import_module for importlib.import_module calls. No public options, parameters, classes, enum values or functions are removed or renamed, since every removed line is an import that reappears at module level. Generated output, templates, defaults, error types and messages, and supported Python versions are unchanged, and the behaviour diff gate reports only both-fail cells with no accepted or violation cells, so default output behaviour does not change.


This analysis was produced by the release draft workflow

@github-actions

Copy link
Copy Markdown
Contributor

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

2 participants