Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support re.Pattern[str] to pattern field #9053

Merged
merged 2 commits into from Mar 25, 2024

Conversation

jag-k
Copy link
Contributor

@jag-k jag-k commented Mar 19, 2024

Change Summary

I think it just a simple idea about using already compiled regexp to set in pattern field.

Example:

import re

from pydantic import BaseModel, Field

TOKEN_RE = re.compile(r"glpat-.{20}")


class ModelWithToken(BaseModel):
    """Token model with secret."""

    token: SecretStr = Field(pattern=TOKEN_RE)  # Instead of `pattern=TOKEN_RE.pattern`

Related issue number

fix #9052

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 — I don't think we need docs for this
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

Selected Reviewer: @davidhewitt

Copy link

codspeed-hq bot commented Mar 19, 2024

CodSpeed Performance Report

Merging #9053 will not alter performance

Comparing jag-k:feat-pattern-as-pattern (6e96b8d) with main (ac9e6ee)

Summary

✅ 10 untouched benchmarks

@jag-k
Copy link
Contributor Author

jag-k commented Mar 19, 2024

please review

Copy link
Member

@sydney-runkle sydney-runkle left a comment

Choose a reason for hiding this comment

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

Looks great, would be helpful to add some tests! Thanks!

@sydney-runkle
Copy link
Member

@jag-k, ping!

@jag-k
Copy link
Contributor Author

jag-k commented Mar 25, 2024

@jag-k, ping!

@sydney-runkle Sorry to keep you waiting. I missed your message a bit in the mail.

I added tests. Tell me, is everything okay with them?

Copy link
Member

@sydney-runkle sydney-runkle left a comment

Choose a reason for hiding this comment

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

Looks great, thanks for your work on this!

@sydney-runkle sydney-runkle merged commit 00c7815 into pydantic:main Mar 25, 2024
53 checks passed
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.

Add support re.Pattern[str] to pattern field in Field
3 participants