diff --git a/fastapi/param_functions.py b/fastapi/param_functions.py index b3621626c..7dc2c1d9f 100644 --- a/fastapi/param_functions.py +++ b/fastapi/param_functions.py @@ -1,5 +1,6 @@ from typing import Any, Callable, Dict, List, Optional, Sequence, Union +from annotated_types import SupportsGe, SupportsGt, SupportsLe, SupportsLt from fastapi import params from fastapi._compat import Undefined from fastapi.openapi.models import Example @@ -90,37 +91,38 @@ def Path( # noqa: N802 ), ] = None, gt: Annotated[ - Optional[float], + Optional[SupportsGt], Doc( """ - Greater than. If set, value must be greater than this. Only applicable to - numbers. + Greater than. If set, value must be greater than this. Applicable to + any comparable type, not just numbers. """ ), ] = None, ge: Annotated[ - Optional[float], + Optional[SupportsGe], Doc( """ Greater than or equal. If set, value must be greater than or equal to - this. Only applicable to numbers. + this. Applicable to any comparable type, not just numbers. """ ), ] = None, lt: Annotated[ - Optional[float], + Optional[SupportsLt], Doc( """ - Less than. If set, value must be less than this. Only applicable to numbers. + Less than. If set, value must be less than this. Applicable to + any comparable type, not just numbers. """ ), ] = None, le: Annotated[ - Optional[float], + Optional[SupportsLe], Doc( """ Less than or equal. If set, value must be less than or equal to this. - Only applicable to numbers. + Applicable to any comparable type, not just numbers. """ ), ] = None, @@ -415,37 +417,38 @@ def Query( # noqa: N802 ), ] = None, gt: Annotated[ - Optional[float], + Optional[SupportsGt], Doc( """ - Greater than. If set, value must be greater than this. Only applicable to - numbers. + Greater than. If set, value must be greater than this. Applicable to + any comparable type, not just numbers. """ ), ] = None, ge: Annotated[ - Optional[float], + Optional[SupportsGe], Doc( """ Greater than or equal. If set, value must be greater than or equal to - this. Only applicable to numbers. + this. Applicable to any comparable type, not just numbers. """ ), ] = None, lt: Annotated[ - Optional[float], + Optional[SupportsLt], Doc( """ - Less than. If set, value must be less than this. Only applicable to numbers. + Less than. If set, value must be less than this. Applicable to + any comparable type, not just numbers. """ ), ] = None, le: Annotated[ - Optional[float], + Optional[SupportsLe], Doc( """ Less than or equal. If set, value must be less than or equal to this. - Only applicable to numbers. + Applicable to any comparable type, not just numbers. """ ), ] = None, @@ -730,37 +733,38 @@ def Header( # noqa: N802 ), ] = None, gt: Annotated[ - Optional[float], + Optional[SupportsGt], Doc( """ - Greater than. If set, value must be greater than this. Only applicable to - numbers. + Greater than. If set, value must be greater than this. Applicable to + any comparable type, not just numbers. """ ), ] = None, ge: Annotated[ - Optional[float], + Optional[SupportsGe], Doc( """ Greater than or equal. If set, value must be greater than or equal to - this. Only applicable to numbers. + this. Applicable to any comparable type, not just numbers. """ ), ] = None, lt: Annotated[ - Optional[float], + Optional[SupportsLt], Doc( """ - Less than. If set, value must be less than this. Only applicable to numbers. + Less than. If set, value must be less than this. Applicable to + any comparable type, not just numbers. """ ), ] = None, le: Annotated[ - Optional[float], + Optional[SupportsLe], Doc( """ Less than or equal. If set, value must be less than or equal to this. - Only applicable to numbers. + Applicable to any comparable type, not just numbers. """ ), ] = None, @@ -1035,37 +1039,38 @@ def Cookie( # noqa: N802 ), ] = None, gt: Annotated[ - Optional[float], + Optional[SupportsGt], Doc( """ - Greater than. If set, value must be greater than this. Only applicable to - numbers. + Greater than. If set, value must be greater than this. Applicable to + any comparable type, not just numbers. """ ), ] = None, ge: Annotated[ - Optional[float], + Optional[SupportsGe], Doc( """ Greater than or equal. If set, value must be greater than or equal to - this. Only applicable to numbers. + this. Applicable to any comparable type, not just numbers. """ ), ] = None, lt: Annotated[ - Optional[float], + Optional[SupportsLt], Doc( """ - Less than. If set, value must be less than this. Only applicable to numbers. + Less than. If set, value must be less than this. Applicable to + any comparable type, not just numbers. """ ), ] = None, le: Annotated[ - Optional[float], + Optional[SupportsLe], Doc( """ Less than or equal. If set, value must be less than or equal to this. - Only applicable to numbers. + Applicable to any comparable type, not just numbers. """ ), ] = None, @@ -1362,37 +1367,38 @@ def Body( # noqa: N802 ), ] = None, gt: Annotated[ - Optional[float], + Optional[SupportsGt], Doc( """ - Greater than. If set, value must be greater than this. Only applicable to - numbers. + Greater than. If set, value must be greater than this. Applicable to + any comparable type, not just numbers. """ ), ] = None, ge: Annotated[ - Optional[float], + Optional[SupportsGe], Doc( """ Greater than or equal. If set, value must be greater than or equal to - this. Only applicable to numbers. + this. Applicable to any comparable type, not just numbers. """ ), ] = None, lt: Annotated[ - Optional[float], + Optional[SupportsLt], Doc( """ - Less than. If set, value must be less than this. Only applicable to numbers. + Less than. If set, value must be less than this. Applicable to + any comparable type, not just numbers. """ ), ] = None, le: Annotated[ - Optional[float], + Optional[SupportsLe], Doc( """ Less than or equal. If set, value must be less than or equal to this. - Only applicable to numbers. + Applicable to any comparable type, not just numbers. """ ), ] = None, @@ -1677,37 +1683,38 @@ def Form( # noqa: N802 ), ] = None, gt: Annotated[ - Optional[float], + Optional[SupportsGt], Doc( """ - Greater than. If set, value must be greater than this. Only applicable to - numbers. + Greater than. If set, value must be greater than this. Applicable to + any comparable type, not just numbers. """ ), ] = None, ge: Annotated[ - Optional[float], + Optional[SupportsGe], Doc( """ Greater than or equal. If set, value must be greater than or equal to - this. Only applicable to numbers. + this. Applicable to any comparable type, not just numbers. """ ), ] = None, lt: Annotated[ - Optional[float], + Optional[SupportsLt], Doc( """ - Less than. If set, value must be less than this. Only applicable to numbers. + Less than. If set, value must be less than this. Applicable to + any comparable type, not just numbers. """ ), ] = None, le: Annotated[ - Optional[float], + Optional[SupportsLe], Doc( """ Less than or equal. If set, value must be less than or equal to this. - Only applicable to numbers. + Applicable to any comparable type, not just numbers. """ ), ] = None, @@ -1991,37 +1998,38 @@ def File( # noqa: N802 ), ] = None, gt: Annotated[ - Optional[float], + Optional[SupportsGt], Doc( """ - Greater than. If set, value must be greater than this. Only applicable to - numbers. + Greater than. If set, value must be greater than this. Applicable to + any comparable type, not just numbers. """ ), ] = None, ge: Annotated[ - Optional[float], + Optional[SupportsGe], Doc( """ Greater than or equal. If set, value must be greater than or equal to - this. Only applicable to numbers. + this. Applicable to any comparable type, not just numbers. """ ), ] = None, lt: Annotated[ - Optional[float], + Optional[SupportsLt], Doc( """ - Less than. If set, value must be less than this. Only applicable to numbers. + Less than. If set, value must be less than this. Applicable to + any comparable type, not just numbers. """ ), ] = None, le: Annotated[ - Optional[float], + Optional[SupportsLe], Doc( """ Less than or equal. If set, value must be less than or equal to this. - Only applicable to numbers. + Applicable to any comparable type, not just numbers. """ ), ] = None, diff --git a/fastapi/params.py b/fastapi/params.py index 8f5601dd3..c178f77bf 100644 --- a/fastapi/params.py +++ b/fastapi/params.py @@ -2,6 +2,7 @@ import warnings from enum import Enum from typing import Any, Callable, Dict, List, Optional, Sequence, Union +from annotated_types import SupportsGe, SupportsGt, SupportsLe, SupportsLt from fastapi.openapi.models import Example from pydantic.fields import FieldInfo from typing_extensions import Annotated, deprecated @@ -39,10 +40,10 @@ class Param(FieldInfo): serialization_alias: Union[str, None] = None, title: Optional[str] = None, description: Optional[str] = None, - gt: Optional[float] = None, - ge: Optional[float] = None, - lt: Optional[float] = None, - le: Optional[float] = None, + gt: Optional[SupportsGt] = None, + ge: Optional[SupportsGe] = None, + lt: Optional[SupportsLt] = None, + le: Optional[SupportsLe] = None, min_length: Optional[int] = None, max_length: Optional[int] = None, pattern: Optional[str] = None, @@ -153,10 +154,10 @@ class Path(Param): serialization_alias: Union[str, None] = None, title: Optional[str] = None, description: Optional[str] = None, - gt: Optional[float] = None, - ge: Optional[float] = None, - lt: Optional[float] = None, - le: Optional[float] = None, + gt: Optional[SupportsGt] = None, + ge: Optional[SupportsGe] = None, + lt: Optional[SupportsLt] = None, + le: Optional[SupportsLe] = None, min_length: Optional[int] = None, max_length: Optional[int] = None, pattern: Optional[str] = None, @@ -239,10 +240,10 @@ class Query(Param): serialization_alias: Union[str, None] = None, title: Optional[str] = None, description: Optional[str] = None, - gt: Optional[float] = None, - ge: Optional[float] = None, - lt: Optional[float] = None, - le: Optional[float] = None, + gt: Optional[SupportsGt] = None, + ge: Optional[SupportsGe] = None, + lt: Optional[SupportsLt] = None, + le: Optional[SupportsLe] = None, min_length: Optional[int] = None, max_length: Optional[int] = None, pattern: Optional[str] = None, @@ -324,10 +325,10 @@ class Header(Param): convert_underscores: bool = True, title: Optional[str] = None, description: Optional[str] = None, - gt: Optional[float] = None, - ge: Optional[float] = None, - lt: Optional[float] = None, - le: Optional[float] = None, + gt: Optional[SupportsGt] = None, + ge: Optional[SupportsGe] = None, + lt: Optional[SupportsLt] = None, + le: Optional[SupportsLe] = None, min_length: Optional[int] = None, max_length: Optional[int] = None, pattern: Optional[str] = None, @@ -409,10 +410,10 @@ class Cookie(Param): serialization_alias: Union[str, None] = None, title: Optional[str] = None, description: Optional[str] = None, - gt: Optional[float] = None, - ge: Optional[float] = None, - lt: Optional[float] = None, - le: Optional[float] = None, + gt: Optional[SupportsGt] = None, + ge: Optional[SupportsGe] = None, + lt: Optional[SupportsLt] = None, + le: Optional[SupportsLe] = None, min_length: Optional[int] = None, max_length: Optional[int] = None, pattern: Optional[str] = None, @@ -493,10 +494,10 @@ class Body(FieldInfo): serialization_alias: Union[str, None] = None, title: Optional[str] = None, description: Optional[str] = None, - gt: Optional[float] = None, - ge: Optional[float] = None, - lt: Optional[float] = None, - le: Optional[float] = None, + gt: Optional[SupportsGt] = None, + ge: Optional[SupportsGe] = None, + lt: Optional[SupportsLt] = None, + le: Optional[SupportsLe] = None, min_length: Optional[int] = None, max_length: Optional[int] = None, pattern: Optional[str] = None, @@ -609,10 +610,10 @@ class Form(Body): serialization_alias: Union[str, None] = None, title: Optional[str] = None, description: Optional[str] = None, - gt: Optional[float] = None, - ge: Optional[float] = None, - lt: Optional[float] = None, - le: Optional[float] = None, + gt: Optional[SupportsGt] = None, + ge: Optional[SupportsGe] = None, + lt: Optional[SupportsLt] = None, + le: Optional[SupportsLe] = None, min_length: Optional[int] = None, max_length: Optional[int] = None, pattern: Optional[str] = None, @@ -693,10 +694,10 @@ class File(Form): serialization_alias: Union[str, None] = None, title: Optional[str] = None, description: Optional[str] = None, - gt: Optional[float] = None, - ge: Optional[float] = None, - lt: Optional[float] = None, - le: Optional[float] = None, + gt: Optional[SupportsGt] = None, + ge: Optional[SupportsGe] = None, + lt: Optional[SupportsLt] = None, + le: Optional[SupportsLe] = None, min_length: Optional[int] = None, max_length: Optional[int] = None, pattern: Optional[str] = None,