From debb84db2d8186b6e30dff43e97559005551ceaf Mon Sep 17 00:00:00 2001 From: Giulio <3272563+giuliohome@users.noreply.github.com> Date: Tue, 5 Nov 2024 10:29:36 +0100 Subject: [PATCH 1/4] See https://github.com/python-effect/effect/issues/86#issuecomment-2456577779 --- effect/__init__.py | 4 ++-- effect/_base.py | 20 ++++++++++---------- effect/_continuation.py | 8 ++++---- effect/_sync.py | 4 ++-- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/effect/__init__.py b/effect/__init__.py index b03abfd..4db92c3 100644 --- a/effect/__init__.py +++ b/effect/__init__.py @@ -8,7 +8,7 @@ """ from ._base import Effect, perform, NoPerformerFoundError, catch, raise_ -from ._sync import NotSynchronousError, sync_perform, sync_performer +from ._sync import NotSynchronousError, async_perform, sync_performer from ._intents import ( Delay, perform_delay_with_sleep, @@ -27,7 +27,7 @@ __all__ = [ # Order here affects the order that these things show up in the API docs. "Effect", - "sync_perform", + "async_perform", "sync_performer", "base_dispatcher", "TypeDispatcher", diff --git a/effect/_base.py b/effect/_base.py index efaef5c..187e8eb 100644 --- a/effect/_base.py +++ b/effect/_base.py @@ -47,11 +47,11 @@ def __init__(self, cont): """ self._cont = cont - def succeed(self, result): + async def succeed(self, result): """ Indicate that the effect has succeeded, and the result is available. """ - self._cont((False, result)) + await self._cont((False, result)) def fail(self, result): """ @@ -77,7 +77,7 @@ class NoPerformerFoundError(Exception): """Raised when a performer for an intent couldn't be found.""" -def perform(dispatcher, effect): +async def perform(dispatcher, effect): """ Perform an effect and invoke callbacks bound to it. You probably don't want to use this. Instead, use :func:`sync_perform` (or, if you're using @@ -120,11 +120,11 @@ def perform(dispatcher, effect): an exception. Decorators like :func:`sync_performer` simply abstract this away. """ - def _run_callbacks(bouncer, chain, result): + async def _run_callbacks(bouncer, chain, result): is_error, value = result if type(value) is Effect: - bouncer.bounce( + await bouncer.bounce( _perform, Effect(value.intent, callbacks=value.callbacks + chain) ) return @@ -136,15 +136,15 @@ def _run_callbacks(bouncer, chain, result): if cb is not None: result = guard(cb, value) chain = chain[1:] - bouncer.bounce(_run_callbacks, chain, result) + await bouncer.bounce(_run_callbacks, chain, result) - def _perform(bouncer, effect): + async def _perform(bouncer, effect): try: performer = dispatcher(effect.intent) if performer is None: raise NoPerformerFoundError(effect.intent) else: - performer( + await performer( dispatcher, effect.intent, _Box(partial(bouncer.bounce, _run_callbacks, effect.callbacks)), @@ -152,7 +152,7 @@ def _perform(bouncer, effect): except Exception as e: _run_callbacks(bouncer, effect.callbacks, (True, e)) - trampoline(_perform, effect) + await trampoline(_perform, effect) def catch(exc_type, callable): @@ -163,7 +163,7 @@ def catch(exc_type, callable): lambda exc: "got an error!")) If any exception other than a ``SpecificException`` is thrown, it will be - ignored by this handler and propagate further down the chain of callbacks. + ignored by this handler and propogate further down the chain of callbacks. """ def catcher(error): diff --git a/effect/_continuation.py b/effect/_continuation.py index 042303c..b3cc59c 100644 --- a/effect/_continuation.py +++ b/effect/_continuation.py @@ -5,7 +5,7 @@ class Bouncer(object): work = None _asynchronous = False - def bounce(self, func, *args, **kwargs): + async def bounce(self, func, *args, **kwargs): """ Bounce a function off the trampoline -- in other words, signal to the trampoline that the given function should be run. It will be passed a @@ -23,11 +23,11 @@ def bounce(self, func, *args, **kwargs): ) self.work = (func, args, kwargs) if self._asynchronous: - trampoline(func, *args, **kwargs) + await trampoline(func, *args, **kwargs) return -def trampoline(f, *args, **kwargs): +async def trampoline(f, *args, **kwargs): """ An asynchronous trampoline. @@ -54,7 +54,7 @@ def trampoline(f, *args, **kwargs): """ while True: bouncer = Bouncer() - f(bouncer, *args, **kwargs) + await f(bouncer, *args, **kwargs) if bouncer.work is not None: f, args, kwargs = bouncer.work else: diff --git a/effect/_sync.py b/effect/_sync.py index f7a8cdd..59afa32 100644 --- a/effect/_sync.py +++ b/effect/_sync.py @@ -12,7 +12,7 @@ class NotSynchronousError(Exception): """Performing an effect did not immediately return a value.""" -def sync_perform(dispatcher, effect): +async def async_perform(dispatcher, effect): """ Perform an effect, and return its ultimate result. If the final result is an error, the exception will be raised. @@ -24,7 +24,7 @@ def sync_perform(dispatcher, effect): successes = [] errors = [] effect = effect.on(success=successes.append, error=errors.append) - perform(dispatcher, effect) + await perform(dispatcher, effect) if successes: return successes[0] elif errors: From c0e142fcde6b09e6dcdf621b35e81c45cf807615 Mon Sep 17 00:00:00 2001 From: Giulio <3272563+giuliohome@users.noreply.github.com> Date: Tue, 5 Nov 2024 10:45:49 +0100 Subject: [PATCH 2/4] previous bugfixes of typos from master after bump to 1.1.0 --- effect/_test_do_py3.py | 11 +++++++++++ effect/fold.py | 2 +- effect/test_base.py | 4 ++-- effect/test_parallel_performers.py | 2 +- 4 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 effect/_test_do_py3.py diff --git a/effect/_test_do_py3.py b/effect/_test_do_py3.py new file mode 100644 index 0000000..1ac1df6 --- /dev/null +++ b/effect/_test_do_py3.py @@ -0,0 +1,11 @@ +# This code only works in Python 3, so it's left out of test_do.py, to be +# optionally imported. + +from effect import Constant, Effect +from effect.do import do + + +@do +def py3_generator_with_return(): + yield Effect(Constant(1)) + return 2 # noqa diff --git a/effect/fold.py b/effect/fold.py index ec92e53..65aa6ae 100644 --- a/effect/fold.py +++ b/effect/fold.py @@ -73,7 +73,7 @@ def sequence(effects): fails. """ # Could be: folder = lambda acc, el: acc + [el] - # But, for performance: + # But, for peformance: result = [] def folder(acc, el): diff --git a/effect/test_base.py b/effect/test_base.py index 3b1d9c4..68acc15 100644 --- a/effect/test_base.py +++ b/effect/test_base.py @@ -107,7 +107,7 @@ def test_performer_raises(self): def test_success_propagates_effect_exception(self): """ - If an success callback is specified, but a exception result occurs, + If an succes callback is specified, but a exception result occurs, the exception is passed to the next callback. """ calls = [] @@ -124,7 +124,7 @@ def test_success_propagates_effect_exception(self): def test_error_propagates_effect_result(self): """ - If an error callback is specified, but a successful result occurs, + If an error callback is specified, but a succesful result occurs, the success is passed to the next callback. """ calls = [] diff --git a/effect/test_parallel_performers.py b/effect/test_parallel_performers.py index 05b2dbf..e40129d 100644 --- a/effect/test_parallel_performers.py +++ b/effect/test_parallel_performers.py @@ -21,7 +21,7 @@ class ParallelPerformerTestsMixin(object): def test_empty(self): """ When given an empty list of effects, ``perform_parallel_async`` returns - an empty list synchronously. + an empty list synchronusly. """ result = sync_perform(self.dispatcher, parallel([])) self.assertEqual(result, []) From 07a351e4106b3bba6bbc2c10e3685a255f9dd72d Mon Sep 17 00:00:00 2001 From: Giulio <3272563+giuliohome@users.noreply.github.com> Date: Tue, 5 Nov 2024 10:46:51 +0100 Subject: [PATCH 3/4] previous bugfixes of typos from master after bump to 1.1.0 --- effect/_base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/effect/_base.py b/effect/_base.py index 187e8eb..36fe168 100644 --- a/effect/_base.py +++ b/effect/_base.py @@ -163,7 +163,7 @@ def catch(exc_type, callable): lambda exc: "got an error!")) If any exception other than a ``SpecificException`` is thrown, it will be - ignored by this handler and propogate further down the chain of callbacks. + ignored by this handler and propagate further down the chain of callbacks. """ def catcher(error): From 5ad5026a37ae40996d95b8d15e6d15d88f345d75 Mon Sep 17 00:00:00 2001 From: Giulio <3272563+giuliohome@users.noreply.github.com> Date: Tue, 5 Nov 2024 10:53:04 +0100 Subject: [PATCH 4/4] revert c0e142fc --- effect/_test_do_py3.py | 11 ----------- effect/fold.py | 2 +- effect/test_base.py | 4 ++-- effect/test_parallel_performers.py | 2 +- 4 files changed, 4 insertions(+), 15 deletions(-) delete mode 100644 effect/_test_do_py3.py diff --git a/effect/_test_do_py3.py b/effect/_test_do_py3.py deleted file mode 100644 index 1ac1df6..0000000 --- a/effect/_test_do_py3.py +++ /dev/null @@ -1,11 +0,0 @@ -# This code only works in Python 3, so it's left out of test_do.py, to be -# optionally imported. - -from effect import Constant, Effect -from effect.do import do - - -@do -def py3_generator_with_return(): - yield Effect(Constant(1)) - return 2 # noqa diff --git a/effect/fold.py b/effect/fold.py index 65aa6ae..ec92e53 100644 --- a/effect/fold.py +++ b/effect/fold.py @@ -73,7 +73,7 @@ def sequence(effects): fails. """ # Could be: folder = lambda acc, el: acc + [el] - # But, for peformance: + # But, for performance: result = [] def folder(acc, el): diff --git a/effect/test_base.py b/effect/test_base.py index 68acc15..3b1d9c4 100644 --- a/effect/test_base.py +++ b/effect/test_base.py @@ -107,7 +107,7 @@ def test_performer_raises(self): def test_success_propagates_effect_exception(self): """ - If an succes callback is specified, but a exception result occurs, + If an success callback is specified, but a exception result occurs, the exception is passed to the next callback. """ calls = [] @@ -124,7 +124,7 @@ def test_success_propagates_effect_exception(self): def test_error_propagates_effect_result(self): """ - If an error callback is specified, but a succesful result occurs, + If an error callback is specified, but a successful result occurs, the success is passed to the next callback. """ calls = [] diff --git a/effect/test_parallel_performers.py b/effect/test_parallel_performers.py index e40129d..05b2dbf 100644 --- a/effect/test_parallel_performers.py +++ b/effect/test_parallel_performers.py @@ -21,7 +21,7 @@ class ParallelPerformerTestsMixin(object): def test_empty(self): """ When given an empty list of effects, ``perform_parallel_async`` returns - an empty list synchronusly. + an empty list synchronously. """ result = sync_perform(self.dispatcher, parallel([])) self.assertEqual(result, [])