Skip to content

List lazy public names in dir() from one table per module - #7155

Merged
Koudai Aono (koxudaxi) merged 4 commits into
mainfrom
lazy-names-in-dir
Nov 18, 2026
Merged

Koudai Aono (koxudaxi) merged 4 commits into
mainfrom
lazy-names-in-dir

Conversation

@koxudaxi

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

Copy link
Copy Markdown
Member

Fixes: #7152
Fixes: #7154

Summary by CodeRabbit

  • Refactor
    • Standardized how public names are loaded on demand across the package. Available names are consistently discoverable through module listings, while unavailable names continue to raise AttributeError.
    • Existing lazy exports remain accessible, including compatibility names used by parser modules.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 18, 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-18T20:48:52.525625Z c88090a 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 18, 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 PR adds shared hooks for lazy module exports and applies them across package, CLI, parser, reference, type, and utility modules. It also adds public-API checks for lazy names, directory listings, and missing-name errors.

Changes: Shared lazy exports

Layer / File(s) Summary
Shared lazy-export hooks
src/datamodel_code_generator/_lazy_exports.py
Adds a helper that loads configured lazy entries, optionally caches results, and returns sorted, deduplicated directory listings.
Package and utility module integration
src/datamodel_code_generator/__init__.py, src/datamodel_code_generator/__main__.py, src/datamodel_code_generator/reference.py, src/datamodel_code_generator/types.py, src/datamodel_code_generator/util.py
These modules use the shared hooks for lazy exports. The reference module builds and caches __all__ without repeating lazy export names.
Parser module integration
src/datamodel_code_generator/parser/asyncapi.py, src/datamodel_code_generator/parser/avro.py, src/datamodel_code_generator/parser/base.py, src/datamodel_code_generator/parser/jsonschema.py, scripts/check_architecture_boundaries.py
Parser modules use the shared hooks for lazy exports. The architecture-boundary allowlist names the dataclass and msgspec loader functions; the typed-dict entry remains keyed by __getattr__.
Public-API checks
tests/main/test_public_api_signature_baseline.py, tests/main/test_public_api_signature_baseline.refs
Adds subprocess and in-process checks for lazy names, dir(), __all__, and missing-name errors, with expected-output references.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix · Severity of issue fixed: Low

Possibly related PRs

Merge Risk: Minimal · previous review c8809

This change makes dir() for modules with lazy exports list each public name once, using the same table as attribute lookup. No merge-blocking risk was identified.

Pre-merge checks: Passed 3 · Inconclusive 1

Failed Failed checks: 1 inconclusive

Check name Status Explanation Resolution
Linked Issues check Inconclusive [#7152] The shared module_hooks __dir__ lists each name once. reference.py __all__ returns public non-lazy names followed by lazy exports, so a name that is stored in globals is not listed twi… Confirm, from the diff against merge base d19bc4f, that the missing-attribute messages of datamodel_code_generator, util, types, parser.avro, parser.asyncapi, parser.base, parser.jsonschema, `r…
Passed 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 "List lazy public names in dir() from one table per module" matches the main change. The raw summary shows a new shared module_hooks helper in _lazy_exports.py that answers __dir__ fro…
Out of Scope Changes check Passed The changes support the two linked issues. The new _lazy_exports.py helper provides the shared table, __getattr__, and __dir__ used by each affected module. The per-module edits in __init__.py…
Full details: Title check

Explanation

The title "List lazy public names in dir() from one table per module" matches the main change. The raw summary shows a new shared module_hooks helper in _lazy_exports.py that answers __dir__ from the same table used by __getattr__, and that is applied across the affected modules. The linked objectives (#7152 and #7154) target listing each lazy public name once in dir(), which the title describes. The title is specific and concise, and it names the primary change.

Full details: Out of Scope Changes check

Explanation

The changes support the two linked issues. The new _lazy_exports.py helper provides the shared table, __getattr__, and __dir__ used by each affected module. The per-module edits in __init__.py, __main__.py, parser/*.py, reference.py, types.py, and util.py replace the old hand-written hooks with that helper. The scripts/check_architecture_boundaries.py allowlist keys were updated to match the renamed parser/base.py hooks. This is a supporting change. The tests in tests/main/test_public_api_signature_baseline.py and the .refs file cover the new dir and __all__ behavior. The docs migration TOML files document the change. No unrelated functional change appears in the summary.

Full details: Linked Issues check

Explanation

[#7152] The shared module_hooks __dir__ lists each name once. reference.py __all__ returns public non-lazy names followed by lazy exports, so a name that is stored in globals is not listed twice. This addresses the duplicate behavior. [#7154] The package root, types, util, parser.avro, parser.asyncapi, parser.base, parser.jsonschema, and __main__ now answer dir() from the same table as __getattr__. _types is not changed. The objective requires each module's AttributeError text to remain unchanged. The summary says the util module's explicit missing-attribute error was removed and that module_hooks has a missing_message option (standard or name). The summary does not show the old and new message text for util and the package root. The expected baseline files are ignored and were not inspected. Compliance with the message requirement cannot be confirmed from the summary.

Resolution

Confirm, from the diff against merge base d19bc4f, that the missing-attribute messages of datamodel_code_generator, util, types, parser.avro, parser.asyncapi, parser.base, parser.jsonschema, reference, and __main__ match their pre-change text. Specifically, check the util module and the package root, where the old custom error was replaced by the shared hook. Also confirm that the missing_message value passed for each module matches the old message format.

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 d19bc4f and c88090a.

Files ignored due to path filters: 12

  • docs/data/migration/unreleased/boundary-report-lazy-export-lines.toml is excluded by none and included by none
  • docs/data/migration/unreleased/lazy-public-names-in-dir.toml is excluded by none and included by none
  • tests/data/expected/architecture_boundaries/cli.txt is excluded by !tests/data/**/*.txt and included by none
  • tests/data/expected/main/public_api_signature_baseline/lazy_public_names_are_listed_once_by_dir[datamodel_code_generator.__main__].txt is excluded by !tests/data/**/*.txt and included by none
  • tests/data/expected/main/public_api_signature_baseline/lazy_public_names_are_listed_once_by_dir[datamodel_code_generator.parser.asyncapi].txt is excluded by !tests/data/**/*.txt and included by none
  • tests/data/expected/main/public_api_signature_baseline/lazy_public_names_are_listed_once_by_dir[datamodel_code_generator.parser.avro].txt is excluded by !tests/data/**/*.txt and included by none
  • tests/data/expected/main/public_api_signature_baseline/lazy_public_names_are_listed_once_by_dir[datamodel_code_generator.parser.base].txt is excluded by !tests/data/**/*.txt and included by none
  • tests/data/expected/main/public_api_signature_baseline/lazy_public_names_are_listed_once_by_dir[datamodel_code_generator.parser.jsonschema].txt is excluded by !tests/data/**/*.txt and included by none
  • tests/data/expected/main/public_api_signature_baseline/lazy_public_names_are_listed_once_by_dir[datamodel_code_generator.reference].txt is excluded by !tests/data/**/*.txt and included by none
  • tests/data/expected/main/public_api_signature_baseline/lazy_public_names_are_listed_once_by_dir[datamodel_code_generator.types].txt is excluded by !tests/data/**/*.txt and included by none
  • tests/data/expected/main/public_api_signature_baseline/lazy_public_names_are_listed_once_by_dir[datamodel_code_generator.util].txt is excluded by !tests/data/**/*.txt and included by none
  • tests/data/expected/main/public_api_signature_baseline/lazy_public_names_are_listed_once_by_dir[datamodel_code_generator].txt is excluded by !tests/data/**/*.txt and included by none
Files selected for processing: 13

  • scripts/check_architecture_boundaries.py
  • src/datamodel_code_generator/__init__.py
  • src/datamodel_code_generator/__main__.py
  • src/datamodel_code_generator/_lazy_exports.py
  • src/datamodel_code_generator/parser/asyncapi.py
  • src/datamodel_code_generator/parser/avro.py
  • src/datamodel_code_generator/parser/base.py
  • src/datamodel_code_generator/parser/jsonschema.py
  • src/datamodel_code_generator/reference.py
  • src/datamodel_code_generator/types.py
  • src/datamodel_code_generator/util.py
  • tests/main/test_public_api_signature_baseline.py
  • tests/main/test_public_api_signature_baseline.refs

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

@github-actions

Copy link
Copy Markdown
Contributor

@codspeed

codspeed Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Merging this PR will degrade performance by 37.43%

2 warnings

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

⚠️ 53 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

❌ 53 regressed benchmarks
⏩ 98 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ test_perf_simple_msgspec_unset_field_rendering 345.7 ms 617.5 ms -44.02%
❌ test_perf_client_calls[async] 28.4 ms 50.7 ms -44.01%
❌ test_perf_large_models_dataclass_builtin 1.1 s 1.9 s -43.9%
❌ test_perf_large_models_typed_dict[legacy-requiredness] 912.6 ms 1,625.8 ms -43.86%
❌ test_perf_large_models_msgspec 1.8 s 3.2 s -43.16%
❌ test_perf_client_calls[sync] 25.4 ms 44.5 ms -43%
❌ test_perf_unique_items_collapsed_builtin 210 ms 367.6 ms -42.87%
❌ test_perf_openapi_large_pydantic_v2_builtin 1.1 s 1.9 s -42.85%
❌ test_perf_large_models_pydantic_v2_noformat 1.1 s 1.9 s -42.53%
❌ test_perf_large_models_typed_dict[total-false-requiredness] 911.5 ms 1,569.9 ms -41.94%
❌ test_perf_large_models_pydantic_v2_builtin 1.1 s 1.9 s -41.9%
❌ test_perf_load_pyproject_config[mapping-None] 167.5 µs 286 µs -41.43%
❌ test_perf_scalar_root_collapsed_builtin 162.6 ms 276.3 ms -41.14%
❌ test_perf_large_models_pydantic_v2_builtin_double_quotes 1.2 s 2 s -41.01%
❌ test_perf_load_pyproject_config[config-None] 164.7 µs 278.3 µs -40.83%
❌ test_perf_large_models_single_module_stdout 1.3 s 2.1 s -40.54%
❌ test_perf_load_pyproject_config[config-multiple] 178.5 µs 300.1 µs -40.51%
❌ test_perf_simple_pydantic_v2_field_construction 9.3 ms 15.6 ms -40.34%
❌ test_perf_target_render[fastapi-large] 7.7 s 12.8 s -40.16%
❌ test_perf_unique_items_schema_validators 276.5 ms 460.3 ms -39.93%
... ... ... ... ...

ℹ️ Only the first 20 benchmarks are displayed. Go to the app to view all benchmarks.

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing lazy-names-in-dir (4cfc1b6) with main (32c4a53)

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 18, 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 405edcb into main Nov 18, 2026
216 of 219 checks passed
@koxudaxi
Koudai Aono (koxudaxi) deleted the lazy-names-in-dir branch November 18, 2026 21:17
@github-actions

Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: Breaking changes detected

Reasoning: The PR moves every module-level lazy getattr onto a shared internal helper that also supplies dir. Missing-name error messages and caching stay the same in every module: the root package and reference still cache, and avro, base and jsonschema still raise with the name alone. The behaviour diff gate has only both-fail cells, so generated code and default output do not change. Two Python API changes reach released behaviour. First, the diff removes the plain imports of create_module_getattr in types.py and parser/asyncapi.py, so the function can no longer be imported from those modules or picked up by star imports of them. It remains available from util. Second, dir() on the root package, types, util, parser.avro, parser.asyncapi, parser.base, parser.jsonschema and main now lists the lazily exported names, so introspection through inspect.getmembers, help or completion now triggers the imports behind those names. The fix that stops reference listing a name twice in dir() and all is a correction to the same introspection behaviour. The other changes are internal or test-only: renaming the private table, updating the architecture-boundary allowlist, and the stored test outputs.

Content for Release Notes

API/CLI Changes

  • Removed create_module_getattr from datamodel_code_generator.types and datamodel_code_generator.parser.asyncapi - Both modules used to import create_module_getattr from datamodel_code_generator.util for their old lazy __getattr__ hooks. That meant from datamodel_code_generator.types import create_module_getattr, the same import from datamodel_code_generator.parser.asyncapi, and star imports of either module all gave you that name. Those imports are gone because the modules now use the internal _lazy_exports helper, so code that reached the function through either module gets ImportError or AttributeError. Import it from datamodel_code_generator.util, where it is still available. (List lazy public names in dir() from one table per module #7155)
  • Module dir() now lists lazily exported names - The package root, types, util, parser.avro, parser.asyncapi, parser.base, parser.jsonschema and __main__ now define __dir__, so dir() now includes lazy names such as StrictTypes, BaseModel, SafeLoader, dataclass_model, msgspec_model, TypedDictModel, Config, generate_dynamic_models and load_pyproject_config, which it used to leave out. Anything that walks dir() and reads each attribute, such as inspect.getmembers(), help() and REPL completion, now imports the modules behind those names. For example, it loads the dataclass and msgspec model modules for parser.base and the protobuf and XML Schema parsers for parser.asyncapi. Also, once a resolver name in datamodel_code_generator.reference has been read, dir() and __all__ now list it once instead of twice. (List lazy public names in dir() from one table per module #7155)

This analysis was produced by the release draft workflow

@github-actions

Copy link
Copy Markdown
Contributor

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

1 participant