Skip to content

Keep generate string input inline#3573

Merged
koxudaxi merged 2 commits into
mainfrom
fix-generate-string-input-inline
Jul 6, 2026
Merged

Keep generate string input inline#3573
koxudaxi merged 2 commits into
mainfrom
fix-generate-string-input-inline

Conversation

@koxudaxi

@koxudaxi koxudaxi commented Jul 4, 2026

Copy link
Copy Markdown
Owner

Fixes: #2381

Summary by CodeRabbit

  • Documentation

    • Added/updated guidance for reading schema input from a file path.
    • Refreshed the multiple-module output example for clearer alignment.
  • Bug Fixes

    • Improved input handling for strings that resemble file paths: they’re treated as inline schema text unless a path input is explicitly provided.
    • Added clearer warning behavior to help distinguish “string is schema text” vs “string points to an existing file” scenarios when parsing fails.
  • Tests

    • Expanded coverage for Path vs string input behavior, including warning and failure cases.

@coderabbitai

coderabbitai Bot commented Jul 4, 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: 543ba62b-0b0b-45e1-b7a4-5d5c9754d188

📥 Commits

Reviewing files that changed from the base of the PR and between 697dc00 and 957b014.

⛔ Files ignored due to path filters (1)
  • docs/llms-full.txt is excluded by none and included by none
📒 Files selected for processing (3)
  • docs/using_as_module.md
  • src/datamodel_code_generator/__init__.py
  • tests/main/test_main_general.py
✅ Files skipped from review due to trivial changes (1)
  • docs/using_as_module.md
🚧 Files skipped from review as they are similar to previous changes (2)
  • tests/main/test_main_general.py
  • src/datamodel_code_generator/init.py

📝 Walkthrough

Walkthrough

This PR changes generate() string-input handling so existing-path strings are no longer coerced to Path. It adds warning emission on failure paths, updates docs for file-based input, and refreshes tests and expected outputs.

Changes

Path string input handling

Layer / File(s) Summary
Remove coercion, add warning helper
src/datamodel_code_generator/__init__.py
Removes the existing-path coercion mechanism, adds helpers for detecting existing-path strings and emitting warnings, and updates the generate() input docstring.
Wire warning helper into generate()
src/datamodel_code_generator/__init__.py
Wraps input-type inference, raw input normalization, MCP-tools conversion, parser construction, and parser parsing so failures trigger the warning helper; parser cleanup now runs before re-raising parse errors.
Docs, tests, and golden outputs
docs/using_as_module.md, tests/main/test_main_general.py, tests/data/expected/main/*
Adds documentation for file-based Path input, revises the module-output example, rewrites generate() input-handling tests, and updates generated expected outputs for the new cases.

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

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant generate
  participant Parser

  Caller->>generate: generate(input_=string)
  generate->>generate: infer / normalize / build parser
  alt failure before parse
    generate->>generate: warn if string resolves to existing path
    generate-->>Caller: re-raise original error
  else parser.parse() fails
    generate->>Parser: parser.parse(...)
    Parser-->>generate: raise error
    generate->>Parser: parser._dispose()
    generate-->>Caller: re-raise original error
  end
Loading

Possibly related PRs

Suggested labels: bug, tests, documentation

Suggested reviewers: koxudaxi

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning The change treats existing string paths as schema text instead of reading them as files, so it does not satisfy #2381. Update generate() to read existing string path inputs as files, preserve direct schema strings, and re-run the path-input tests.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is concise and directly reflects the change around string input handling.
Out of Scope Changes check ✅ Passed The docs, generated fixtures, and tests all support the input-handling change, with no unrelated feature work visible.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ 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 fix-generate-string-input-inline

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.

@github-actions

github-actions Bot commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

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

@codspeed-hq

codspeed-hq Bot commented Jul 4, 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 fix-generate-string-input-inline (957b014) with main (6147628)

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 Jul 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (6147628) to head (957b014).

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #3573   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          163       163           
  Lines        34977     35010   +33     
  Branches      4047      4047           
=========================================
+ Hits         34977     35010   +33     
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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (1)
src/datamodel_code_generator/__init__.py (1)

1456-1456: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Repeated warn+re-raise boilerplate across 5 call sites.

The except Exception: _warn_if_input_string_points_to_existing_path(input_); raise pattern is duplicated near-verbatim at each failure point (type inference, normalization, MCP conversion, parser construction, parse). Consider a small context manager to consolidate this and reduce drift risk if the pattern needs to change later.

♻️ Example consolidation
+@contextlib.contextmanager
+def _warn_on_string_path_failure(input_: Path | str | ParseResult | Mapping[str, Any] | list[Any]) -> Iterator[None]:
+    try:
+        yield
+    except Exception:
+        _warn_if_input_string_points_to_existing_path(input_)
+        raise

Then wrap each block, e.g.:

-    try:
-        input_text = _normalize_raw_input(input_, input_text, input_file_type, config)
-    except Exception:
-        _warn_if_input_string_points_to_existing_path(input_)
-        raise
+    with _warn_on_string_path_failure(input_):
+        input_text = _normalize_raw_input(input_, input_text, input_file_type, config)

Also applies to: 1468-1484, 1505-1520, 1532-1536

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/datamodel_code_generator/__init__.py` at line 1456, The same
warn-and-reraise pattern is duplicated across the input handling flow in
__init__.py, so consolidate it into a small reusable helper or context manager.
Update the failure paths around the type inference, normalization, MCP
conversion, parser construction, and parse logic to use that shared wrapper
instead of repeating try/except with
_warn_if_input_string_points_to_existing_path(input_) and raise, keeping the
existing warning behavior via the same function names.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@docs/using_as_module.md`:
- Around line 47-48: The documentation for input_ needs to reflect the full
string-handling behavior, not just that strings are schema text. Update the
wording in the module usage section to clarify that Path should be used for file
input, while string values are interpreted as schema text unless they point to
an existing path, in which case the code handles them specially and may warn or
recommend Path. Make sure the text around input_ accurately distinguishes Path,
schema text, and existing-path strings using the input_ parameter description as
the anchor.

In `@src/datamodel_code_generator/__init__.py`:
- Around line 762-784: The warning in
_warn_if_input_string_points_to_existing_path can be promoted to an exception
under strict warning filters and mask the original failure in generate(). Make
the warning emission safe in that helper by guarding it so it cannot replace an
active exception (for example, avoid raising from warnings.warn when called
during error handling), and ensure the existing InvalidFileFormatError/parser
exception remains the one propagated.

---

Nitpick comments:
In `@src/datamodel_code_generator/__init__.py`:
- Line 1456: The same warn-and-reraise pattern is duplicated across the input
handling flow in __init__.py, so consolidate it into a small reusable helper or
context manager. Update the failure paths around the type inference,
normalization, MCP conversion, parser construction, and parse logic to use that
shared wrapper instead of repeating try/except with
_warn_if_input_string_points_to_existing_path(input_) and raise, keeping the
existing warning behavior via the same function names.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 5f7c25a1-ea20-4027-aae4-7cc60764ed75

📥 Commits

Reviewing files that changed from the base of the PR and between 6147628 and 697dc00.

⛔ Files ignored due to path filters (1)
  • docs/llms-full.txt is excluded by none and included by none
📒 Files selected for processing (6)
  • docs/using_as_module.md
  • src/datamodel_code_generator/__init__.py
  • tests/data/expected/main/generate_accepts_path_input.py
  • tests/data/expected/main/generate_keeps_existing_path_string_input.py
  • tests/data/expected/main/generate_keeps_non_path_string_input.py
  • tests/main/test_main_general.py

Comment thread docs/using_as_module.md Outdated
Comment thread src/datamodel_code_generator/__init__.py Outdated
@koxudaxi koxudaxi merged commit 430582a into main Jul 6, 2026
62 checks passed
@koxudaxi koxudaxi deleted the fix-generate-string-input-inline branch July 6, 2026 06:50
@github-actions

github-actions Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: This PR reworks how generate() handles string input_: it removes the private, unreleased _coerce_existing_path_input() helper (and _SCHEMA_TEXT_START_CHARS) that silently read a string as a file when it resolved to an existing path, replacing it with always-treat-string-as-schema-text plus a failure-only UserWarning suggesting Path. Although this is a behavioral change to the string-path handling, the coercion behavior it modifies was introduced in commit 730b494 (#3561), which git tag --contains shows is not part of any release tag (latest release is 0.67.0). Both the introduction and this modification occur within the same unreleased development cycle, so no released user could have depended on the old behavior. The public generate() signature is unchanged (no parameters/classes/enums added or removed in the diff), the removed symbols are private, no Python versions are dropped, no templates change, and the original Error is still raised on failure. The only removed lines are private internals and test-fixture formatting, so there is no user-facing breaking change.


This analysis was performed by Claude Code Action

@github-actions

github-actions Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

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

Bug: Path Handling Error - Schema File Paths as Strings - Pydantic

1 participant