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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion sqlmodel/main.py
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ def Field(
max_length: Optional[int] = None,
allow_mutation: bool = True,
regex: Optional[str] = None,
pattern: Optional[str] = None,
discriminator: Optional[str] = None,
repr: bool = True,
primary_key: Union[bool, UndefinedType] = Undefined,
Expand Down Expand Up @@ -285,6 +286,7 @@ def Field(
max_length: Optional[int] = None,
allow_mutation: bool = True,
regex: Optional[str] = None,
pattern: Optional[str] = None,
discriminator: Optional[str] = None,
repr: bool = True,
primary_key: Union[bool, UndefinedType] = Undefined,
Expand Down Expand Up @@ -339,6 +341,7 @@ def Field(
max_length: Optional[int] = None,
allow_mutation: bool = True,
regex: Optional[str] = None,
pattern: Optional[str] = None,
discriminator: Optional[str] = None,
repr: bool = True,
sa_column: Union[Column, UndefinedType] = Undefined, # type: ignore
Expand Down Expand Up @@ -374,6 +377,7 @@ def Field(
max_length: Optional[int] = None,
allow_mutation: bool = True,
regex: Optional[str] = None,
pattern: Optional[str] = None,
discriminator: Optional[str] = None,
repr: bool = True,
primary_key: Union[bool, UndefinedType] = Undefined,
Expand All @@ -389,6 +393,12 @@ def Field(
schema_extra: Optional[Dict[str, Any]] = None,
) -> Any:
current_schema_extra = schema_extra or {}

if IS_PYDANTIC_V2:
current_schema_extra.update(pattern=pattern or regex)
else:
current_schema_extra.update(regex=regex or pattern)

field_info = FieldInfo(
default,
default_factory=default_factory,
Expand All @@ -411,7 +421,6 @@ def Field(
min_length=min_length,
max_length=max_length,
allow_mutation=allow_mutation,
regex=regex,
discriminator=discriminator,
repr=repr,
primary_key=primary_key,
Expand Down
15 changes: 15 additions & 0 deletions tests/test_pydantic/test_field.py
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,18 @@ class Model(SQLModel):

instance = Model(id=123, foo="bar")
assert "foo=" not in repr(instance)


@pytest.mark.parametrize("param", ["regex", "pattern"])
def test_field_regex_param(param: str):
class DateModel(SQLModel):
date_1: str = Field(**{param: r"^\d{2}-\d{2}-\d{4}$"})
date_2: str = Field(schema_extra={param: r"^\d{2}-\d{2}-\d{4}$"})

DateModel(date_1="12-31-2024", date_2="12-31-2024")
# Validates correctly

with pytest.raises(ValidationError):
DateModel(date_1="2024年12月31日", date_2="2024年12月31日")
# This will raise a ValueError due to regex mismatch
raise AssertionError("Expected ValidationError for invalid date format")

AltStyle によって変換されたページ (->オリジナル) /