From a65082ea50a59c58238190d063c94872336f3ffb Mon Sep 17 00:00:00 2001 From: Brian Larsen Date: Sat, 27 May 2023 17:33:17 -0500 Subject: [PATCH] Improve flip to accept *args and kwargs --- toolz/_signatures.py | 2 +- toolz/functoolz.py | 4 ++-- toolz/tests/test_functoolz.py | 7 +++++++ 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/toolz/_signatures.py b/toolz/_signatures.py index ff536327..4c855df6 100644 --- a/toolz/_signatures.py +++ b/toolz/_signatures.py @@ -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=[ diff --git a/toolz/functoolz.py b/toolz/functoolz.py index 2c75d3a4..8bfba588 100644 --- a/toolz/functoolz.py +++ b/toolz/functoolz.py @@ -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. @@ -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): diff --git a/toolz/tests/test_functoolz.py b/toolz/tests/test_functoolz.py index 555cf48d..ec2a3f22 100644 --- a/toolz/tests/test_functoolz.py +++ b/toolz/tests/test_functoolz.py @@ -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'