diff --git a/sqlmodel/main.py b/sqlmodel/main.py index 40fe64e423..eab21e78a4 100644 --- a/sqlmodel/main.py +++ b/sqlmodel/main.py @@ -91,6 +91,7 @@ ) OnDeleteType = Literal["CASCADE", "SET NULL", "RESTRICT"] +INCLUDE_DEPRECATION_MSG = "`include` is deprecated and does nothing. It will be removed, use `exclude` instead" MIN_ITEMS_DEPRECATION_MSG = ( "`min_items` is deprecated and will be removed, use `min_length` instead" ) @@ -252,8 +253,11 @@ def Field( serialization_alias: str | None = None, title: str | None = None, description: str | None = None, - exclude: Set[int | str] | Mapping[int | str, Any] | Any = None, - include: Set[int | str] | Mapping[int | str, Any] | Any = None, + exclude: bool | None = None, + include: Annotated[ + Set[int | str] | Mapping[int | str, Any] | Any, + deprecated(INCLUDE_DEPRECATION_MSG), + ] = None, const: bool | None = None, gt: float | None = None, ge: float | None = None, @@ -301,8 +305,11 @@ def Field( serialization_alias: str | None = None, title: str | None = None, description: str | None = None, - exclude: Set[int | str] | Mapping[int | str, Any] | Any = None, - include: Set[int | str] | Mapping[int | str, Any] | Any = None, + exclude: bool | None = None, + include: Annotated[ + Set[int | str] | Mapping[int | str, Any] | Any, + deprecated(INCLUDE_DEPRECATION_MSG), + ] = None, const: bool | None = None, gt: float | None = None, ge: float | None = None, @@ -359,8 +366,11 @@ def Field( serialization_alias: str | None = None, title: str | None = None, description: str | None = None, - exclude: Set[int | str] | Mapping[int | str, Any] | Any = None, - include: Set[int | str] | Mapping[int | str, Any] | Any = None, + exclude: bool | None = None, + include: Annotated[ + Set[int | str] | Mapping[int | str, Any] | Any, + deprecated(INCLUDE_DEPRECATION_MSG), + ] = None, const: bool | None = None, gt: float | None = None, ge: float | None = None, @@ -398,8 +408,11 @@ def Field( serialization_alias: str | None = None, title: str | None = None, description: str | None = None, - exclude: Set[int | str] | Mapping[int | str, Any] | Any = None, - include: Set[int | str] | Mapping[int | str, Any] | Any = None, + exclude: bool | None = None, + include: Annotated[ + Set[int | str] | Mapping[int | str, Any] | Any, + deprecated(INCLUDE_DEPRECATION_MSG), + ] = None, const: bool | None = None, gt: float | None = None, ge: float | None = None, @@ -437,6 +450,8 @@ def Field( ) -> Any: current_schema_extra = schema_extra or {} + if include is not None: + warnings.warn(INCLUDE_DEPRECATION_MSG, DeprecationWarning, stacklevel=2) if min_items is not None: warnings.warn(MIN_ITEMS_DEPRECATION_MSG, DeprecationWarning, stacklevel=2) if min_length is None: diff --git a/tests/test_pydantic/test_field.py b/tests/test_pydantic/test_field.py index e41a252852..9709c28109 100644 --- a/tests/test_pydantic/test_field.py +++ b/tests/test_pydantic/test_field.py @@ -88,6 +88,29 @@ class Model(SQLModel): assert "foo=" not in repr(instance) +def test_exclude(): + class Model(SQLModel): + id: int + name: str + value: int = Field(exclude=True) + + instance = Model(id=1, name="test", value=42) + dict_representation = instance.model_dump() + assert "id" in dict_representation + assert "name" in dict_representation + assert "value" not in dict_representation + + +def test_include_is_deprecated(): + with pytest.warns( + DeprecationWarning, + match="`include` is deprecated and does nothing. It will be removed, use `exclude` instead", + ): + + class Model(SQLModel): + values: list[int] = Field(include=True) + + def test_min_items(): with pytest.warns( DeprecationWarning,