Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
6afa967
Add compatibility for Pydantic V2 regex (renamed param from regex to …
stickm4n Dec 1, 2024
e19734f
Merge branch 'fastapi:main' into main
stickm4n Feb 22, 2025
2d6bb87
Merge branch 'main' into main
svlandeg Feb 24, 2025
5de3c0b
Add test for field regex/pattern parameter
stickm4n Aug 30, 2025
926bd78
Split test case with `VaidationError` into two
YuriiMotov Oct 7, 2025
f45a40f
Fix failing test
YuriiMotov Oct 7, 2025
a26b384
Merge branch 'main' into main
YuriiMotov Oct 7, 2025
2b6de42
Merge branch 'main' into main
svlandeg Oct 8, 2025
71cb27d
Merge branch 'fastapi:main' into main
stickm4n Oct 11, 2025
91e8e7e
Remove unused ignore comment
stickm4n Oct 12, 2025
8e9105b
Merge remote-tracking branch 'upstream/main' into stickM4N_main
YuriiMotov Jan 28, 2026
57c6c53
Deprecate `regex` parameter in favor of `pattern`
YuriiMotov Jan 28, 2026
01b3954
Add warning when `pattern` is passed via `schema_extra`
YuriiMotov Jan 28, 2026
570be83
Rename tests
YuriiMotov Jan 28, 2026
2b342da
Update deprecations to be consistent with other PRs
YuriiMotov Jan 28, 2026
6eda60a
Merge branch 'main' into main
YuriiMotov Feb 21, 2026
879d4b8
Fix `discriminator` type annotation
YuriiMotov Feb 21, 2026
7ecd621
Merge branch 'main' into main
YuriiMotov Apr 3, 2026
1acc4b9
Update type hints for regex and pattern parameters to support `re.Pat…
stickm4n Apr 5, 2026
98a52b1
Remove support for `re.Pattern` in regex parameter type hints
stickm4n Apr 5, 2026
cfc88f3
Add `re` import in main.py
stickm4n Apr 5, 2026
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
43 changes: 38 additions & 5 deletions sqlmodel/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import builtins
import ipaddress
import re
import uuid
import warnings
from collections.abc import Callable, Mapping, Sequence, Set
from dataclasses import dataclass
from datetime import date, datetime, time, timedelta
Expand All @@ -11,6 +13,7 @@
from pathlib import Path
from typing import (
TYPE_CHECKING,
Annotated,
Any,
ClassVar,
Literal,
Expand Down Expand Up @@ -89,6 +92,10 @@
)
OnDeleteType = Literal["CASCADE", "SET NULL", "RESTRICT"]

REGEX_PARAM_DEPRECATION_MSG = (
"The `regex` parameter is deprecated. \nUse `pattern` parameter instead."
)


def __dataclass_transform__(
*,
Expand Down Expand Up @@ -259,7 +266,11 @@ def Field(
min_length: int | None = None,
max_length: int | None = None,
allow_mutation: bool = True,
regex: str | None = None,
regex: Annotated[
str | None,
deprecated(REGEX_PARAM_DEPRECATION_MSG),
] = None,
pattern: str | re.Pattern[str] | None = None,
discriminator: str | None = None,
repr: bool = True,
primary_key: bool | UndefinedType = Undefined,
Expand Down Expand Up @@ -302,7 +313,11 @@ def Field(
min_length: int | None = None,
max_length: int | None = None,
allow_mutation: bool = True,
regex: str | None = None,
regex: Annotated[
str | None,
deprecated(REGEX_PARAM_DEPRECATION_MSG),
] = None,
pattern: str | re.Pattern[str] | None = None,
discriminator: str | None = None,
repr: bool = True,
primary_key: bool | UndefinedType = Undefined,
Expand Down Expand Up @@ -354,7 +369,11 @@ def Field(
min_length: int | None = None,
max_length: int | None = None,
allow_mutation: bool = True,
regex: str | None = None,
regex: Annotated[
str | None,
deprecated(REGEX_PARAM_DEPRECATION_MSG),
] = None,
pattern: str | re.Pattern[str] | None = None,
discriminator: str | None = None,
repr: bool = True,
sa_column: Column[Any] | UndefinedType = Undefined,
Expand Down Expand Up @@ -387,7 +406,11 @@ def Field(
min_length: int | None = None,
max_length: int | None = None,
allow_mutation: bool = True,
regex: str | None = None,
regex: Annotated[
str | None,
deprecated(REGEX_PARAM_DEPRECATION_MSG),
] = None,
pattern: str | re.Pattern[str] | None = None,
discriminator: str | None = None,
repr: bool = True,
primary_key: bool | UndefinedType = Undefined,
Expand All @@ -403,9 +426,19 @@ def Field(
schema_extra: dict[str, Any] | None = None,
) -> Any:
current_schema_extra = schema_extra or {}

if regex:
warnings.warn(REGEX_PARAM_DEPRECATION_MSG, DeprecationWarning, stacklevel=2)

for param_name in ("pattern",):
if param_name in current_schema_extra:
msg = f"Pass `{param_name}` parameter directly to Field instead of passing it via `schema_extra`"
warnings.warn(msg, UserWarning, stacklevel=2)

# Extract possible alias settings from schema_extra so we can control precedence
schema_validation_alias = current_schema_extra.pop("validation_alias", None)
schema_serialization_alias = current_schema_extra.pop("serialization_alias", None)
current_pattern = pattern or regex or current_schema_extra.pop("pattern", None)
field_info_kwargs = {
"alias": alias,
"title": title,
Expand All @@ -426,7 +459,7 @@ def Field(
"min_length": min_length,
"max_length": max_length,
"allow_mutation": allow_mutation,
"regex": regex,
"pattern": current_pattern,
"discriminator": discriminator,
"repr": repr,
"primary_key": primary_key,
Expand Down
35 changes: 35 additions & 0 deletions tests/test_pydantic/test_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,38 @@ class Model(SQLModel):

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


def test_regex():
with pytest.warns(DeprecationWarning, match="The `regex` parameter is deprecated"):

class DateModel(SQLModel):
date_1: str = Field(regex=r"^\d{2}-\d{2}-\d{4}$")

DateModel(date_1="12-31-2024")

with pytest.raises(ValidationError):
DateModel(date_1="incorrect")


def test_pattern():
class DateModel(SQLModel):
date_1: str = Field(pattern=r"^\d{2}-\d{2}-\d{4}$")

DateModel(date_1="12-31-2024")

with pytest.raises(ValidationError):
DateModel(date_1="incorrect")


def test_pattern_via_schema_extra():
with pytest.warns(
UserWarning,
match="Pass `pattern` parameter directly to Field instead of passing it via `schema_extra`",
):

class DateModel(SQLModel):
date_1: str = Field(schema_extra={"pattern": r"^\d{2}-\d{2}-\d{4}$"})

with pytest.raises(ValidationError):
DateModel(date_1="incorrect")
Loading