Skip to content

Commit

Permalink
Might as well cache _has_unknown_args in the curried object.
Browse files Browse the repository at this point in the history
  • Loading branch information
eriknw committed Apr 9, 2016
1 parent 75a0733 commit fd77029
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions toolz/functoolz.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ def __init__(self, *args, **kwargs):
self.__doc__ = getattr(func, '__doc__', None)
self.__name__ = getattr(func, '__name__', '<curry>')
self._sigspec = None
self._has_unknown_args = None

@property
def func(self):
Expand Down Expand Up @@ -200,20 +201,21 @@ def _should_curry(self, args, kwargs, exc=None):
kwargs = dict(self.keywords, **kwargs)
if self._sigspec is None:
sigspec = self._sigspec = _signature_or_spec(func)
self._has_unknown_args = _has_unknown_args(func, sigspec=sigspec)
else:
sigspec = self._sigspec

if is_partial_args(func, args, kwargs, sigspec=sigspec) is False:
# Nothing can make the call valid
return False
elif self._has_unknown_args:
# The call may be valid and raised a TypeError, but we curry
# anyway because the function may have `*args`. This is useful
# for decorators with signature `func(*args, **kwargs)`.
return True
elif not is_valid_args(func, args, kwargs, sigspec=sigspec):
# Adding more arguments may make the call valid
return True
elif _has_unknown_args(func, sigspec=sigspec):
# The call was valid and raised a TypeError, but we curry anyway
# because the function may have `*args`. This is convenient for
# decorators with signature `func(*args, **kwargs)`.
return True
else:
# There was a genuine TypeError
return False
Expand Down

0 comments on commit fd77029

Please sign in to comment.