Skip to content

Commit

Permalink
Merge pull request #709 from rouge8/field-number-kind
Browse files Browse the repository at this point in the history
`Field.number(kind=...)` supports any function that returns a number
  • Loading branch information
glyph authored Mar 12, 2024
2 parents 0f3597e + 030e604 commit a8c9e69
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ source=
exclude_lines =
pragma: no cover
if TYPE_CHECKING:
\s*\.\.\.$
61 changes: 57 additions & 4 deletions src/klein/_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
NoReturn,
Optional,
Sequence,
Type,
TypeVar,
cast,
overload,
)

import attr
Expand All @@ -29,6 +30,7 @@

from ._app import KleinRenderable, _call
from ._decorators import bindable
from ._typing_compat import Protocol
from .interfaces import (
EarlyExit,
IDependencyInjector,
Expand All @@ -41,6 +43,23 @@
)


_Self = TypeVar("_Self")


class _Numeric(Protocol):
def __float__(self) -> float:
...

def __lt__(self: _Self, other: _Self) -> bool:
...

def __gt__(self: _Self, other: _Self) -> bool:
...


_N = TypeVar("_N", bound=_Numeric)


class CrossSiteRequestForgery(Resource):
"""
Cross site request forgery detected. Request aborted.
Expand Down Expand Up @@ -255,12 +274,46 @@ def hidden(cls, name: str, value: str, **kw: Any) -> "Field":
**kw,
).maybeNamed(name)

@overload
@classmethod
def number(
cls,
minimum: Optional[float] = None,
maximum: Optional[float] = None,
kind: Callable[[str], float] = float,
**kw: Any,
) -> "Field":
...

@overload
@classmethod
def number(
cls,
minimum: Optional[_N] = None,
maximum: Optional[_N] = None,
*,
kind: Callable[[str], _N],
**kw: Any,
) -> "Field":
...

@overload
@classmethod
def number(
cls,
minimum: _N,
maximum: _N,
kind: Callable[[str], _N],
**kw: Any,
) -> "Field":
...

@classmethod
def number(
cls,
minimum: Optional[int] = None,
maximum: Optional[int] = None,
kind: Type = float,
minimum: Optional[_N] = None,
maximum: Optional[_N] = None,
kind: Callable[[str], _N] = float, # type:ignore[assignment]
**kw: Any,
) -> "Field":
"""
Expand Down

0 comments on commit a8c9e69

Please sign in to comment.