Skip to content
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

Improve flip to accept *args and kwargs #566

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion toolz/_signatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@
excepts=[
(0, lambda exc, func, handler=None: None)],
flip=[
(0, lambda func=None, a=None, b=None: None)],
(0, lambda func=None, *args, **kwargs: None)],
juxt=[
(0, lambda *funcs: None)],
memoize=[
Expand Down
4 changes: 2 additions & 2 deletions toolz/functoolz.py
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ def do(func, x):


@curry
def flip(func, a, b):
def flip(func, *args, **kwargs):
""" Call the function call with the arguments flipped

This function is curried.
Expand All @@ -728,7 +728,7 @@ def flip(func, a, b):
>>> only_ints
[1, 2, 3]
"""
return func(b, a)
return func(*reversed(args), **kwargs)


def return_none(exc):
Expand Down
7 changes: 7 additions & 0 deletions toolz/tests/test_functoolz.py
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,13 @@ def f(a, b):
assert flip(f, 'a', 'b') == ('b', 'a')


def test_flip_args_kwargs():
def g(a, b, c, *, d, e):
return a, b, c, d, e

assert flip(g, 3, 2, 1, d=4, e=5) == (1, 2, 3, 4, 5)


def test_excepts():
# These are descriptors, make sure this works correctly.
assert excepts.__name__ == 'excepts'
Expand Down