Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Field.number(kind=...) supports any function that returns a number #709

Merged
merged 5 commits into from
Mar 12, 2024
Merged
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
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*\.\.\.$
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Loading