-
-
Notifications
You must be signed in to change notification settings - Fork 348
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
Switch to using standalone Outcome library. #503
Merged
Merged
Changes from 6 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
e27b50a
Switch to using standalone Outcome library.
Fuyukai d645b06
Rip out Result code from the rest of the core.
Fuyukai c7f4c67
Remove certain references in old changelog entries.
Fuyukai 04452e5
Add outcome to intersphinx mapping
Fuyukai 005b633
Move deprecated attributes to the right place.
Fuyukai 6587bbf
Fix RTD link
Fuyukai cad04ae
Solve the curse of the mutable default
Fuyukai 89f3150
Fix reschedule docstring
Fuyukai d49962c
Remove broken test code (this was basically testing a different libra…
Fuyukai 7bc2a18
Fix some usages of _core.Value/Error and default Value values.
Fuyukai 768fdcf
Missing newline
Fuyukai 58a431a
Simplify socket_ready helper a bit
njsmith 83fec3f
Remove vestigial enable_attribute_deprecations call
njsmith ed0510a
Fix __deprecated_attributes__ usage
njsmith 0cbed90
Re-YAPFify
Fuyukai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Switch to using standalone | ||
`Outcome <https://github.com/python-trio/outcome>`__ library for Result | ||
objects. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,125 +1,33 @@ | ||
import abc | ||
import attr | ||
import outcome | ||
|
||
__all__ = ["Result", "Value", "Error"] | ||
|
||
|
||
class Result(metaclass=abc.ABCMeta): | ||
"""An abstract class representing the result of a Python computation. | ||
|
||
This class has two concrete subclasses: :class:`Value` representing a | ||
value, and :class:`Error` representing an exception. | ||
|
||
In addition to the methods described below, comparison operators on | ||
:class:`Value` and :class:`Error` objects (``==``, ``<``, etc.) check that | ||
the other object is also a :class:`Value` or :class:`Error` object | ||
respectively, and then compare the contained objects. | ||
|
||
:class:`Result` objects are hashable if the contained objects are | ||
hashable. | ||
|
||
""" | ||
__slots__ = () | ||
|
||
@staticmethod | ||
def capture(sync_fn, *args): | ||
"""Run ``sync_fn(*args)`` and capture the result. | ||
|
||
Returns: | ||
Either a :class:`Value` or :class:`Error` as appropriate. | ||
|
||
""" | ||
try: | ||
return Value(sync_fn(*args)) | ||
except BaseException as exc: | ||
return Error(exc) | ||
|
||
@staticmethod | ||
async def acapture(async_fn, *args): | ||
"""Run ``await async_fn(*args)`` and capture the result. | ||
|
||
Returns: | ||
Either a :class:`Value` or :class:`Error` as appropriate. | ||
|
||
""" | ||
try: | ||
return Value(await async_fn(*args)) | ||
except BaseException as exc: | ||
return Error(exc) | ||
|
||
@abc.abstractmethod | ||
def unwrap(self): | ||
"""Return or raise the contained value or exception. | ||
|
||
These two lines of code are equivalent:: | ||
|
||
x = fn(*args) | ||
x = Result.capture(fn, *args).unwrap() | ||
|
||
""" | ||
|
||
@abc.abstractmethod | ||
def send(self, gen): | ||
"""Send or throw the contained value or exception into the given | ||
generator object. | ||
|
||
Args: | ||
gen: A generator object supporting ``.send()`` and ``.throw()`` | ||
methods. | ||
|
||
""" | ||
|
||
@abc.abstractmethod | ||
async def asend(self, agen): | ||
"""Send or throw the contained value or exception into the given async | ||
generator object. | ||
|
||
Args: | ||
agen: An async generator object supporting ``.asend()`` and | ||
``.athrow()`` methods. | ||
|
||
""" | ||
|
||
|
||
@attr.s(frozen=True, repr=False) | ||
class Value(Result): | ||
"""Concrete :class:`Result` subclass representing a regular value. | ||
|
||
""" | ||
|
||
value = attr.ib() | ||
"""The contained value.""" | ||
|
||
def __repr__(self): | ||
return "Value({!r})".format(self.value) | ||
|
||
def unwrap(self): | ||
return self.value | ||
|
||
def send(self, gen): | ||
return gen.send(self.value) | ||
|
||
async def asend(self, agen): | ||
return await agen.asend(self.value) | ||
from .. import _deprecate | ||
|
||
__all__ = ["Result", "Value", "Error"] | ||
|
||
@attr.s(frozen=True, repr=False) | ||
class Error(Result): | ||
"""Concrete :class:`Result` subclass representing a raised exception. | ||
_deprecate.enable_attribute_deprecations(__name__) | ||
|
||
""" | ||
|
||
error = attr.ib(validator=attr.validators.instance_of(BaseException)) | ||
"""The contained exception object.""" | ||
class Result(outcome.Outcome): | ||
@classmethod | ||
@_deprecate.deprecated( | ||
version="0.5.0", issue=494, instead="outcome.capture" | ||
) | ||
def capture(cls, sync_fn, *args): | ||
return outcome.capture(sync_fn, *args) | ||
|
||
def __repr__(self): | ||
return "Error({!r})".format(self.error) | ||
@classmethod | ||
@_deprecate.deprecated( | ||
version="0.5.0", issue=494, instead="outcome.acapture" | ||
) | ||
async def acapture(cls, async_fn, *args): | ||
return await outcome.acapture(async_fn, *args) | ||
|
||
def unwrap(self): | ||
raise self.error | ||
|
||
def send(self, it): | ||
return it.throw(self.error) | ||
# alias these types so they don't mysteriously disappear | ||
Value = outcome.Value | ||
Error = outcome.Error | ||
|
||
async def asend(self, agen): | ||
return await agen.athrow(self.error) | ||
# ensure that isinstance(Value(), Result)/issubclass(Value, Result) and etc | ||
# don't break | ||
Result.register(Value) | ||
Result.register(Error) | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Ugh. Could you please keep the newline?
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.
Whoops, missed that. Surprised yapf didn't catch it.