-
Notifications
You must be signed in to change notification settings - Fork 78
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
Add decorator to turn regular functions in Result returning ones #71
Conversation
01c8a50
to
4faa486
Compare
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.
Amazing. Very nice 👍
Don't forget a changelog entry
What does this mean for Python <3.10 users? Ideally, we'd like to keep the library usable even if less functional/type checked |
2ea0cf6
to
cbb57c3
Compare
i've updated the pull request. the description / commit message has more information about this, and a similar note is in a comment in the code. what do you think? |
update: see comment below; this is not entirely true at the moment another problem is mypy: it runs in CI using different python versions, and mypy on different python versions has different support for python features, so that means it will not recognize some things. in particular, mypy on some python versions needs ‘type: ignore’ overrides, while on the latest python version it must not have these overriding comments (because this is not fixable by adding more code. i think the only way is to only run mypy on python 3.10 in CI? (or at least as a ‘required check’). 🤔 |
cbb57c3
to
1f33ee5
Compare
ok, i got CI to pass ✅✅✅✅✅. locally i was running an unreleased mypy version (to check out its partial |
Had a quick look, it looks good. |
cc @francium the situation is better than i initially thought; see #71 (comment) status is:
so apart from docs/changelog, i think this code is fine to merge. what about the name, is |
|
So I understand what you've stated here as users can opt into using this decorator, but until mypy provides better typing, they'll have to accept untyped nature of this due to lack of ParamSpec support |
Looks good |
1f33ee5
to
49040c1
Compare
2c3aae2
to
3f04465
Compare
🥳 mypy now has improved (but incomplete) PEP612 support: @as_result(ValueError)
def good(value: int) -> int:
return value
@as_result(IndexError, ValueError)
def bad(value: int) -> int:
raise ValueError
reveal_type(good)
reveal_type(bad) mypy 0.910 complains a lot and makes everything
mypy 0.921 complains a lot but does recognize the return types:
mypy 0.930 is much happier:
|
3f04465
to
ce6bc72
Compare
Codecov Report
@@ Coverage Diff @@
## master #71 +/- ##
===========================================
- Coverage 100.00% 99.03% -0.97%
===========================================
Files 3 3
Lines 252 308 +56
===========================================
+ Hits 252 305 +53
- Misses 0 3 +3
|
as_result
decorator to turn regular functions into Result
returning ones
9c78f34
to
bd61192
Compare
bd61192
to
fd95812
Compare
@francium pls have a look again. lots of and i even added docs |
fd95812
to
9d66177
Compare
9d66177
to
894296e
Compare
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.
The only thing I think we still need to consider is mentioning publicly that mypy has a limitation regarding the type checking and users will currently have to accept Any
s.
Do you think that should be mentioned in the README (FAQ section?) or in the CHANGELOG entry?
Add a as_result() helper to make a decorator to turn a function into one that returns a Result: Regular return values are turned into Ok(return_value). Raised exceptions of the specified exception type(s) are turned into Err(exc). The decorator is signature-preserving, except for wrapping the return type into a Result, of course. For type annotations, this depends on typing.ParamSpec which requires Python 3.10+ (or use typing_extensions); see PEP612 (https://www.python.org/dev/peps/pep-0612/). This is currently not fully supported by Mypy; see python/mypy#8645 Calling decorated functions works without errors from Mypy, but will not be type-safe, i.e. it will behave as if it is calling a function like f(*args: Any, **kwargs: Any) Fixes rustedpy#33.
894296e
to
27e3f55
Compare
unless i'm missing something, this actually works well with mypy 0.390; see bottom of #71 (comment) the incomplete mypy support (python/mypy#8645) seems to be about things that don't affect i've removed the misleading comments from the implementation; this was true until mypy 0.930 but not anymore. |
Sorry, I must've misunderstood. Great. |
as_result
decorator to turn regular functions into Result
returning ones
fyi, i've updated the pr description and the (squashed) commit message to no longer refer to the behaviour of old mypy versions. |
Add decorator to turn regular functions in Result returning ones
Add a as_result() helper to make a decorator to turn a function into one
that returns a Result: Regular return values are turned into
Ok(return_value). Raised exceptions of the specified exception type(s)
are turned into Err(exc).
The decorator is signature-preserving, except for wrapping the return
type into a Result, of course.
For type annotations, this depends on typing.ParamSpec which requires
Python 3.10+ (or use typing_extensions); see
PEP612 (https://www.python.org/dev/peps/pep-0612/).
Fixes #33.