FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

🏷️ Fix typing for `exclude` parameter of `Field`, deprecate `include` parameter by YuriiMotov · Pull Request #1724 · fastapi/sqlmodel · GitHub

Repository navigation

31 changes: 23 additions & 8 deletions sqlmodel/main.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down
23 changes: 23 additions & 0 deletions tests/test_pydantic/test_field.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

Back | FazBrowse Home | New Git URL