Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

πŸ’₯ Rename regex to pattern to provide compatibility with Pydantic V2 #1231

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

Open
stickM4N wants to merge 4 commits into fastapi:main
base: main
Choose a base branch
Loading
from stickM4N:main

Conversation

Copy link

@stickM4N stickM4N commented Dec 1, 2024
edited
Loading

I faced this issue with Pydantic V2 and there is a simple workaround to it by passing the pattern Field parameter as schema extra:

Ex:

class MyClass(SQLModel)
 id: str = Field(unique=True, schema_extra={'pattern': r"^\d{5}$"})

This should be labeled as breaking
I don't know if this issue was already fixed in another branch, I saw there was a temporary PR already made but closed.

dev-chickenx reacted with heart emoji
Copy link
Author

@tiangolo @alejsdev or some other maintainer... could you please review this issue?

@svlandeg svlandeg changed the title (ε‰Šι™€) Add compatibility for Pydantic V2 regex (renamed param from regex to ... (ε‰Šι™€γ“γ“γΎγ§) (追記) πŸ’₯ Rename regex to param to provide compatibility with Pydantic V2 (θΏ½θ¨˜γ“γ“γΎγ§) Feb 20, 2025
Copy link
Member

Thanks for the PR! We're going through the backlog of PRs right now, and will get back to you once we've had the time to review this and consider the (breaking) change πŸ™

Copy link
Author

stickM4N commented Feb 22, 2025
edited
Loading

@svlandeg Nice to hear you are taking action on this matter. There is remark, the parameter the was renamed between Pydantic v1 and v2 is regex-> pattern, not as listed in the PR.

The suggestion I made in the PR was to keep both versions compatible when upgrading, but there is an alternative, which is not managing the change between them and just provide the proper type hints.

If you want me to update the PR just let me know, IDK which approach is more adequate for the project.

Copy link
Contributor

@stickM4N, could you please clarify why this should be treated as breaking change?

Apart from that, should we add a test for new parameter?

@stickM4N stickM4N changed the title (ε‰Šι™€) πŸ’₯ Rename regex to param to provide compatibility with Pydantic V2 (ε‰Šι™€γ“γ“γΎγ§) (追記) πŸ’₯ Rename regex to pattern to provide compatibility with Pydantic V2 (θΏ½θ¨˜γ“γ“γΎγ§) Aug 8, 2025
Copy link
Author

stickM4N commented Aug 8, 2025
edited
Loading

Hi @YuriiMotov, nice to hear from you again.
Sorry for not have given a better description for the problem, but here it is:

from sqlmodel import SQLModel, Field
class MyClass(SQLModel):
 id: str = Field(unique=True, pattern=r"^\d{5}$")

This code raises TypeError: Field() got an unexpected keyword argument 'pattern' which is the intended keyword for pydantic.v2.FieldInfo, differet from regex for pydantic.v1.FieldInfo
The aim of this PR is to make them compatible and have the same logic for both version sqlmodel-wise

I have other proposition that is more comfortable to work with in my opinion, but its more of a change... and its to convert schema_extra param to **schema_extra, so the input from sqlmodel.Field is more flexible, should not have impacts in other places and it add flexibility to implementations and simpler way to migrate from pydantic.Field to sqlmodel.Field

Copy link
Contributor

YuriiMotov commented Aug 23, 2025
edited
Loading

Since we still support regex parameter and just add alias (pattern) and make it working with Pydantic V2, I wouldn't call these changes braking.
So, I would remove the mention of breaking.

Also, I think we should mark regex as deprecated and prioritize pattern over regexp regardless the version of Pydantic.

Still we need to add some test:

  • pass regex and validate valid\invalid value
  • pass pattern and validate valid\invalid value
  • pass both regex and pattern and see that pattern is used
  • don't pass pattern directly, but pass schema_extra={"pattern": r"..."} (workaround that is currently being used to make it working with Pydantic V2) - it should still work

Copy link
Author

stickM4N commented Aug 28, 2025
edited
Loading

Noted @YuriiMotov, I will do that.
Here there is the reference to the removed parameter and error raise associated to it. Here is its implementation.

I would like to propose the following change:

def Field(default, *, ..., **schema_extra)

rather than

def Field(default, *, ..., schema_extra)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /