You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Keep a regex holding a quote unchanged in the pattern literal - #6790
Generated Pydantic v2 constraints now preserve regex patterns containing quotes, including escaped apostrophes, without changing the pattern’s meaning.
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.
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
Pydantic v2 regex rendering now selects a raw-string delimiter that is absent from the pattern, or falls back to a regular string when needed. Generated-output expectations and regression coverage are updated for patterns containing quotes.
Changes: Pydantic v2 regex pattern literals
Layer / File(s)
Summary
Raw literal generation and regex rendering src/datamodel_code_generator/_python_syntax.py, src/datamodel_code_generator/model/pydantic_v2/types.py
The syntax helper supports raw literals with single or double quotes. Pydantic v2 regex rendering uses the helper and falls back to a regular string when control-character escaping would change the regex or the pattern cannot be represented as a raw literal.
Generated pattern literal spellings are updated, and the JSON Schema property-name pattern test and payload-validation references are added. The allowlist records the escaped-quote pattern difference.
This change keeps quoted regex patterns unchanged in generated Pydantic output, and no code defect was found. The Behaviour diff gate reported a violation for the new quoted-pattern fixture even though the new allowlist entry appears to cover it. Confirm that the gate passes on the latest commit before merging.
Pre-merge checks: 4
Check name
Status
Explanation
Description Check
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
The title states that a regex containing a quote is kept unchanged in the pattern literal. The raw summary confirms this is the main change: _get_regex_literal now renders patterns with `raw_literal…
Linked Issues check
Objective [#6789]: a JSON Schema pattern containing a single quote must be emitted unchanged in the generated pattern literal. Evidence from the PR summary: _python_syntax.py replaces `raw_single_qu…
Out of Scope Changes check
All changed files relate to the #6789 pattern-literal fix. The source changes are in _python_syntax.py and model/pydantic_v2/types.py. The expected snapshots and test files cover the new rendering…
Full details: Title check
Explanation
The title states that a regex containing a quote is kept unchanged in the pattern literal. The raw summary confirms this is the main change: _get_regex_literal now renders patterns with raw_literal, which picks a usable quote, so the pattern is no longer escaped. The linked issue #6789 asks for the same behaviour. The title is specific and accurate, so it passes.
Full details: Linked Issues check
Explanation
Objective [#6789]: a JSON Schema pattern containing a single quote must be emitted unchanged in the generated pattern literal. Evidence from the PR summary: _python_syntax.py replaces raw_single_quoted with raw_literal, which selects the first quote character absent from the escaped text. types.py_get_regex_literal uses raw_literal. For ^[a-z'*]+$, the single quote is no longer escaped, so the output is a raw literal with the pattern unchanged, not r'^[a-z\'*]+$'. Snapshot updates (regex_literal_text, pattern_escaped_quote.py, pattern.py) show the same regex value in the new literal forms. jsonschema_property_names_pattern_quotes.py and its test cover single, double, and mixed quote keys. The fixture tests/data/jsonschema/property_names_pattern_quotes.json is in ignored_files, so the test input was not inspected. The expected output is reviewable and supports the behavior. The stack members (#6775, #6776, #6777, #6781) are not needed for this objective. Their stated changes concern enum merging and do not touch pattern literal rendering. Stack roll-up: the objective is Addressed by the current PR (#6790) alone. Residual uncertainty: the excluded fixture input cannot be checked directly, and the PR reports no test run results.
Full details: Out of Scope Changes check
Explanation
All changed files relate to the #6789 pattern-literal fix. The source changes are in _python_syntax.py and model/pydantic_v2/types.py. The expected snapshots and test files cover the new rendering and its regression test. scripts/behaviour_diff_allowlist.toml adds an accepted default-output change for patterns with an escaped single quote, citing owner decision #6789. This entry records the rendering change for the behaviour-diff check, so it is connected to the objective. It is a suppression entry, so reviewers should confirm it does not hide unrelated regressions. The .refs manifest updates register the new test and fixture. No unrelated behavior change was identified in the summary. Excluded paths were not inspected.
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.
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
⚠️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.
Reasoning: The PR changes how the Pydantic v2 type manager writes regex pattern literals. raw_single_quoted becomes raw_literal: it picks a quote the pattern does not contain and no longer backslash-escapes a single quote, and it falls back to a plain literal when the pattern holds both quotes. The behaviour diff gate marks the pattern.json, openapi pattern.yaml, regex_literal_text.json and property_names_pattern_quotes.json cells as accepted constraint changes under default options, so this changes default generated output. Validation stays the same under Python re, but the stored pattern string and the pattern that model_json_schema publishes now lose the extra backslash, and the source text of the generated fields changes, so it is reported as a code generation change. The removed and renamed helpers in the private _python_syntax module, and the narrowed value of the module-level escape_characters in model.pydantic_v2.types, are internal implementation details and not reported separately. The large edit to behaviour_diff_allowlist.toml is workflow configuration, and the docs and test changes only reflect the new output. There are no CLI option, template, Python version or error handling changes.
Content for Release Notes
Code Generation Changes
Regex patterns containing quotes are written differently in Pydantic v2 output - With pydantic_v2.BaseModel and pydantic_v2.dataclass output, a pattern containing a single quote used to be written as a single-quoted raw literal with the quote escaped by a backslash, for example constr(pattern=r'^it\'s$'). Because raw literals keep backslashes, the runtime pattern string had an extra backslash before the quote. The generator now picks a quote character the pattern does not use, so this becomes constr(pattern=r"^it's$"). A pattern containing both quote characters is now written as a plain (non-raw) literal, for example '^a-z\'"+$' instead of r'^a-z\'"+$', and backslashes in such patterns are doubled. A pattern that already holds a backslash before a quote changes only in source text: "^don\\'t$" becomes r"^don\'t$". This affects string field patterns, propertyNames key patterns, and StringConstraints written under --field-constraints or --use-annotated. Python's re matches the same values, so validation does not change. However, the pattern stored in constr or StringConstraints, and the pattern that model_json_schema() publishes (for example in FastAPI OpenAPI docs), no longer has the extra backslash. The generated source text for these fields also changes, so snapshot tests or committed generated code will show differences after regenerating (Keep a regex holding a quote unchanged in the pattern literal #6790)
This analysis was produced by the release draft workflow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes: #6789
Summary by CodeRabbit