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

🏷️ Allow values that support `SupportsGt` (and others) protocol for `gt`, `ge`, `lt` and `le` in `Field` by YuriiMotov · Pull Request #1730 · fastapi/sqlmodel · GitHub

Repository navigation

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
33 changes: 17 additions & 16 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 @@ -22,6 +22,7 @@
overload,
)

import annotated_types
from pydantic import BaseModel, EmailStr
from pydantic.fields import FieldInfo as PydanticFieldInfo
from sqlalchemy import (
Expand Down Expand Up @@ -246,10 +247,10 @@ def Field(
exclude: Set[int | str] | Mapping[int | str, Any] | Any = None,
include: Set[int | str] | Mapping[int | str, Any] | Any = None,
const: bool | None = None,
gt: float | None = None,
ge: float | None = None,
lt: float | None = None,
le: float | None = None,
gt: annotated_types.SupportsGt | None = None,
ge: annotated_types.SupportsGe | None = None,
lt: annotated_types.SupportsLt | None = None,
le: annotated_types.SupportsLe | None = None,
multiple_of: float | None = None,
max_digits: int | None = None,
decimal_places: int | None = None,
Expand Down Expand Up @@ -289,10 +290,10 @@ def Field(
exclude: Set[int | str] | Mapping[int | str, Any] | Any = None,
include: Set[int | str] | Mapping[int | str, Any] | Any = None,
const: bool | None = None,
gt: float | None = None,
ge: float | None = None,
lt: float | None = None,
le: float | None = None,
gt: annotated_types.SupportsGt | None = None,
ge: annotated_types.SupportsGe | None = None,
lt: annotated_types.SupportsLt | None = None,
le: annotated_types.SupportsLe | None = None,
multiple_of: float | None = None,
max_digits: int | None = None,
decimal_places: int | None = None,
Expand Down Expand Up @@ -341,10 +342,10 @@ def Field(
exclude: Set[int | str] | Mapping[int | str, Any] | Any = None,
include: Set[int | str] | Mapping[int | str, Any] | Any = None,
const: bool | None = None,
gt: float | None = None,
ge: float | None = None,
lt: float | None = None,
le: float | None = None,
gt: annotated_types.SupportsGt | None = None,
ge: annotated_types.SupportsGe | None = None,
lt: annotated_types.SupportsLt | None = None,
le: annotated_types.SupportsLe | None = None,
multiple_of: float | None = None,
max_digits: int | None = None,
decimal_places: int | None = None,
Expand Down Expand Up @@ -374,10 +375,10 @@ def Field(
exclude: Set[int | str] | Mapping[int | str, Any] | Any = None,
include: Set[int | str] | Mapping[int | str, Any] | Any = None,
const: bool | None = None,
gt: float | None = None,
ge: float | None = None,
lt: float | None = None,
le: float | None = None,
gt: annotated_types.SupportsGt | None = None,
ge: annotated_types.SupportsGe | None = None,
lt: annotated_types.SupportsLt | None = None,
le: annotated_types.SupportsLe | None = None,
multiple_of: float | None = None,
max_digits: int | None = None,
decimal_places: int | None = None,
Expand Down
80 changes: 80 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 @@ -54,3 +54,83 @@ class Model(SQLModel):

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


def test_gt():
class Model(SQLModel):
int_value: int = Field(gt=10)
tuple_value: tuple[int, int] = Field(gt=(1, 2))

Model(int_value=11, tuple_value=(1, 3))

with pytest.raises(ValidationError) as exc_info:
Model(int_value=10, tuple_value=(1, 3))
assert len(exc_info.value.errors()) == 1
assert exc_info.value.errors()[0]["type"] == "greater_than"
assert exc_info.value.errors()[0]["loc"] == ("int_value",)

with pytest.raises(ValidationError) as exc_info_2:
Model(int_value=11, tuple_value=(1, 2))
assert len(exc_info_2.value.errors()) == 1
assert exc_info_2.value.errors()[0]["type"] == "greater_than"
assert exc_info_2.value.errors()[0]["loc"] == ("tuple_value",)


def test_ge():
class Model(SQLModel):
int_value: int = Field(ge=10)
tuple_value: tuple[int, int] = Field(ge=(1, 2))

Model(int_value=10, tuple_value=(1, 2))

with pytest.raises(ValidationError) as exc_info:
Model(int_value=9, tuple_value=(1, 2))
assert len(exc_info.value.errors()) == 1
assert exc_info.value.errors()[0]["type"] == "greater_than_equal"
assert exc_info.value.errors()[0]["loc"] == ("int_value",)

with pytest.raises(ValidationError) as exc_info_2:
Model(int_value=10, tuple_value=(1, 1))
assert len(exc_info_2.value.errors()) == 1
assert exc_info_2.value.errors()[0]["type"] == "greater_than_equal"
assert exc_info_2.value.errors()[0]["loc"] == ("tuple_value",)


def test_lt():
class Model(SQLModel):
int_value: int = Field(lt=10)
tuple_value: tuple[int, int] = Field(lt=(1, 2))

Model(int_value=9, tuple_value=(1, 1))

with pytest.raises(ValidationError) as exc_info:
Model(int_value=10, tuple_value=(1, 1))
assert len(exc_info.value.errors()) == 1
assert exc_info.value.errors()[0]["type"] == "less_than"
assert exc_info.value.errors()[0]["loc"] == ("int_value",)

with pytest.raises(ValidationError) as exc_info_2:
Model(int_value=9, tuple_value=(1, 2))
assert len(exc_info_2.value.errors()) == 1
assert exc_info_2.value.errors()[0]["type"] == "less_than"
assert exc_info_2.value.errors()[0]["loc"] == ("tuple_value",)


def test_le():
class Model(SQLModel):
int_value: int = Field(le=10)
tuple_value: tuple[int, int] = Field(le=(1, 2))

Model(int_value=10, tuple_value=(1, 2))

with pytest.raises(ValidationError) as exc_info:
Model(int_value=11, tuple_value=(1, 2))
assert len(exc_info.value.errors()) == 1
assert exc_info.value.errors()[0]["type"] == "less_than_equal"
assert exc_info.value.errors()[0]["loc"] == ("int_value",)

with pytest.raises(ValidationError) as exc_info_2:
Model(int_value=10, tuple_value=(1, 3))
assert len(exc_info_2.value.errors()) == 1
assert exc_info_2.value.errors()[0]["type"] == "less_than_equal"
assert exc_info_2.value.errors()[0]["loc"] == ("tuple_value",)

Back | FazBrowse Home | New Git URL