Skip to content

Commit

Permalink
Invert the logic and rename the function to 'bypass_unless'
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed May 29, 2023
1 parent 8b71f1a commit 1ca6d75
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
v3.7.0
======

Added ``pass_through_when``.
Added ``bypass_unless``.

v3.6.0
======
Expand Down
12 changes: 6 additions & 6 deletions jaraco/functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,26 +556,26 @@ def wrapper(*args, **kwargs):
return decorate


def pass_through_when(check):
def bypass_unless(check):
"""
Decorate a function to return its parameter when ``check``.
Decorate a function to return its parameter unless ``check``.
>>> pass_through = [] # False
>>> enabled = [object()] # True
>>> @pass_through_when(pass_through)
>>> @bypass_unless(enabled)
... def double(x):
... return x * 2
>>> double(2)
4
>>> pass_through[:] = [object()] # True
>>> del enabled[:] # False
>>> double(2)
2
"""

def decorate(func):
@functools.wraps(func)
def wrapper(param):
return param if check else func(param)
return func(param) if check else param

return wrapper

Expand Down

0 comments on commit 1ca6d75

Please sign in to comment.