Skip to content

Add ascii_only option to StringConstraints#12907

Merged
Viicos merged 5 commits intopydantic:mainfrom
ai-man-codes:feature/string-ascii-only-constraint
Mar 16, 2026
Merged

Add ascii_only option to StringConstraints#12907
Viicos merged 5 commits intopydantic:mainfrom
ai-man-codes:feature/string-ascii-only-constraint

Conversation

@ai-man-codes
Copy link
Copy Markdown
Contributor

@ai-man-codes ai-man-codes commented Mar 10, 2026

feat: add ascii_only constraint to StringConstraints

Change Summary

Implements the ascii_only option for StringConstraints as requested in #12300.

Previously, developers had to use an unintuitive regex pattern to enforce ASCII-only strings:

username: Annotated[str, StringConstraints(pattern=r'^[\x00-\x7F]*$')]

This PR adds a clean boolean flag instead:

username: Annotated[str, StringConstraints(ascii_only=True)]

Changes made:

  • pydantic-core: Added ascii_only field to StrConstrainedValidator, implemented the check using Rust's native .is_ascii() before pattern matching for performance, and registered a new StringNotAscii error type
  • pydantic: Exposed ascii_only parameter in StringConstraints, registered it in _known_annotated_metadata.py, and mapped it to the ASCII regex pattern in JSON Schema output
  • docs: Added string_not_ascii section to validation_errors.md

Related issue number

Fixes #12300

Checklist

  • The pull request title is a good summary of the changes - it will be used in the changelog
  • Unit tests for the changes exist
  • Tests pass on CI
  • Documentation reflects the changes where applicable
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

Selected Reviewer: @Viicos

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Mar 10, 2026

Merging this PR will improve performance by 5.19%

⚡ 1 improved benchmark
✅ 211 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
test_list_of_strs_json_uncached 441.7 µs 419.9 µs +5.19%

Comparing ai-man-codes:feature/string-ascii-only-constraint (5943840) with main (4b92ae3)

Open in CodSpeed

@github-actions
Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  pydantic
  types.py
  pydantic/_internal
  _known_annotated_metadata.py
Project Total  

This report was generated by python-coverage-comment-action

@ai-man-codes
Copy link
Copy Markdown
Contributor Author

@davidolrik what do you think about this implementation, can you please review this PR ? Thank you

Copy link
Copy Markdown
Member

@Viicos Viicos left a comment

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please remove

Comment thread tests/test_types.py Outdated
@pydantic-hooky pydantic-hooky Bot added awaiting author revision awaiting changes from the PR author and removed ready for review labels Mar 10, 2026
@pydantic-hooky pydantic-hooky Bot assigned ai-man-codes and unassigned Viicos Mar 10, 2026
@Viicos Viicos changed the title Feature/string ascii only constraint Add ascii_only option to StringConstraints Mar 10, 2026
Comment thread docs/api/standard_library_types.md Outdated
@davidolrik
Copy link
Copy Markdown
Contributor

@davidolrik what do you think about this implementation, can you please review this PR ? Thank you

I think you ment to highlight @davidhewitt and not me 😊

@ai-man-codes
Copy link
Copy Markdown
Contributor Author

ai-man-codes commented Mar 10, 2026

I think you ment to highlight @davidhewitt and not me 😊

yeah, I wanted to highlight @davidhewitt. Sorry for the misunderstanding.

@pydantic-hooky pydantic-hooky Bot added ready for review and removed awaiting author revision awaiting changes from the PR author labels Mar 10, 2026
@pydantic-hooky pydantic-hooky Bot assigned Viicos and unassigned ai-man-codes Mar 10, 2026
@ai-man-codes
Copy link
Copy Markdown
Contributor Author

ai-man-codes commented Mar 14, 2026

@Viicos are there any more changes to be done ?

@Viicos Viicos merged commit fbf9c6e into pydantic:main Mar 16, 2026
81 checks passed
@Viicos Viicos added the needs-blogpost-entry This PR needs to be documented in the release notes blog post label Mar 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-blogpost-entry This PR needs to be documented in the release notes blog post ready for review relnotes-feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add ascii_only option to StringConstraints

4 participants