diff --git a/sqlmodel/main.py b/sqlmodel/main.py index 40fe64e423..8646b5ac8d 100644 --- a/sqlmodel/main.py +++ b/sqlmodel/main.py @@ -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" ) @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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: @@ -455,7 +487,6 @@ def Field( "description": description, "exclude": exclude, "include": include, - "const": const, "gt": gt, "ge": ge, "lt": lt, @@ -463,7 +494,6 @@ def Field( "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, diff --git a/tests/test_pydantic/test_field.py b/tests/test_pydantic/test_field.py index e41a252852..b43d6419f2 100644 --- a/tests/test_pydantic/test_field.py +++ b/tests/test_pydantic/test_field.py @@ -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,