Skip to content

Write the dots of a directory of the input as _ in a model's module path without --treat-dot-as-module - #6000

Merged
Koudai Aono (koxudaxi) merged 1 commit into
fix1-7from
fix1-8
Nov 2, 2026
Merged

Koudai Aono (koxudaxi) merged 1 commit into
fix1-7from
fix1-8

Conversation

@koxudaxi

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

Copy link
Copy Markdown
Member

Fixes: #5710

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of dotted directory and filename components when generating model module paths. Dots are now processed consistently with the selected dot-handling setting, while file extensions remain intact.
    • Added coverage for generated models that reference schemas in dotted directories.

… path without `--treat-dot-as-module`

A directory of the input named with a dot (`api.v2/`) was written as it was named, while the import of a model in it read
the dot as a package separator (`from .api.v2 import b`), a module that does not exist. The module path of a model
writes the dots of its directories as `_`, as it does those of its file's name, unless `--treat-dot-as-module` splits
them into packages, so the written directory (`api_v2/`), the keys of the parser's result and every import name the
same package.

Only output that failed to import changes; the stored output of `complex.directory` is written at `complex_directory`.

Fixes: #5710
@koxudaxi
Koudai Aono (koxudaxi) marked this pull request as ready for review November 2, 2026 18:13
@coderabbitai

coderabbitai Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between c7766f7 and 15629d0.

Files ignored due to path filters: 6
  • docs/llms-full.txt is excluded by none and included by none
  • tests/data/jsonschema/dotted_directory_reference/a.json is excluded by !tests/data/**/*.json and included by none
  • tests/data/jsonschema/dotted_directory_reference/a_leaf.json is excluded by !tests/data/**/*.json and included by none
  • tests/data/jsonschema/dotted_directory_reference/api.v2/b.json is excluded by !tests/data/**/*.json and included by none
  • tests/data/jsonschema/dotted_directory_reference/api.v2/b_list.json is excluded by !tests/data/**/*.json and included by none
  • tests/data/jsonschema/dotted_directory_reference/api.v2/sub.dir/c.json is excluded by !tests/data/**/*.json and included by none
Files selected for processing: 35
  • docs/architecture.md
  • src/datamodel_code_generator/model/base.py
  • tests/data/expected/main/jsonschema/main_jsonschema/dotted_directory_reference[collapse-root-models]/__init__.py
  • tests/data/expected/main/jsonschema/main_jsonschema/dotted_directory_reference[collapse-root-models]/a.py
  • tests/data/expected/main/jsonschema/main_jsonschema/dotted_directory_reference[collapse-root-models]/a_leaf.py
  • tests/data/expected/main/jsonschema/main_jsonschema/dotted_directory_reference[collapse-root-models]/api_v2/__init__.py
  • tests/data/expected/main/jsonschema/main_jsonschema/dotted_directory_reference[collapse-root-models]/api_v2/b.py
  • tests/data/expected/main/jsonschema/main_jsonschema/dotted_directory_reference[collapse-root-models]/api_v2/sub_dir/__init__.py
  • tests/data/expected/main/jsonschema/main_jsonschema/dotted_directory_reference[collapse-root-models]/api_v2/sub_dir/c.py
  • tests/data/expected/main/jsonschema/main_jsonschema/dotted_directory_reference[default]/__init__.py
  • tests/data/expected/main/jsonschema/main_jsonschema/dotted_directory_reference[default]/a.py
  • tests/data/expected/main/jsonschema/main_jsonschema/dotted_directory_reference[default]/a_leaf.py
  • tests/data/expected/main/jsonschema/main_jsonschema/dotted_directory_reference[default]/api_v2/__init__.py
  • tests/data/expected/main/jsonschema/main_jsonschema/dotted_directory_reference[default]/api_v2/b.py
  • tests/data/expected/main/jsonschema/main_jsonschema/dotted_directory_reference[default]/api_v2/b_list.py
  • tests/data/expected/main/jsonschema/main_jsonschema/dotted_directory_reference[default]/api_v2/sub_dir/__init__.py
  • tests/data/expected/main/jsonschema/main_jsonschema/dotted_directory_reference[default]/api_v2/sub_dir/c.py
  • tests/data/expected/main/jsonschema/main_jsonschema/dotted_directory_reference[treat-dot-as-module]/__init__.py
  • tests/data/expected/main/jsonschema/main_jsonschema/dotted_directory_reference[treat-dot-as-module]/a.py
  • tests/data/expected/main/jsonschema/main_jsonschema/dotted_directory_reference[treat-dot-as-module]/a_leaf.py
  • tests/data/expected/main/jsonschema/main_jsonschema/dotted_directory_reference[treat-dot-as-module]/api/__init__.py
  • tests/data/expected/main/jsonschema/main_jsonschema/dotted_directory_reference[treat-dot-as-module]/api/v2/__init__.py
  • tests/data/expected/main/jsonschema/main_jsonschema/dotted_directory_reference[treat-dot-as-module]/api/v2/b.py
  • tests/data/expected/main/jsonschema/main_jsonschema/dotted_directory_reference[treat-dot-as-module]/api/v2/b_list.py
  • tests/data/expected/main/jsonschema/main_jsonschema/dotted_directory_reference[treat-dot-as-module]/api/v2/sub/__init__.py
  • tests/data/expected/main/jsonschema/main_jsonschema/dotted_directory_reference[treat-dot-as-module]/api/v2/sub/dir/__init__.py
  • tests/data/expected/main/jsonschema/main_jsonschema/dotted_directory_reference[treat-dot-as-module]/api/v2/sub/dir/c.py
  • tests/data/expected/main/jsonschema/treat_dot_not_as_module/complex_directory/__init__.py
  • tests/data/expected/main/jsonschema/treat_dot_not_as_module/complex_directory/api_path_basic_enum.py
  • tests/data/expected/main/jsonschema/treat_dot_not_as_module/complex_directory/api_path_input.py
  • tests/data/expected/main/jsonschema/treat_dot_not_as_module/complex_directory/api_path_output.py
  • tests/data/expected/main/jsonschema/treat_dot_not_as_module/complex_directory/schema.py
  • tests/main/jsonschema/test_main_jsonschema.py
  • tests/main/jsonschema/test_main_jsonschema.refs
  • tests/main/test_payload_validation.refs

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

Walkthrough

The change updates generated module-path handling for dotted directory names. It adds regression cases for multiple dot-handling settings, expected generated models, and payload validation.

Changes: Dotted directory module paths

Layer / File(s) Summary
Module path dot handling
src/datamodel_code_generator/model/base.py, docs/architecture.md
get_module_path now sanitizes directory components unless treat_dot_as_module is true. The documentation describes the module path format.
Dotted directory regression coverage
tests/main/jsonschema/test_main_jsonschema.py, tests/main/jsonschema/test_main_jsonschema.refs, tests/data/expected/main/jsonschema/main_jsonschema/dotted_directory_reference[collapse-root-models]/*, tests/data/expected/main/jsonschema/main_jsonschema/dotted_directory_reference[default]/*, tests/data/expected/main/jsonschema/main_jsonschema/dotted_directory_reference[treat-dot-as-module]/*, tests/main/test_payload_validation.refs
Regression cases cover default behavior, explicit dot handling, and collapsed root models. Expected output includes generated models for each configuration, and payload validation references include the fixture.

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

Change: Bug fix · Severity of issue fixed: Medium

Possibly related issues

  • #5999: This change handles the dotted-directory case described in the issue; other invalid directory names remain outside this change.

Possibly related PRs

Merge Risk: Minimal · up to 15629

The change aligns dotted-directory output paths with generated imports. The suspected model-loss regression was not substantiated; mergeable after normal checks.

Pre-merge checks: Passed 5
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly describes the main change: replacing dots in directory names with underscores in model module paths when --treat-dot-as-module is not enabled. It is specific and related to the pull …
Linked Issues check Passed [#5710] Requirement: without --treat-dot-as-module, the generated import must point to a module that exists when a schema references a file in a dotted directory (for example api.v2/b.json). The impor…
Out of Scope Changes check Passed All changes connect to the dotted-directory import fix for #5710. The core change is in get_module_path (src/datamodel_code_generator/model/base.py). The docs/architecture.md edit clarifies the writte…
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Full details: Title check

Explanation

The title clearly describes the main change: replacing dots in directory names with underscores in model module paths when --treat-dot-as-module is not enabled. It is specific and related to the pull request.

Full details: Linked Issues check

Explanation

[#5710] Requirement: without --treat-dot-as-module, the generated import must point to a module that exists when a schema references a file in a dotted directory (for example api.v2/b.json). The import and the written path must agree. The PR changes get_module_path in src/datamodel_code_generator/model/base.py. When treat_dot_as_module is not True, directory components and the file stem have their dots replaced with underscores. The expected outputs under tests/data/expected/main/jsonschema/main_jsonschema/dotted_directory_reference[default]/ show the module written as api_v2/b.py and the import in a.py as .api_v2 (for example b_1.B from api_v2). Both the write path and the import now use api_v2, so import out.a resolves. The new parameterized test in tests/main/jsonschema/test_main_jsonschema.py covers default, --no-treat-dot-as-module, --treat-dot-as-module, and --collapse-root-models. The --treat-dot-as-module expected output keeps the api/v2 layout, as the issue describes. The assessment relies on the PR summary and the expected-output summaries. The ignored fixture files under tests/data/jsonschema/dotted_directory_reference/ were not inspected, per the exclusion rules. No requirement remains unmet from the summary evidence.

Full details: Out of Scope Changes check

Explanation

All changes connect to the dotted-directory import fix for #5710. The core change is in get_module_path (src/datamodel_code_generator/model/base.py). The docs/architecture.md edit clarifies the written_name and module-path behavior that this change alters. The new test, its .refs matrix entry, and the test_payload_validation.refs entry for the dotted_directory_reference/a_leaf.json fixture exercise the new behavior. The generated expected-output files cover the fixture's default, collapse-root-models, and treat-dot-as-module variants. The added generator and filename header comments in those files are produced by the generator for this fixture. No unrelated behavior change appears in the summary. The summary does not list changes to other existing expected outputs, so regressions elsewhere cannot be ruled out from this evidence alone. That uncertainty does not show an out-of-scope change.

Full details: Docstring Coverage

Explanation

Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 1 functions across 26 files. (4 skipped: 3 unsupported, 1 too large.)

Finishing Touches
Generate docstrings
  • Commit to this branch
  • Create a new PR
Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

LanguageTool
docs/architecture.md

LanguageTool checks are incomplete because the per-file request limit of 5 was reached. Remaining text was skipped; findings from completed checks are retained.

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

@chatgpt-codex-connector

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

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-02T18:14:57.295720Z 15629d0 Draft marked ready
ℹ️ 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.

@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

@codspeed

codspeed Bot commented Nov 2, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 96%

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

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

⚡ 52 improved benchmarks
⏩ 98 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_perf_client_calls[async] 47.6 ms 21.1 ms ×2.3
⚡ test_perf_client_calls[sync] 43.4 ms 19.5 ms ×2.2
⚡ test_perf_local_reference_file_resolution 149.2 ms 67.3 ms ×2.2
⚡ test_perf_simple_msgspec_unset_field_rendering 582.9 ms 269.9 ms ×2.2
⚡ test_perf_graphql_style_pydantic_v2 949.8 ms 439.9 ms ×2.2
⚡ test_perf_false_reference_validation 304.6 ms 141.4 ms ×2.2
⚡ test_perf_openapi_large 3.5 s 1.6 s ×2.1
⚡ test_perf_stripe_style_pydantic_v2 2.5 s 1.2 s ×2.1
⚡ test_perf_load_pyproject_config[mapping-None] 383 µs 179.8 µs ×2.1
⚡ test_perf_aws_style_openapi_pydantic_v2 2.4 s 1.1 s ×2.1
⚡ test_perf_kubernetes_style_pydantic_v2 3 s 1.4 s ×2.1
⚡ test_perf_load_pyproject_config[config-None] 381.4 µs 181.2 µs ×2.1
⚡ test_perf_load_pyproject_config[mapping-multiple] 395.7 µs 188.6 µs ×2.1
⚡ test_perf_inherited_required_fields[required-derived-first] 821 ms 391.5 ms ×2.1
⚡ test_perf_inherited_required_fields[required-base-first] 728.1 ms 349.1 ms ×2.1
⚡ test_perf_load_pyproject_config[config-multiple] 397.1 µs 190.7 µs ×2.1
⚡ test_perf_inherited_required_fields[partial-base-first] 1,164.4 ms 560.2 ms ×2.1
⚡ test_perf_extreme_large_schema 17.9 s 8.6 s ×2.1
⚡ test_perf_pydantic_field_name_resolution 2.7 ms 1.3 ms ×2.1
⚡ test_perf_multiple_files_input 4.1 s 2 s ×2
... ... ... ... ...

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

Tip

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


Comparing fix1-8 (15629d0) with fix1-7 (2ce62ad)2

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. ↩

  2. No successful run was found on fix1-7 (c7766f7) during the generation of this report, so 86978a3 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@codecov

codecov Bot commented Nov 2, 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 99da5f7 into main Nov 2, 2026
165 checks passed
@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: Breaking changes detected

Reasoning: The only source change is in model/base.py get_module_path. Before, the directory parts of a file path were kept as they were. Now their dots become underscores unless treat_dot_as_module is True, in which case they are kept and the dotted names split into nested packages. This renames the generated directories for any input directory with a dot in its name, in the default mode and with --no-treat-dot-as-module. Evidence: the existing treat_dot_not_as_module expected output moved from complex.directory to complex_directory with identical file contents, and the new dotted_directory_reference expectations show api.v2 written as api_v2 and imported as .api_v2. The behaviour diff gate marks the default cell for the new dotted_directory_reference input as a crash-fix, because the old generated imports pointed at a missing module. Even so, the output file tree changes for existing users, so anyone who references the old directory paths or commits the generated code is affected. I list it under Code Generation Changes. The accepted cells in the gate about integer constraints past 2 to the 53 come from unrelated changes, not this diff. No CLI options, Python API, templates, Python version support or error handling changed.

Content for Release Notes

Code Generation Changes

  • Input directories with dots in their names are now written with underscores - Unless --treat-dot-as-module is set (so in the default mode and with --no-treat-dot-as-module), every dot in an input directory's name now becomes _ in the written package, as already happened for file names. For example, api.v2/ is now written as api_v2/ and complex.directory/ as complex_directory/, and imports of their modules use the new name, such as from .api_v2 import b. Before, the output directory kept its dots, so in the default mode the generated imports pointed at .api.v2, a package that did not exist, and failed. Anyone who commits generated code, or whose scripts, sys.path entries or tooling point at the old dotted directory paths, must update them and remove the stale dotted directories. With --treat-dot-as-module, each dot still becomes a nested package as before. (Write the dots of a directory of the input as _ in a model's module path without --treat-dot-as-module #6000)

This analysis was produced by the release draft workflow

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.

Without --treat-dot-as-module, a module importing from a directory with a dot in its name fails to import

1 participant