Browse Source

♻️ Replace float with SupportsGt, SupportsGe, SupportsLt, SupportsLe for better type consistency

pull/13250/head
junah201 2 months ago
parent
commit
70bec360aa
No known key found for this signature in database GPG Key ID: CF7A94DA24CDA12F
  1. 57
      fastapi/param_functions.py
  2. 65
      fastapi/params.py

57
fastapi/param_functions.py

@ -1,5 +1,6 @@
from typing import Any, Callable, Dict, List, Optional, Sequence, Union from typing import Any, Callable, Dict, List, Optional, Sequence, Union
from annotated_types import SupportsGe, SupportsGt, SupportsLe, SupportsLt
from fastapi import params from fastapi import params
from fastapi._compat import Undefined from fastapi._compat import Undefined
from fastapi.openapi.models import Example from fastapi.openapi.models import Example
@ -90,7 +91,7 @@ def Path( # noqa: N802
), ),
] = None, ] = None,
gt: Annotated[ gt: Annotated[
Optional[float], Optional[SupportsGt],
Doc( Doc(
""" """
Greater than. If set, value must be greater than this. Only applicable to Greater than. If set, value must be greater than this. Only applicable to
@ -99,7 +100,7 @@ def Path( # noqa: N802
), ),
] = None, ] = None,
ge: Annotated[ ge: Annotated[
Optional[float], Optional[SupportsGe],
Doc( Doc(
""" """
Greater than or equal. If set, value must be greater than or equal to Greater than or equal. If set, value must be greater than or equal to
@ -108,7 +109,7 @@ def Path( # noqa: N802
), ),
] = None, ] = None,
lt: Annotated[ lt: Annotated[
Optional[float], Optional[SupportsLt],
Doc( 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. Only applicable to numbers.
@ -116,7 +117,7 @@ def Path( # noqa: N802
), ),
] = None, ] = None,
le: Annotated[ le: Annotated[
Optional[float], Optional[SupportsLe],
Doc( Doc(
""" """
Less than or equal. If set, value must be less than or equal to this. Less than or equal. If set, value must be less than or equal to this.
@ -415,7 +416,7 @@ def Query( # noqa: N802
), ),
] = None, ] = None,
gt: Annotated[ gt: Annotated[
Optional[float], Optional[SupportsGt],
Doc( Doc(
""" """
Greater than. If set, value must be greater than this. Only applicable to Greater than. If set, value must be greater than this. Only applicable to
@ -424,7 +425,7 @@ def Query( # noqa: N802
), ),
] = None, ] = None,
ge: Annotated[ ge: Annotated[
Optional[float], Optional[SupportsGe],
Doc( Doc(
""" """
Greater than or equal. If set, value must be greater than or equal to Greater than or equal. If set, value must be greater than or equal to
@ -433,7 +434,7 @@ def Query( # noqa: N802
), ),
] = None, ] = None,
lt: Annotated[ lt: Annotated[
Optional[float], Optional[SupportsLt],
Doc( 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. Only applicable to numbers.
@ -441,7 +442,7 @@ def Query( # noqa: N802
), ),
] = None, ] = None,
le: Annotated[ le: Annotated[
Optional[float], Optional[SupportsLe],
Doc( Doc(
""" """
Less than or equal. If set, value must be less than or equal to this. Less than or equal. If set, value must be less than or equal to this.
@ -730,7 +731,7 @@ def Header( # noqa: N802
), ),
] = None, ] = None,
gt: Annotated[ gt: Annotated[
Optional[float], Optional[SupportsGt],
Doc( Doc(
""" """
Greater than. If set, value must be greater than this. Only applicable to Greater than. If set, value must be greater than this. Only applicable to
@ -739,7 +740,7 @@ def Header( # noqa: N802
), ),
] = None, ] = None,
ge: Annotated[ ge: Annotated[
Optional[float], Optional[SupportsGe],
Doc( Doc(
""" """
Greater than or equal. If set, value must be greater than or equal to Greater than or equal. If set, value must be greater than or equal to
@ -748,7 +749,7 @@ def Header( # noqa: N802
), ),
] = None, ] = None,
lt: Annotated[ lt: Annotated[
Optional[float], Optional[SupportsLt],
Doc( 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. Only applicable to numbers.
@ -756,7 +757,7 @@ def Header( # noqa: N802
), ),
] = None, ] = None,
le: Annotated[ le: Annotated[
Optional[float], Optional[SupportsLe],
Doc( Doc(
""" """
Less than or equal. If set, value must be less than or equal to this. Less than or equal. If set, value must be less than or equal to this.
@ -1035,7 +1036,7 @@ def Cookie( # noqa: N802
), ),
] = None, ] = None,
gt: Annotated[ gt: Annotated[
Optional[float], Optional[SupportsGt],
Doc( Doc(
""" """
Greater than. If set, value must be greater than this. Only applicable to Greater than. If set, value must be greater than this. Only applicable to
@ -1044,7 +1045,7 @@ def Cookie( # noqa: N802
), ),
] = None, ] = None,
ge: Annotated[ ge: Annotated[
Optional[float], Optional[SupportsGe],
Doc( Doc(
""" """
Greater than or equal. If set, value must be greater than or equal to Greater than or equal. If set, value must be greater than or equal to
@ -1053,7 +1054,7 @@ def Cookie( # noqa: N802
), ),
] = None, ] = None,
lt: Annotated[ lt: Annotated[
Optional[float], Optional[SupportsLt],
Doc( 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. Only applicable to numbers.
@ -1061,7 +1062,7 @@ def Cookie( # noqa: N802
), ),
] = None, ] = None,
le: Annotated[ le: Annotated[
Optional[float], Optional[SupportsLe],
Doc( Doc(
""" """
Less than or equal. If set, value must be less than or equal to this. Less than or equal. If set, value must be less than or equal to this.
@ -1362,7 +1363,7 @@ def Body( # noqa: N802
), ),
] = None, ] = None,
gt: Annotated[ gt: Annotated[
Optional[float], Optional[SupportsGt],
Doc( Doc(
""" """
Greater than. If set, value must be greater than this. Only applicable to Greater than. If set, value must be greater than this. Only applicable to
@ -1371,7 +1372,7 @@ def Body( # noqa: N802
), ),
] = None, ] = None,
ge: Annotated[ ge: Annotated[
Optional[float], Optional[SupportsGe],
Doc( Doc(
""" """
Greater than or equal. If set, value must be greater than or equal to Greater than or equal. If set, value must be greater than or equal to
@ -1380,7 +1381,7 @@ def Body( # noqa: N802
), ),
] = None, ] = None,
lt: Annotated[ lt: Annotated[
Optional[float], Optional[SupportsLt],
Doc( 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. Only applicable to numbers.
@ -1388,7 +1389,7 @@ def Body( # noqa: N802
), ),
] = None, ] = None,
le: Annotated[ le: Annotated[
Optional[float], Optional[SupportsLe],
Doc( Doc(
""" """
Less than or equal. If set, value must be less than or equal to this. Less than or equal. If set, value must be less than or equal to this.
@ -1677,7 +1678,7 @@ def Form( # noqa: N802
), ),
] = None, ] = None,
gt: Annotated[ gt: Annotated[
Optional[float], Optional[SupportsGt],
Doc( Doc(
""" """
Greater than. If set, value must be greater than this. Only applicable to Greater than. If set, value must be greater than this. Only applicable to
@ -1686,7 +1687,7 @@ def Form( # noqa: N802
), ),
] = None, ] = None,
ge: Annotated[ ge: Annotated[
Optional[float], Optional[SupportsGe],
Doc( Doc(
""" """
Greater than or equal. If set, value must be greater than or equal to Greater than or equal. If set, value must be greater than or equal to
@ -1695,7 +1696,7 @@ def Form( # noqa: N802
), ),
] = None, ] = None,
lt: Annotated[ lt: Annotated[
Optional[float], Optional[SupportsLt],
Doc( 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. Only applicable to numbers.
@ -1703,7 +1704,7 @@ def Form( # noqa: N802
), ),
] = None, ] = None,
le: Annotated[ le: Annotated[
Optional[float], Optional[SupportsLe],
Doc( Doc(
""" """
Less than or equal. If set, value must be less than or equal to this. Less than or equal. If set, value must be less than or equal to this.
@ -1991,7 +1992,7 @@ def File( # noqa: N802
), ),
] = None, ] = None,
gt: Annotated[ gt: Annotated[
Optional[float], Optional[SupportsGt],
Doc( Doc(
""" """
Greater than. If set, value must be greater than this. Only applicable to Greater than. If set, value must be greater than this. Only applicable to
@ -2000,7 +2001,7 @@ def File( # noqa: N802
), ),
] = None, ] = None,
ge: Annotated[ ge: Annotated[
Optional[float], Optional[SupportsGe],
Doc( Doc(
""" """
Greater than or equal. If set, value must be greater than or equal to Greater than or equal. If set, value must be greater than or equal to
@ -2009,7 +2010,7 @@ def File( # noqa: N802
), ),
] = None, ] = None,
lt: Annotated[ lt: Annotated[
Optional[float], Optional[SupportsLt],
Doc( 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. Only applicable to numbers.
@ -2017,7 +2018,7 @@ def File( # noqa: N802
), ),
] = None, ] = None,
le: Annotated[ le: Annotated[
Optional[float], Optional[SupportsLe],
Doc( Doc(
""" """
Less than or equal. If set, value must be less than or equal to this. Less than or equal. If set, value must be less than or equal to this.

65
fastapi/params.py

@ -2,6 +2,7 @@ import warnings
from enum import Enum from enum import Enum
from typing import Any, Callable, Dict, List, Optional, Sequence, Union 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 fastapi.openapi.models import Example
from pydantic.fields import FieldInfo from pydantic.fields import FieldInfo
from typing_extensions import Annotated, deprecated from typing_extensions import Annotated, deprecated
@ -39,10 +40,10 @@ class Param(FieldInfo):
serialization_alias: Union[str, None] = None, serialization_alias: Union[str, None] = None,
title: Optional[str] = None, title: Optional[str] = None,
description: Optional[str] = None, description: Optional[str] = None,
gt: Optional[float] = None, gt: Optional[SupportsGt] = None,
ge: Optional[float] = None, ge: Optional[SupportsGe] = None,
lt: Optional[float] = None, lt: Optional[SupportsLt] = None,
le: Optional[float] = None, le: Optional[SupportsLe] = None,
min_length: Optional[int] = None, min_length: Optional[int] = None,
max_length: Optional[int] = None, max_length: Optional[int] = None,
pattern: Optional[str] = None, pattern: Optional[str] = None,
@ -153,10 +154,10 @@ class Path(Param):
serialization_alias: Union[str, None] = None, serialization_alias: Union[str, None] = None,
title: Optional[str] = None, title: Optional[str] = None,
description: Optional[str] = None, description: Optional[str] = None,
gt: Optional[float] = None, gt: Optional[SupportsGt] = None,
ge: Optional[float] = None, ge: Optional[SupportsGe] = None,
lt: Optional[float] = None, lt: Optional[SupportsLt] = None,
le: Optional[float] = None, le: Optional[SupportsLe] = None,
min_length: Optional[int] = None, min_length: Optional[int] = None,
max_length: Optional[int] = None, max_length: Optional[int] = None,
pattern: Optional[str] = None, pattern: Optional[str] = None,
@ -239,10 +240,10 @@ class Query(Param):
serialization_alias: Union[str, None] = None, serialization_alias: Union[str, None] = None,
title: Optional[str] = None, title: Optional[str] = None,
description: Optional[str] = None, description: Optional[str] = None,
gt: Optional[float] = None, gt: Optional[SupportsGt] = None,
ge: Optional[float] = None, ge: Optional[SupportsGe] = None,
lt: Optional[float] = None, lt: Optional[SupportsLt] = None,
le: Optional[float] = None, le: Optional[SupportsLe] = None,
min_length: Optional[int] = None, min_length: Optional[int] = None,
max_length: Optional[int] = None, max_length: Optional[int] = None,
pattern: Optional[str] = None, pattern: Optional[str] = None,
@ -324,10 +325,10 @@ class Header(Param):
convert_underscores: bool = True, convert_underscores: bool = True,
title: Optional[str] = None, title: Optional[str] = None,
description: Optional[str] = None, description: Optional[str] = None,
gt: Optional[float] = None, gt: Optional[SupportsGt] = None,
ge: Optional[float] = None, ge: Optional[SupportsGe] = None,
lt: Optional[float] = None, lt: Optional[SupportsLt] = None,
le: Optional[float] = None, le: Optional[SupportsLe] = None,
min_length: Optional[int] = None, min_length: Optional[int] = None,
max_length: Optional[int] = None, max_length: Optional[int] = None,
pattern: Optional[str] = None, pattern: Optional[str] = None,
@ -409,10 +410,10 @@ class Cookie(Param):
serialization_alias: Union[str, None] = None, serialization_alias: Union[str, None] = None,
title: Optional[str] = None, title: Optional[str] = None,
description: Optional[str] = None, description: Optional[str] = None,
gt: Optional[float] = None, gt: Optional[SupportsGt] = None,
ge: Optional[float] = None, ge: Optional[SupportsGe] = None,
lt: Optional[float] = None, lt: Optional[SupportsLt] = None,
le: Optional[float] = None, le: Optional[SupportsLe] = None,
min_length: Optional[int] = None, min_length: Optional[int] = None,
max_length: Optional[int] = None, max_length: Optional[int] = None,
pattern: Optional[str] = None, pattern: Optional[str] = None,
@ -493,10 +494,10 @@ class Body(FieldInfo):
serialization_alias: Union[str, None] = None, serialization_alias: Union[str, None] = None,
title: Optional[str] = None, title: Optional[str] = None,
description: Optional[str] = None, description: Optional[str] = None,
gt: Optional[float] = None, gt: Optional[SupportsGt] = None,
ge: Optional[float] = None, ge: Optional[SupportsGe] = None,
lt: Optional[float] = None, lt: Optional[SupportsLt] = None,
le: Optional[float] = None, le: Optional[SupportsLe] = None,
min_length: Optional[int] = None, min_length: Optional[int] = None,
max_length: Optional[int] = None, max_length: Optional[int] = None,
pattern: Optional[str] = None, pattern: Optional[str] = None,
@ -609,10 +610,10 @@ class Form(Body):
serialization_alias: Union[str, None] = None, serialization_alias: Union[str, None] = None,
title: Optional[str] = None, title: Optional[str] = None,
description: Optional[str] = None, description: Optional[str] = None,
gt: Optional[float] = None, gt: Optional[SupportsGt] = None,
ge: Optional[float] = None, ge: Optional[SupportsGe] = None,
lt: Optional[float] = None, lt: Optional[SupportsLt] = None,
le: Optional[float] = None, le: Optional[SupportsLe] = None,
min_length: Optional[int] = None, min_length: Optional[int] = None,
max_length: Optional[int] = None, max_length: Optional[int] = None,
pattern: Optional[str] = None, pattern: Optional[str] = None,
@ -693,10 +694,10 @@ class File(Form):
serialization_alias: Union[str, None] = None, serialization_alias: Union[str, None] = None,
title: Optional[str] = None, title: Optional[str] = None,
description: Optional[str] = None, description: Optional[str] = None,
gt: Optional[float] = None, gt: Optional[SupportsGt] = None,
ge: Optional[float] = None, ge: Optional[SupportsGe] = None,
lt: Optional[float] = None, lt: Optional[SupportsLt] = None,
le: Optional[float] = None, le: Optional[SupportsLe] = None,
min_length: Optional[int] = None, min_length: Optional[int] = None,
max_length: Optional[int] = None, max_length: Optional[int] = None,
pattern: Optional[str] = None, pattern: Optional[str] = None,

Loading…
Cancel
Save