Skip to content

Commit 2101bca

Browse files
author
Sylvain MARIE
committed
Fixed an issue where using partial on a generator function in python 3.5 was raising a SyntaxError. Fixed #79
1 parent e4f4b5b commit 2101bca

File tree

4 files changed

+45
-26
lines changed

4 files changed

+45
-26
lines changed

src/makefun/_main_latest_py.py renamed to src/makefun/_main_py35_and_higher.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,3 @@ def partial_f(*args, **kwargs):
2222
kwargs.update(preset_kwargs) # for python 3.4: explicit dict update
2323
yield from f(*chain(preset_pos_args, args), **kwargs)
2424
return partial_f
25-
26-
27-
def make_partial_using_async_for_in_yield(new_sig, f, *preset_pos_args, **preset_kwargs):
28-
"""
29-
Makes a 'partial' when f is a async generator and python is new enough to support `async for v in f(): yield v`
30-
31-
:param new_sig:
32-
:param f:
33-
:param presets:
34-
:return:
35-
"""
36-
37-
@wraps(f, new_sig=new_sig)
38-
async def partial_f(*args, **kwargs):
39-
kwargs.update(preset_kwargs)
40-
async for v in f(*chain(preset_pos_args, args), **kwargs):
41-
yield v
42-
43-
return partial_f

src/makefun/_main_py36_and_higher.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Authors: Sylvain MARIE <[email protected]>
2+
# + All contributors to <https://github.com/smarie/python-makefun>
3+
#
4+
# License: 3-clause BSD, <https://github.com/smarie/python-makefun/blob/master/LICENSE>
5+
from itertools import chain
6+
7+
from makefun.main import wraps
8+
9+
10+
def make_partial_using_async_for_in_yield(new_sig, f, *preset_pos_args, **preset_kwargs):
11+
"""
12+
Makes a 'partial' when f is a async generator and python is new enough to support `async for v in f(): yield v`
13+
14+
:param new_sig:
15+
:param f:
16+
:param presets:
17+
:return:
18+
"""
19+
20+
@wraps(f, new_sig=new_sig)
21+
async def partial_f(*args, **kwargs):
22+
kwargs.update(preset_kwargs)
23+
async for v in f(*chain(preset_pos_args, args), **kwargs):
24+
yield v
25+
26+
return partial_f

src/makefun/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,13 +1127,13 @@ def partial(f, # type: Callable
11271127

11281128
if _is_generator_func(f):
11291129
if sys.version_info >= (3, 3):
1130-
from makefun._main_latest_py import make_partial_using_yield_from
1130+
from makefun._main_py35_and_higher import make_partial_using_yield_from
11311131
partial_f = make_partial_using_yield_from(new_sig, f, *preset_pos_args, **preset_kwargs)
11321132
else:
11331133
from makefun._main_legacy_py import make_partial_using_yield
11341134
partial_f = make_partial_using_yield(new_sig, f, *preset_pos_args, **preset_kwargs)
11351135
elif isasyncgenfunction(f) and sys.version_info >= (3, 6):
1136-
from makefun._main_latest_py import make_partial_using_async_for_in_yield
1136+
from makefun._main_py36_and_higher import make_partial_using_async_for_in_yield
11371137
partial_f = make_partial_using_async_for_in_yield(new_sig, f, *preset_pos_args, **preset_kwargs)
11381138
else:
11391139
@wraps(f, new_sig=new_sig)

tests/test_partial_and_macros.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,19 @@ def f(a, b, c, **d):
174174
# assert str(signature(fp_ref)) == str(signature(fp))
175175

176176

177-
def test_simple_partial_copy():
178-
"""Test that when not providing any argument to partial, it is equivalent to wraps with new sig = None"""
179-
def f1(a):
180-
return a + 1
177+
@pytest.mark.parametrize("is_generator", [False, True])
178+
def test_simple_partial_copy(is_generator):
179+
"""Test that when not providing any argument to partial, it is equivalent to wraps with new sig = None
180+
181+
This test was extended to cover issue 79.
182+
"""
183+
184+
if is_generator:
185+
def f1(a):
186+
yield a + 1
187+
else:
188+
def f1(a):
189+
return a + 1
181190

182191
f2 = makefun.partial(f1)
183192

@@ -188,7 +197,10 @@ def f1(a):
188197
f3 = makefun.wraps(f1)(f1)
189198
assert f3.__wrapped__ == f1
190199

191-
assert f2(1) == f3(1) == 2
200+
if is_generator:
201+
assert next(f2(1)) == next(f3(1)) == 2
202+
else:
203+
assert f2(1) == f3(1) == 2
192204

193205
# the func attribute is there too
194206
f4 = functools.partial(f1)

0 commit comments

Comments
 (0)