Skip to content

Commit 3daf182

Browse files
committed
deprecate do_return.
Should have just deleted it before releasing 1.0, but I forgot. Oh well!
1 parent 6e35ad9 commit 3daf182

File tree

5 files changed

+43
-44
lines changed

5 files changed

+43
-44
lines changed

effect/_test_do_py3.py

Lines changed: 0 additions & 11 deletions
This file was deleted.

effect/do.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"""
66

77
import types
8+
import warnings
89

910
from . import Effect, Func
1011
from ._utils import wraps
@@ -18,19 +19,15 @@ def do(f):
1819
@do
1920
def foo():
2021
thing = yield Effect(Constant(1))
21-
yield do_return('the result was %r' % (thing,))
22+
return 'the result was %r' % (thing,)
2223
2324
eff = foo()
2425
return eff.on(...)
2526
2627
``@do`` must decorate a generator function (not any other type of
27-
iterator). Any yielded values must either be Effects or the result of a
28-
:func:`do_return` call. The result of a yielded Effect will be passed back
29-
into the generator as the result of the ``yield`` expression. Yielded
30-
:func:`do_return` values will provide the ultimate result of the Effect
31-
that is returned by the decorated function. Note that :func:`do_return` is
32-
only necessary for Python 2 compatibility; return statements can be used
33-
directly in Python 3-only code.
28+
iterator). Any yielded values must be Effects. The result of a yielded Effect will be passed
29+
back into the generator as the result of the ``yield`` expression. A returned value becomes the
30+
ultimate result of the Effect that is returned by the decorated function.
3431
3532
It's important to note that any generator function decorated by ``@do``
3633
will no longer return a generator, but instead it will return an Effect,
@@ -43,7 +40,7 @@ def foo():
4340
try:
4441
thing = yield Effect(Error(RuntimeError('foo')))
4542
except RuntimeError:
46-
yield do_return('got a RuntimeError as expected')
43+
return 'got a RuntimeError as expected'
4744
4845
(This decorator is named for Haskell's ``do`` notation, which is similar in
4946
spirit).
@@ -77,15 +74,19 @@ def do_return(val):
7774
"""
7875
Specify a return value for a @do function.
7976
77+
This is deprecated. Just use `return`.
78+
8079
The result of this function must be yielded. e.g.::
8180
8281
@do
8382
def foo():
8483
yield do_return('hello')
85-
86-
If you're writing Python 3-only code, you don't need to use this function,
87-
and can just use the `return` statement as normal.
8884
"""
85+
warnings.warn(
86+
"do_return is deprecated. Just return as normal.",
87+
DeprecationWarning,
88+
stacklevel=1,
89+
)
8990
return _ReturnSentinel(val)
9091

9192

@@ -119,6 +120,6 @@ def _do(result, generator, is_error):
119120
)
120121
else:
121122
raise TypeError(
122-
"@do functions must only yield Effects or results of do_return. "
123+
"@do functions must only yield Effects. "
123124
"Got %r from %r" % (val, generator)
124125
)

effect/test_do.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import sys
22
from functools import partial
33

4-
from py.test import raises
4+
from py.test import raises, warns
55

66
from . import (
77
ComposedDispatcher,
@@ -34,19 +34,25 @@ def test_do_return():
3434
"""
3535
When a @do function yields a do_return, the given value becomes the
3636
eventual result.
37+
38+
This is deprecated.
3739
"""
3840

3941
@do
4042
def f():
4143
yield do_return("hello")
4244

43-
assert perf(f()) == "hello"
45+
with warns(DeprecationWarning):
46+
assert perf(f()) == "hello"
4447

4548

46-
def test_do_return_effect():
49+
def test_return_effect():
4750
@do
4851
def f():
49-
yield do_return(Effect(Constant("hello")))
52+
return Effect(Constant("hello"))
53+
# this is a dumb trick we're playing on Python to make sure this function is a generator,
54+
# even though we never want to yield anything.
55+
yield
5056

5157
assert perf(f()) == "hello"
5258

@@ -57,7 +63,7 @@ def test_yield_effect():
5763
@do
5864
def f():
5965
x = yield Effect(Constant(3))
60-
yield do_return(x)
66+
return x
6167

6268
perf(f()) == 3
6369

@@ -83,8 +89,7 @@ def f():
8389
with raises(TypeError) as err_info:
8490
perf(result)
8591
assert str(err_info.value).startswith(
86-
"@do functions must only yield Effects or results of "
87-
"do_return. Got 1 from <generator object"
92+
"@do functions must only yield Effects. Got 1 from <generator object"
8893
)
8994

9095

@@ -100,7 +105,7 @@ def f():
100105
yield Effect(Error(ZeroDivisionError("foo")))
101106
except Exception as e:
102107
got_error = e
103-
yield do_return(got_error)
108+
return got_error
104109

105110
exc = perf(f())
106111
assert type(exc) is ZeroDivisionError
@@ -114,7 +119,7 @@ def test_works_with_sync_perform():
114119
@do
115120
def perform_myintent(dispatcher, myintent):
116121
result = yield Effect(Constant(1))
117-
yield do_return(result + 1)
122+
return result + 1
118123

119124
class MyIntent(object):
120125
pass
@@ -133,7 +138,7 @@ def test_promote_metadata():
133138

134139
def original(dispatcher, intent):
135140
"""Original!"""
136-
yield do_return(1)
141+
return 1
137142

138143
original.attr = 1
139144
wrapped = do(original)
@@ -166,7 +171,7 @@ def test_repeatable_effect():
166171
@do
167172
def f():
168173
x = yield Effect(Constant("foo"))
169-
yield do_return(x)
174+
return x
170175

171176
eff = f()
172177
assert perf(eff) == "foo"
@@ -196,7 +201,11 @@ def f():
196201

197202
def test_py3_return():
198203
"""The `return x` syntax in Py3 sets the result of the Effect to `x`."""
199-
from effect._test_do_py3 import py3_generator_with_return
204+
205+
@do
206+
def py3_generator_with_return():
207+
yield Effect(Constant(1))
208+
return 2 # noqa
200209

201210
eff = py3_generator_with_return()
202211
assert perf(eff) == 2

effect/test_testing.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
sync_perform,
1919
sync_performer,
2020
)
21-
from .do import do, do_return
21+
from .do import do
2222
from .fold import FoldError, sequence
2323
from .testing import (
2424
_ANY,
@@ -397,7 +397,7 @@ def test_perform_sequence():
397397
def code_under_test():
398398
r = yield Effect(MyIntent("a"))
399399
r2 = yield Effect(OtherIntent("b"))
400-
yield do_return((r, r2))
400+
return (r, r2)
401401

402402
seq = [
403403
(MyIntent("a"), lambda i: "result1"),
@@ -417,7 +417,7 @@ def test_perform_sequence_log():
417417
def code_under_test():
418418
r = yield Effect(MyIntent("a"))
419419
r2 = yield Effect(OtherIntent("b"))
420-
yield do_return((r, r2))
420+
return (r, r2)
421421

422422
seq = [(MyIntent("a"), lambda i: "result1")]
423423
with pytest.raises(AssertionError) as exc:
@@ -505,13 +505,13 @@ class WrappedIntent(object):
505505
def internal():
506506
yield Effect(1)
507507
yield Effect(2)
508-
yield do_return("wrap")
508+
return "wrap"
509509

510510
@do
511511
def code_under_test():
512512
r = yield Effect(WrappedIntent(internal(), "field"))
513513
r2 = yield Effect(MyIntent("a"))
514-
yield do_return((r, r2))
514+
return (r, r2)
515515

516516
seq = [
517517
(

effect/testing.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def perform_sequence(seq, eff, fallback_dispatcher=None):
4848
def code_under_test():
4949
r = yield Effect(MyIntent('a'))
5050
r2 = yield Effect(OtherIntent('b'))
51-
yield do_return((r, r2))
51+
return (r, r2)
5252
5353
def test_code():
5454
seq = [
@@ -142,7 +142,7 @@ def code_under_test():
142142
r = yield Effect(SerialIntent('serial'))
143143
r2 = yield parallel([Effect(MyIntent('a')),
144144
Effect(OtherIntent('b'))])
145-
yield do_return((r, r2))
145+
return (r, r2)
146146
147147
def test_code():
148148
seq = [
@@ -576,7 +576,7 @@ def intent_func(fname):
576576
def code_under_test(arg1, arg2, eff_returning_func=eff_returning_func):
577577
r = yield Effect(MyIntent('a'))
578578
r2 = yield eff_returning_func(arg1, arg2)
579-
yield do_return((r, r2))
579+
return (r, r2)
580580
581581
you will need to know the intents which ``eff_returning_func`` generates
582582
to test this using :func:`perform_sequence`. You can avoid that by doing::

0 commit comments

Comments
 (0)