From a60ced7a931a514a79f5d0111fbe5dc0fc0c1d10 Mon Sep 17 00:00:00 2001 From: Donatas Rasiukevicius Date: Sat, 25 May 2019 21:06:24 +0200 Subject: [PATCH] added run_callable argument to allow returning unalled functions --- pampy/pampy.py | 4 ++-- tests/test_basic.py | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/pampy/pampy.py b/pampy/pampy.py index 1cb7746..54c5a13 100644 --- a/pampy/pampy.py +++ b/pampy/pampy.py @@ -142,7 +142,7 @@ def match_iterable(patterns, values) -> Tuple[bool, List]: -def match(var, *args, default=NoDefault, strict=True): +def match(var, *args, default=NoDefault, strict=True, run_callable=True): """ Match `var` against a number of potential patterns. @@ -181,7 +181,7 @@ def match(var, *args, default=NoDefault, strict=True): if matched_as_value: lambda_args = args if len(args) > 0 else BoxedArgs(var) - return run(action, lambda_args) + return run(action, lambda_args) if run_callable else action if default is NoDefault: if _ not in patterns: diff --git a/tests/test_basic.py b/tests/test_basic.py index c7114f0..2cd61a3 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -152,4 +152,9 @@ class Color(Enum): self.assertEqual(match(Color.RED, Color.BLUE, "blue", Color.RED, "red", _, "else"), "red") self.assertEqual(match(Color.RED, Color.BLUE, "blue", Color.GREEN, "green", _, "else"), "else") self.assertEqual(match(1, Color.BLUE, "blue", Color.GREEN, "green", _, "else"), "else") - \ No newline at end of file + + def test_match_no_run(self): + self.assertEqual(match(2, 2, lambda: 0, run_callable=False)(), 0) + def fn(): + return "xyz" + self.assertEqual(match(2, 2, fn, run_callable=False), fn)