pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/fastapi/sqlmodel/pull/1730/files

eet" href="https://github.githubassets.com/assets/github-cf976967feea1e66.css" /> 🏷️ Allow values that support `SupportsGt` (and others) protocol for `gt`, `ge`, `lt` and `le` in `Field` by YuriiMotov · Pull Request #1730 · fastapi/sqlmodel · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions sqlmodel/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
overload,
)

import annotated_types
from pydantic import BaseModel, EmailStr
from pydantic.fields import FieldInfo as PydanticFieldInfo
from sqlalchemy import (
Expand Down Expand Up @@ -247,10 +248,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 @@ -290,10 +291,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 @@ -342,10 +343,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 @@ -375,10 +376,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
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",)
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy