-
Notifications
You must be signed in to change notification settings - Fork 120
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SupportsFloat
is a weird type for this (presumably in the context of Decimal
it's quite intentionally not being float
-ified), but it's more correct than Type
given how it is used, so we should probably merge this for now.
Hmm, I didn't notice mypy isn't happy with this. We might need our own protocol with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, looks like the mypy
failure here is real. @rouge8 looks like SupportsFloat
is sufficiently weird that it's actually wrong. Can you pick a type here that at least makes mypy happy? The best option here would be to write a numeric protocol, set it as a bound on a TypeVar
, then have minimum
, maximum
, and kind
all reference that var so as to ensure consistency.
Is |
That's a good point. The other mypy issue is |
For example, in our internal codebase, we have a function: ```python def decimal_from_str(value: str, parens_negate: bool = False) -> Decimal: ... amount = Field.number(kind=decimal_from_str) ```
4f0d01e
to
0e6d4c0
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## trunk #709 +/- ##
=======================================
Coverage 99.07% 99.07%
=======================================
Files 46 46
Lines 3986 4002 +16
Branches 531 540 +9
=======================================
+ Hits 3949 3965 +16
Misses 23 23
Partials 14 14 ☔ View full report in Codecov by Sentry. |
@glyph updated with a diff --git a/src/klein/_form.py b/src/klein/_form.py
index da93a33..04de050 100644
--- a/src/klein/_form.py
+++ b/src/klein/_form.py
@@ -56,6 +56,9 @@ class _Numeric(Protocol[_T]):
...
+_N = TypeVar("_N", bound=_Numeric)
+
+
class CrossSiteRequestForgery(Resource):
"""
Cross site request forgery detected. Request aborted.
@@ -273,9 +276,9 @@ class Field:
@classmethod
def number(
cls,
- minimum: Optional[_Numeric] = None,
- maximum: Optional[_Numeric] = None,
- kind: Callable[[str], _Numeric] = float,
+ minimum: Optional[_N] = None,
+ maximum: Optional[_N] = None,
+ kind: Callable[[str], _N] = float,
**kw: Any,
) -> "Field":
"""
|
@rouge8 to try to better explain the structure I was looking for, I opened a PR against your PR: rouge8#1 . Numerics aren't generic, because a numeric has to work with itself and (in this context, at least) only itself. The goofy verbose |
Re: the coverage failure, I don't really have time to look at it now, but there should be a coverage ignore pattern you can grab for |
de-genericize Numeric type
Ohhh, thanks for the fix! I'll add the coverage ignore tomorrow during the workday. |
@@ -15,3 +15,4 @@ source= | |||
exclude_lines = | |||
pragma: no cover | |||
if TYPE_CHECKING: | |||
\s*\.\.\.$ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I guess my change ended up being the change here :)
For example, in our internal codebase, we have a function: