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

added run_callable argument to allow returning unalled functions #29

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
4 changes: 2 additions & 2 deletions pampy/pampy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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:
Expand Down
7 changes: 6 additions & 1 deletion tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")


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)