Skip to content
50 changes: 40 additions & 10 deletions sqlmodel/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@
)
OnDeleteType = Literal["CASCADE", "SET NULL", "RESTRICT"]

CONST_DEPRECATION_MSG = "`const` is deprecated and doesn't work, use `Literal` instead"
UNIQUE_ITEMS_DEPRECATION_MSG = (
"`unique_items` is deprecated and doesn't work, use `set` type instead"
)
MIN_ITEMS_DEPRECATION_MSG = (
"`min_items` is deprecated and will be removed, use `min_length` instead"
)
Expand Down Expand Up @@ -254,7 +258,10 @@ def Field(
description: str | None = None,
exclude: Set[int | str] | Mapping[int | str, Any] | Any = None,
include: Set[int | str] | Mapping[int | str, Any] | Any = None,
const: bool | None = None,
const: Annotated[
bool | None,
deprecated(CONST_DEPRECATION_MSG),
] = None,
gt: float | None = None,
ge: float | None = None,
lt: float | None = None,
Expand All @@ -270,7 +277,10 @@ def Field(
int | None,
deprecated(MAX_ITEMS_DEPRECATION_MSG),
] = None,
unique_items: bool | None = None,
unique_items: Annotated[
bool | None,
deprecated(UNIQUE_ITEMS_DEPRECATION_MSG),
] = None,
min_length: int | None = None,
max_length: int | None = None,
allow_mutation: bool = True,
Expand Down Expand Up @@ -303,7 +313,10 @@ def Field(
description: str | None = None,
exclude: Set[int | str] | Mapping[int | str, Any] | Any = None,
include: Set[int | str] | Mapping[int | str, Any] | Any = None,
const: bool | None = None,
const: Annotated[
bool | None,
deprecated(CONST_DEPRECATION_MSG),
] = None,
gt: float | None = None,
ge: float | None = None,
lt: float | None = None,
Expand All @@ -319,7 +332,10 @@ def Field(
int | None,
deprecated(MAX_ITEMS_DEPRECATION_MSG),
] = None,
unique_items: bool | None = None,
unique_items: Annotated[
bool | None,
deprecated(UNIQUE_ITEMS_DEPRECATION_MSG),
] = None,
min_length: int | None = None,
max_length: int | None = None,
allow_mutation: bool = True,
Expand Down Expand Up @@ -361,7 +377,10 @@ def Field(
description: str | None = None,
exclude: Set[int | str] | Mapping[int | str, Any] | Any = None,
include: Set[int | str] | Mapping[int | str, Any] | Any = None,
const: bool | None = None,
const: Annotated[
bool | None,
deprecated(CONST_DEPRECATION_MSG),
] = None,
gt: float | None = None,
ge: float | None = None,
lt: float | None = None,
Expand All @@ -377,7 +396,10 @@ def Field(
int | None,
deprecated(MAX_ITEMS_DEPRECATION_MSG),
] = None,
unique_items: bool | None = None,
unique_items: Annotated[
bool | None,
deprecated(UNIQUE_ITEMS_DEPRECATION_MSG),
] = None,
min_length: int | None = None,
max_length: int | None = None,
allow_mutation: bool = True,
Expand All @@ -400,7 +422,10 @@ def Field(
description: str | None = None,
exclude: Set[int | str] | Mapping[int | str, Any] | Any = None,
include: Set[int | str] | Mapping[int | str, Any] | Any = None,
const: bool | None = None,
const: Annotated[
bool | None,
deprecated(CONST_DEPRECATION_MSG),
] = None,
gt: float | None = None,
ge: float | None = None,
lt: float | None = None,
Expand All @@ -416,7 +441,10 @@ def Field(
int | None,
deprecated(MAX_ITEMS_DEPRECATION_MSG),
] = None,
unique_items: bool | None = None,
unique_items: Annotated[
bool | None,
deprecated(UNIQUE_ITEMS_DEPRECATION_MSG),
] = None,
min_length: int | None = None,
max_length: int | None = None,
allow_mutation: bool = True,
Expand All @@ -437,6 +465,10 @@ def Field(
) -> Any:
current_schema_extra = schema_extra or {}

if const is not None:
raise RuntimeError(CONST_DEPRECATION_MSG)
if unique_items is not None:
raise RuntimeError(UNIQUE_ITEMS_DEPRECATION_MSG)
if min_items is not None:
warnings.warn(MIN_ITEMS_DEPRECATION_MSG, DeprecationWarning, stacklevel=2)
if min_length is None:
Expand All @@ -455,15 +487,13 @@ def Field(
"description": description,
"exclude": exclude,
"include": include,
"const": const,
"gt": gt,
"ge": ge,
"lt": lt,
"le": le,
"multiple_of": multiple_of,
"max_digits": max_digits,
"decimal_places": decimal_places,
"unique_items": unique_items,
"min_length": min_length,
"max_length": max_length,
"allow_mutation": allow_mutation,
Expand Down
20 changes: 20 additions & 0 deletions tests/test_pydantic/test_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,26 @@ class Model(SQLModel):
assert "foo=" not in repr(instance)


def test_const_is_deprecated():
with pytest.raises(
RuntimeError,
match="`const` is deprecated and doesn't work, use `Literal` instead",
):

class Model(SQLModel):
int_value: int = Field(default=10, const=True)


def test_unique_items_is_deprecated():
with pytest.raises(
RuntimeError,
match="`unique_items` is deprecated and doesn't work, use `set` type instead",
):

class Model(SQLModel):
values: list[int] = Field(unique_items=True)


def test_min_items():
with pytest.warns(
DeprecationWarning,
Expand Down