You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1075 lines
31 KiB

4 years ago
  1. from functools import reduce, partial
  2. import inspect
  3. import operator
  4. from operator import attrgetter
  5. from textwrap import dedent
  6. from .compatibility import PY3, PY33, PY34, PYPY, import_module
  7. from .utils import no_default
  8. __all__ = ('identity', 'thread_first', 'thread_last', 'memoize', 'compose',
  9. 'pipe', 'complement', 'juxt', 'do', 'curry', 'flip', 'excepts')
  10. def identity(x):
  11. """ Identity function. Return x
  12. >>> identity(3)
  13. 3
  14. """
  15. return x
  16. def thread_first(val, *forms):
  17. """ Thread value through a sequence of functions/forms
  18. >>> def double(x): return 2*x
  19. >>> def inc(x): return x + 1
  20. >>> thread_first(1, inc, double)
  21. 4
  22. If the function expects more than one input you can specify those inputs
  23. in a tuple. The value is used as the first input.
  24. >>> def add(x, y): return x + y
  25. >>> def pow(x, y): return x**y
  26. >>> thread_first(1, (add, 4), (pow, 2)) # pow(add(1, 4), 2)
  27. 25
  28. So in general
  29. thread_first(x, f, (g, y, z))
  30. expands to
  31. g(f(x), y, z)
  32. See Also:
  33. thread_last
  34. """
  35. def evalform_front(val, form):
  36. if callable(form):
  37. return form(val)
  38. if isinstance(form, tuple):
  39. func, args = form[0], form[1:]
  40. args = (val,) + args
  41. return func(*args)
  42. return reduce(evalform_front, forms, val)
  43. def thread_last(val, *forms):
  44. """ Thread value through a sequence of functions/forms
  45. >>> def double(x): return 2*x
  46. >>> def inc(x): return x + 1
  47. >>> thread_last(1, inc, double)
  48. 4
  49. If the function expects more than one input you can specify those inputs
  50. in a tuple. The value is used as the last input.
  51. >>> def add(x, y): return x + y
  52. >>> def pow(x, y): return x**y
  53. >>> thread_last(1, (add, 4), (pow, 2)) # pow(2, add(4, 1))
  54. 32
  55. So in general
  56. thread_last(x, f, (g, y, z))
  57. expands to
  58. g(y, z, f(x))
  59. >>> def iseven(x):
  60. ... return x % 2 == 0
  61. >>> list(thread_last([1, 2, 3], (map, inc), (filter, iseven)))
  62. [2, 4]
  63. See Also:
  64. thread_first
  65. """
  66. def evalform_back(val, form):
  67. if callable(form):
  68. return form(val)
  69. if isinstance(form, tuple):
  70. func, args = form[0], form[1:]
  71. args = args + (val,)
  72. return func(*args)
  73. return reduce(evalform_back, forms, val)
  74. def instanceproperty(fget=None, fset=None, fdel=None, doc=None, classval=None):
  75. """ Like @property, but returns ``classval`` when used as a class attribute
  76. >>> class MyClass(object):
  77. ... '''The class docstring'''
  78. ... @instanceproperty(classval=__doc__)
  79. ... def __doc__(self):
  80. ... return 'An object docstring'
  81. ... @instanceproperty
  82. ... def val(self):
  83. ... return 42
  84. ...
  85. >>> MyClass.__doc__
  86. 'The class docstring'
  87. >>> MyClass.val is None
  88. True
  89. >>> obj = MyClass()
  90. >>> obj.__doc__
  91. 'An object docstring'
  92. >>> obj.val
  93. 42
  94. """
  95. if fget is None:
  96. return partial(instanceproperty, fset=fset, fdel=fdel, doc=doc,
  97. classval=classval)
  98. return InstanceProperty(fget=fget, fset=fset, fdel=fdel, doc=doc,
  99. classval=classval)
  100. class InstanceProperty(property):
  101. """ Like @property, but returns ``classval`` when used as a class attribute
  102. Should not be used directly. Use ``instanceproperty`` instead.
  103. """
  104. def __init__(self, fget=None, fset=None, fdel=None, doc=None,
  105. classval=None):
  106. self.classval = classval
  107. property.__init__(self, fget=fget, fset=fset, fdel=fdel, doc=doc)
  108. def __get__(self, obj, type=None):
  109. if obj is None:
  110. return self.classval
  111. return property.__get__(self, obj, type)
  112. def __reduce__(self):
  113. state = (self.fget, self.fset, self.fdel, self.__doc__, self.classval)
  114. return InstanceProperty, state
  115. class curry(object):
  116. """ Curry a callable function
  117. Enables partial application of arguments through calling a function with an
  118. incomplete set of arguments.
  119. >>> def mul(x, y):
  120. ... return x * y
  121. >>> mul = curry(mul)
  122. >>> double = mul(2)
  123. >>> double(10)
  124. 20
  125. Also supports keyword arguments
  126. >>> @curry # Can use curry as a decorator
  127. ... def f(x, y, a=10):
  128. ... return a * (x + y)
  129. >>> add = f(a=1)
  130. >>> add(2, 3)
  131. 5
  132. See Also:
  133. toolz.curried - namespace of curried functions
  134. https://toolz.readthedocs.io/en/latest/curry.html
  135. """
  136. def __init__(self, *args, **kwargs):
  137. if not args:
  138. raise TypeError('__init__() takes at least 2 arguments (1 given)')
  139. func, args = args[0], args[1:]
  140. if not callable(func):
  141. raise TypeError("Input must be callable")
  142. # curry- or functools.partial-like object? Unpack and merge arguments
  143. if (
  144. hasattr(func, 'func')
  145. and hasattr(func, 'args')
  146. and hasattr(func, 'keywords')
  147. and isinstance(func.args, tuple)
  148. ):
  149. _kwargs = {}
  150. if func.keywords:
  151. _kwargs.update(func.keywords)
  152. _kwargs.update(kwargs)
  153. kwargs = _kwargs
  154. args = func.args + args
  155. func = func.func
  156. if kwargs:
  157. self._partial = partial(func, *args, **kwargs)
  158. else:
  159. self._partial = partial(func, *args)
  160. self.__doc__ = getattr(func, '__doc__', None)
  161. self.__name__ = getattr(func, '__name__', '<curry>')
  162. self.__module__ = getattr(func, '__module__', None)
  163. self.__qualname__ = getattr(func, '__qualname__', None)
  164. self._sigspec = None
  165. self._has_unknown_args = None
  166. @instanceproperty
  167. def func(self):
  168. return self._partial.func
  169. if PY3: # pragma: py2 no cover
  170. @instanceproperty
  171. def __signature__(self):
  172. sig = inspect.signature(self.func)
  173. args = self.args or ()
  174. keywords = self.keywords or {}
  175. if is_partial_args(self.func, args, keywords, sig) is False:
  176. raise TypeError('curry object has incorrect arguments')
  177. params = list(sig.parameters.values())
  178. skip = 0
  179. for param in params[:len(args)]:
  180. if param.kind == param.VAR_POSITIONAL:
  181. break
  182. skip += 1
  183. kwonly = False
  184. newparams = []
  185. for param in params[skip:]:
  186. kind = param.kind
  187. default = param.default
  188. if kind == param.VAR_KEYWORD:
  189. pass
  190. elif kind == param.VAR_POSITIONAL:
  191. if kwonly:
  192. continue
  193. elif param.name in keywords:
  194. default = keywords[param.name]
  195. kind = param.KEYWORD_ONLY
  196. kwonly = True
  197. else:
  198. if kwonly:
  199. kind = param.KEYWORD_ONLY
  200. if default is param.empty:
  201. default = no_default
  202. newparams.append(param.replace(default=default, kind=kind))
  203. return sig.replace(parameters=newparams)
  204. @instanceproperty
  205. def args(self):
  206. return self._partial.args
  207. @instanceproperty
  208. def keywords(self):
  209. return self._partial.keywords
  210. @instanceproperty
  211. def func_name(self):
  212. return self.__name__
  213. def __str__(self):
  214. return str(self.func)
  215. def __repr__(self):
  216. return repr(self.func)
  217. def __hash__(self):
  218. return hash((self.func, self.args,
  219. frozenset(self.keywords.items()) if self.keywords
  220. else None))
  221. def __eq__(self, other):
  222. return (isinstance(other, curry) and self.func == other.func and
  223. self.args == other.args and self.keywords == other.keywords)
  224. def __ne__(self, other):
  225. return not self.__eq__(other)
  226. def __call__(self, *args, **kwargs):
  227. try:
  228. return self._partial(*args, **kwargs)
  229. except TypeError as exc:
  230. if self._should_curry(args, kwargs, exc):
  231. return self.bind(*args, **kwargs)
  232. raise
  233. def _should_curry(self, args, kwargs, exc=None):
  234. func = self.func
  235. args = self.args + args
  236. if self.keywords:
  237. kwargs = dict(self.keywords, **kwargs)
  238. if self._sigspec is None:
  239. sigspec = self._sigspec = _sigs.signature_or_spec(func)
  240. self._has_unknown_args = has_varargs(func, sigspec) is not False
  241. else:
  242. sigspec = self._sigspec
  243. if is_partial_args(func, args, kwargs, sigspec) is False:
  244. # Nothing can make the call valid
  245. return False
  246. elif self._has_unknown_args:
  247. # The call may be valid and raised a TypeError, but we curry
  248. # anyway because the function may have `*args`. This is useful
  249. # for decorators with signature `func(*args, **kwargs)`.
  250. return True
  251. elif not is_valid_args(func, args, kwargs, sigspec):
  252. # Adding more arguments may make the call valid
  253. return True
  254. else:
  255. # There was a genuine TypeError
  256. return False
  257. def bind(self, *args, **kwargs):
  258. return type(self)(self, *args, **kwargs)
  259. def call(self, *args, **kwargs):
  260. return self._partial(*args, **kwargs)
  261. def __get__(self, instance, owner):
  262. if instance is None:
  263. return self
  264. return curry(self, instance)
  265. def __reduce__(self):
  266. func = self.func
  267. modname = getattr(func, '__module__', None)
  268. qualname = getattr(func, '__qualname__', None)
  269. if qualname is None: # pragma: py3 no cover
  270. qualname = getattr(func, '__name__', None)
  271. is_decorated = None
  272. if modname and qualname:
  273. attrs = []
  274. obj = import_module(modname)
  275. for attr in qualname.split('.'):
  276. if isinstance(obj, curry): # pragma: py2 no cover
  277. attrs.append('func')
  278. obj = obj.func
  279. obj = getattr(obj, attr, None)
  280. if obj is None:
  281. break
  282. attrs.append(attr)
  283. if isinstance(obj, curry) and obj.func is func:
  284. is_decorated = obj is self
  285. qualname = '.'.join(attrs)
  286. func = '%s:%s' % (modname, qualname)
  287. # functools.partial objects can't be pickled
  288. userdict = tuple((k, v) for k, v in self.__dict__.items()
  289. if k not in ('_partial', '_sigspec'))
  290. state = (type(self), func, self.args, self.keywords, userdict,
  291. is_decorated)
  292. return (_restore_curry, state)
  293. def _restore_curry(cls, func, args, kwargs, userdict, is_decorated):
  294. if isinstance(func, str):
  295. modname, qualname = func.rsplit(':', 1)
  296. obj = import_module(modname)
  297. for attr in qualname.split('.'):
  298. obj = getattr(obj, attr)
  299. if is_decorated:
  300. return obj
  301. func = obj.func
  302. obj = cls(func, *args, **(kwargs or {}))
  303. obj.__dict__.update(userdict)
  304. return obj
  305. @curry
  306. def memoize(func, cache=None, key=None):
  307. """ Cache a function's result for speedy future evaluation
  308. Considerations:
  309. Trades memory for speed.
  310. Only use on pure functions.
  311. >>> def add(x, y): return x + y
  312. >>> add = memoize(add)
  313. Or use as a decorator
  314. >>> @memoize
  315. ... def add(x, y):
  316. ... return x + y
  317. Use the ``cache`` keyword to provide a dict-like object as an initial cache
  318. >>> @memoize(cache={(1, 2): 3})
  319. ... def add(x, y):
  320. ... return x + y
  321. Note that the above works as a decorator because ``memoize`` is curried.
  322. It is also possible to provide a ``key(args, kwargs)`` function that
  323. calculates keys used for the cache, which receives an ``args`` tuple and
  324. ``kwargs`` dict as input, and must return a hashable value. However,
  325. the default key function should be sufficient most of the time.
  326. >>> # Use key function that ignores extraneous keyword arguments
  327. >>> @memoize(key=lambda args, kwargs: args)
  328. ... def add(x, y, verbose=False):
  329. ... if verbose:
  330. ... print('Calculating %s + %s' % (x, y))
  331. ... return x + y
  332. """
  333. if cache is None:
  334. cache = {}
  335. try:
  336. may_have_kwargs = has_keywords(func) is not False
  337. # Is unary function (single arg, no variadic argument or keywords)?
  338. is_unary = is_arity(1, func)
  339. except TypeError: # pragma: no cover
  340. may_have_kwargs = True
  341. is_unary = False
  342. if key is None:
  343. if is_unary:
  344. def key(args, kwargs):
  345. return args[0]
  346. elif may_have_kwargs:
  347. def key(args, kwargs):
  348. return (
  349. args or None,
  350. frozenset(kwargs.items()) if kwargs else None,
  351. )
  352. else:
  353. def key(args, kwargs):
  354. return args
  355. def memof(*args, **kwargs):
  356. k = key(args, kwargs)
  357. try:
  358. return cache[k]
  359. except TypeError:
  360. raise TypeError("Arguments to memoized function must be hashable")
  361. except KeyError:
  362. cache[k] = result = func(*args, **kwargs)
  363. return result
  364. try:
  365. memof.__name__ = func.__name__
  366. except AttributeError:
  367. pass
  368. memof.__doc__ = func.__doc__
  369. memof.__wrapped__ = func
  370. return memof
  371. class Compose(object):
  372. """ A composition of functions
  373. See Also:
  374. compose
  375. """
  376. __slots__ = 'first', 'funcs'
  377. def __init__(self, funcs):
  378. funcs = tuple(reversed(funcs))
  379. self.first = funcs[0]
  380. self.funcs = funcs[1:]
  381. def __call__(self, *args, **kwargs):
  382. ret = self.first(*args, **kwargs)
  383. for f in self.funcs:
  384. ret = f(ret)
  385. return ret
  386. def __getstate__(self):
  387. return self.first, self.funcs
  388. def __setstate__(self, state):
  389. self.first, self.funcs = state
  390. @instanceproperty(classval=__doc__)
  391. def __doc__(self):
  392. def composed_doc(*fs):
  393. """Generate a docstring for the composition of fs.
  394. """
  395. if not fs:
  396. # Argument name for the docstring.
  397. return '*args, **kwargs'
  398. return '{f}({g})'.format(f=fs[0].__name__, g=composed_doc(*fs[1:]))
  399. try:
  400. return (
  401. 'lambda *args, **kwargs: ' +
  402. composed_doc(*reversed((self.first,) + self.funcs))
  403. )
  404. except AttributeError:
  405. # One of our callables does not have a `__name__`, whatever.
  406. return 'A composition of functions'
  407. @property
  408. def __name__(self):
  409. try:
  410. return '_of_'.join(
  411. f.__name__ for f in reversed((self.first,) + self.funcs)
  412. )
  413. except AttributeError:
  414. return type(self).__name__
  415. def compose(*funcs):
  416. """ Compose functions to operate in series.
  417. Returns a function that applies other functions in sequence.
  418. Functions are applied from right to left so that
  419. ``compose(f, g, h)(x, y)`` is the same as ``f(g(h(x, y)))``.
  420. If no arguments are provided, the identity function (f(x) = x) is returned.
  421. >>> inc = lambda i: i + 1
  422. >>> compose(str, inc)(3)
  423. '4'
  424. See Also:
  425. pipe
  426. """
  427. if not funcs:
  428. return identity
  429. if len(funcs) == 1:
  430. return funcs[0]
  431. else:
  432. return Compose(funcs)
  433. def pipe(data, *funcs):
  434. """ Pipe a value through a sequence of functions
  435. I.e. ``pipe(data, f, g, h)`` is equivalent to ``h(g(f(data)))``
  436. We think of the value as progressing through a pipe of several
  437. transformations, much like pipes in UNIX
  438. ``$ cat data | f | g | h``
  439. >>> double = lambda i: 2 * i
  440. >>> pipe(3, double, str)
  441. '6'
  442. See Also:
  443. compose
  444. thread_first
  445. thread_last
  446. """
  447. for func in funcs:
  448. data = func(data)
  449. return data
  450. def complement(func):
  451. """ Convert a predicate function to its logical complement.
  452. In other words, return a function that, for inputs that normally
  453. yield True, yields False, and vice-versa.
  454. >>> def iseven(n): return n % 2 == 0
  455. >>> isodd = complement(iseven)
  456. >>> iseven(2)
  457. True
  458. >>> isodd(2)
  459. False
  460. """
  461. return compose(operator.not_, func)
  462. class juxt(object):
  463. """ Creates a function that calls several functions with the same arguments
  464. Takes several functions and returns a function that applies its arguments
  465. to each of those functions then returns a tuple of the results.
  466. Name comes from juxtaposition: the fact of two things being seen or placed
  467. close together with contrasting effect.
  468. >>> inc = lambda x: x + 1
  469. >>> double = lambda x: x * 2
  470. >>> juxt(inc, double)(10)
  471. (11, 20)
  472. >>> juxt([inc, double])(10)
  473. (11, 20)
  474. """
  475. __slots__ = ['funcs']
  476. def __init__(self, *funcs):
  477. if len(funcs) == 1 and not callable(funcs[0]):
  478. funcs = funcs[0]
  479. self.funcs = tuple(funcs)
  480. def __call__(self, *args, **kwargs):
  481. return tuple(func(*args, **kwargs) for func in self.funcs)
  482. def __getstate__(self):
  483. return self.funcs
  484. def __setstate__(self, state):
  485. self.funcs = state
  486. def do(func, x):
  487. """ Runs ``func`` on ``x``, returns ``x``
  488. Because the results of ``func`` are not returned, only the side
  489. effects of ``func`` are relevant.
  490. Logging functions can be made by composing ``do`` with a storage function
  491. like ``list.append`` or ``file.write``
  492. >>> from toolz import compose
  493. >>> from toolz.curried import do
  494. >>> log = []
  495. >>> inc = lambda x: x + 1
  496. >>> inc = compose(inc, do(log.append))
  497. >>> inc(1)
  498. 2
  499. >>> inc(11)
  500. 12
  501. >>> log
  502. [1, 11]
  503. """
  504. func(x)
  505. return x
  506. @curry
  507. def flip(func, a, b):
  508. """ Call the function call with the arguments flipped
  509. This function is curried.
  510. >>> def div(a, b):
  511. ... return a // b
  512. ...
  513. >>> flip(div, 2, 6)
  514. 3
  515. >>> div_by_two = flip(div, 2)
  516. >>> div_by_two(4)
  517. 2
  518. This is particularly useful for built in functions and functions defined
  519. in C extensions that accept positional only arguments. For example:
  520. isinstance, issubclass.
  521. >>> data = [1, 'a', 'b', 2, 1.5, object(), 3]
  522. >>> only_ints = list(filter(flip(isinstance, int), data))
  523. >>> only_ints
  524. [1, 2, 3]
  525. """
  526. return func(b, a)
  527. def return_none(exc):
  528. """ Returns None.
  529. """
  530. return None
  531. class excepts(object):
  532. """A wrapper around a function to catch exceptions and
  533. dispatch to a handler.
  534. This is like a functional try/except block, in the same way that
  535. ifexprs are functional if/else blocks.
  536. Examples
  537. --------
  538. >>> excepting = excepts(
  539. ... ValueError,
  540. ... lambda a: [1, 2].index(a),
  541. ... lambda _: -1,
  542. ... )
  543. >>> excepting(1)
  544. 0
  545. >>> excepting(3)
  546. -1
  547. Multiple exceptions and default except clause.
  548. >>> excepting = excepts((IndexError, KeyError), lambda a: a[0])
  549. >>> excepting([])
  550. >>> excepting([1])
  551. 1
  552. >>> excepting({})
  553. >>> excepting({0: 1})
  554. 1
  555. """
  556. def __init__(self, exc, func, handler=return_none):
  557. self.exc = exc
  558. self.func = func
  559. self.handler = handler
  560. def __call__(self, *args, **kwargs):
  561. try:
  562. return self.func(*args, **kwargs)
  563. except self.exc as e:
  564. return self.handler(e)
  565. @instanceproperty(classval=__doc__)
  566. def __doc__(self):
  567. exc = self.exc
  568. try:
  569. if isinstance(exc, tuple):
  570. exc_name = '(%s)' % ', '.join(
  571. map(attrgetter('__name__'), exc),
  572. )
  573. else:
  574. exc_name = exc.__name__
  575. return dedent(
  576. """\
  577. A wrapper around {inst.func.__name__!r} that will except:
  578. {exc}
  579. and handle any exceptions with {inst.handler.__name__!r}.
  580. Docs for {inst.func.__name__!r}:
  581. {inst.func.__doc__}
  582. Docs for {inst.handler.__name__!r}:
  583. {inst.handler.__doc__}
  584. """
  585. ).format(
  586. inst=self,
  587. exc=exc_name,
  588. )
  589. except AttributeError:
  590. return type(self).__doc__
  591. @property
  592. def __name__(self):
  593. exc = self.exc
  594. try:
  595. if isinstance(exc, tuple):
  596. exc_name = '_or_'.join(map(attrgetter('__name__'), exc))
  597. else:
  598. exc_name = exc.__name__
  599. return '%s_excepting_%s' % (self.func.__name__, exc_name)
  600. except AttributeError:
  601. return 'excepting'
  602. if PY3: # pragma: py2 no cover
  603. def _check_sigspec(sigspec, func, builtin_func, *builtin_args):
  604. if sigspec is None:
  605. try:
  606. sigspec = inspect.signature(func)
  607. except (ValueError, TypeError) as e:
  608. sigspec = e
  609. if isinstance(sigspec, ValueError):
  610. return None, builtin_func(*builtin_args)
  611. elif not isinstance(sigspec, inspect.Signature):
  612. if (
  613. func in _sigs.signatures
  614. and ((
  615. hasattr(func, '__signature__')
  616. and hasattr(func.__signature__, '__get__')
  617. ) or (
  618. PY33
  619. and hasattr(func, '__wrapped__')
  620. and hasattr(func.__wrapped__, '__get__')
  621. and not callable(func.__wrapped__)
  622. ))
  623. ): # pragma: no cover (not covered in Python 3.4)
  624. val = builtin_func(*builtin_args)
  625. return None, val
  626. return None, False
  627. return sigspec, None
  628. else: # pragma: py3 no cover
  629. def _check_sigspec(sigspec, func, builtin_func, *builtin_args):
  630. if sigspec is None:
  631. try:
  632. sigspec = inspect.getargspec(func)
  633. except TypeError as e:
  634. sigspec = e
  635. if isinstance(sigspec, TypeError):
  636. if not callable(func):
  637. return None, False
  638. return None, builtin_func(*builtin_args)
  639. return sigspec, None
  640. if PY34 or PYPY: # pragma: no cover
  641. _check_sigspec_orig = _check_sigspec
  642. def _check_sigspec(sigspec, func, builtin_func, *builtin_args):
  643. # Python 3.4 and PyPy may lie, so use our registry for builtins instead
  644. if func in _sigs.signatures:
  645. val = builtin_func(*builtin_args)
  646. return None, val
  647. return _check_sigspec_orig(sigspec, func, builtin_func, *builtin_args)
  648. _check_sigspec.__doc__ = """ \
  649. Private function to aid in introspection compatibly across Python versions.
  650. If a callable doesn't have a signature (Python 3) or an argspec (Python 2),
  651. the signature registry in toolz._signatures is used.
  652. """
  653. if PY3: # pragma: py2 no cover
  654. def num_required_args(func, sigspec=None):
  655. sigspec, rv = _check_sigspec(sigspec, func, _sigs._num_required_args,
  656. func)
  657. if sigspec is None:
  658. return rv
  659. return sum(1 for p in sigspec.parameters.values()
  660. if p.default is p.empty
  661. and p.kind in (p.POSITIONAL_OR_KEYWORD, p.POSITIONAL_ONLY))
  662. def has_varargs(func, sigspec=None):
  663. sigspec, rv = _check_sigspec(sigspec, func, _sigs._has_varargs, func)
  664. if sigspec is None:
  665. return rv
  666. return any(p.kind == p.VAR_POSITIONAL
  667. for p in sigspec.parameters.values())
  668. def has_keywords(func, sigspec=None):
  669. sigspec, rv = _check_sigspec(sigspec, func, _sigs._has_keywords, func)
  670. if sigspec is None:
  671. return rv
  672. return any(p.default is not p.empty
  673. or p.kind in (p.KEYWORD_ONLY, p.VAR_KEYWORD)
  674. for p in sigspec.parameters.values())
  675. def is_valid_args(func, args, kwargs, sigspec=None):
  676. sigspec, rv = _check_sigspec(sigspec, func, _sigs._is_valid_args,
  677. func, args, kwargs)
  678. if sigspec is None:
  679. return rv
  680. try:
  681. sigspec.bind(*args, **kwargs)
  682. except TypeError:
  683. return False
  684. return True
  685. def is_partial_args(func, args, kwargs, sigspec=None):
  686. sigspec, rv = _check_sigspec(sigspec, func, _sigs._is_partial_args,
  687. func, args, kwargs)
  688. if sigspec is None:
  689. return rv
  690. try:
  691. sigspec.bind_partial(*args, **kwargs)
  692. except TypeError:
  693. return False
  694. return True
  695. else: # pragma: py3 no cover
  696. def num_required_args(func, sigspec=None):
  697. sigspec, rv = _check_sigspec(sigspec, func, _sigs._num_required_args,
  698. func)
  699. if sigspec is None:
  700. return rv
  701. num_defaults = len(sigspec.defaults) if sigspec.defaults else 0
  702. return len(sigspec.args) - num_defaults
  703. def has_varargs(func, sigspec=None):
  704. sigspec, rv = _check_sigspec(sigspec, func, _sigs._has_varargs, func)
  705. if sigspec is None:
  706. return rv
  707. return sigspec.varargs is not None
  708. def has_keywords(func, sigspec=None):
  709. sigspec, rv = _check_sigspec(sigspec, func, _sigs._has_keywords, func)
  710. if sigspec is None:
  711. return rv
  712. return sigspec.defaults is not None or sigspec.keywords is not None
  713. def is_valid_args(func, args, kwargs, sigspec=None):
  714. sigspec, rv = _check_sigspec(sigspec, func, _sigs._is_valid_args,
  715. func, args, kwargs)
  716. if sigspec is None:
  717. return rv
  718. spec = sigspec
  719. defaults = spec.defaults or ()
  720. num_pos = len(spec.args) - len(defaults)
  721. missing_pos = spec.args[len(args):num_pos]
  722. if any(arg not in kwargs for arg in missing_pos):
  723. return False
  724. if spec.varargs is None:
  725. num_extra_pos = max(0, len(args) - num_pos)
  726. else:
  727. num_extra_pos = 0
  728. kwargs = dict(kwargs)
  729. # Add missing keyword arguments (unless already included in `args`)
  730. missing_kwargs = spec.args[num_pos + num_extra_pos:]
  731. kwargs.update(zip(missing_kwargs, defaults[num_extra_pos:]))
  732. # Convert call to use positional arguments
  733. args = args + tuple(kwargs.pop(key) for key in spec.args[len(args):])
  734. if (
  735. not spec.keywords and kwargs
  736. or not spec.varargs and len(args) > len(spec.args)
  737. or set(spec.args[:len(args)]) & set(kwargs)
  738. ):
  739. return False
  740. else:
  741. return True
  742. def is_partial_args(func, args, kwargs, sigspec=None):
  743. sigspec, rv = _check_sigspec(sigspec, func, _sigs._is_partial_args,
  744. func, args, kwargs)
  745. if sigspec is None:
  746. return rv
  747. spec = sigspec
  748. defaults = spec.defaults or ()
  749. num_pos = len(spec.args) - len(defaults)
  750. if spec.varargs is None:
  751. num_extra_pos = max(0, len(args) - num_pos)
  752. else:
  753. num_extra_pos = 0
  754. kwargs = dict(kwargs)
  755. # Add missing keyword arguments (unless already included in `args`)
  756. missing_kwargs = spec.args[num_pos + num_extra_pos:]
  757. kwargs.update(zip(missing_kwargs, defaults[num_extra_pos:]))
  758. # Add missing position arguments as keywords (may already be in kwargs)
  759. missing_args = spec.args[len(args):num_pos + num_extra_pos]
  760. kwargs.update((x, None) for x in missing_args)
  761. # Convert call to use positional arguments
  762. args = args + tuple(kwargs.pop(key) for key in spec.args[len(args):])
  763. if (
  764. not spec.keywords and kwargs
  765. or not spec.varargs and len(args) > len(spec.args)
  766. or set(spec.args[:len(args)]) & set(kwargs)
  767. ):
  768. return False
  769. else:
  770. return True
  771. def is_arity(n, func, sigspec=None):
  772. """ Does a function have only n positional arguments?
  773. This function relies on introspection and does not call the function.
  774. Returns None if validity can't be determined.
  775. >>> def f(x):
  776. ... return x
  777. >>> is_arity(1, f)
  778. True
  779. >>> def g(x, y=1):
  780. ... return x + y
  781. >>> is_arity(1, g)
  782. False
  783. """
  784. sigspec, rv = _check_sigspec(sigspec, func, _sigs._is_arity, n, func)
  785. if sigspec is None:
  786. return rv
  787. num = num_required_args(func, sigspec)
  788. if num is not None:
  789. num = num == n
  790. if not num:
  791. return False
  792. varargs = has_varargs(func, sigspec)
  793. if varargs:
  794. return False
  795. keywords = has_keywords(func, sigspec)
  796. if keywords:
  797. return False
  798. if num is None or varargs is None or keywords is None: # pragma: no cover
  799. return None
  800. return True
  801. num_required_args.__doc__ = """ \
  802. Number of required positional arguments
  803. This function relies on introspection and does not call the function.
  804. Returns None if validity can't be determined.
  805. >>> def f(x, y, z=3):
  806. ... return x + y + z
  807. >>> num_required_args(f)
  808. 2
  809. >>> def g(*args, **kwargs):
  810. ... pass
  811. >>> num_required_args(g)
  812. 0
  813. """
  814. has_varargs.__doc__ = """ \
  815. Does a function have variadic positional arguments?
  816. This function relies on introspection and does not call the function.
  817. Returns None if validity can't be determined.
  818. >>> def f(*args):
  819. ... return args
  820. >>> has_varargs(f)
  821. True
  822. >>> def g(**kwargs):
  823. ... return kwargs
  824. >>> has_varargs(g)
  825. False
  826. """
  827. has_keywords.__doc__ = """ \
  828. Does a function have keyword arguments?
  829. This function relies on introspection and does not call the function.
  830. Returns None if validity can't be determined.
  831. >>> def f(x, y=0):
  832. ... return x + y
  833. >>> has_keywords(f)
  834. True
  835. """
  836. is_valid_args.__doc__ = """ \
  837. Is ``func(*args, **kwargs)`` a valid function call?
  838. This function relies on introspection and does not call the function.
  839. Returns None if validity can't be determined.
  840. >>> def add(x, y):
  841. ... return x + y
  842. >>> is_valid_args(add, (1,), {})
  843. False
  844. >>> is_valid_args(add, (1, 2), {})
  845. True
  846. >>> is_valid_args(map, (), {})
  847. False
  848. **Implementation notes**
  849. Python 2 relies on ``inspect.getargspec``, which only works for
  850. user-defined functions. Python 3 uses ``inspect.signature``, which
  851. works for many more types of callables.
  852. Many builtins in the standard library are also supported.
  853. """
  854. is_partial_args.__doc__ = """ \
  855. Can partial(func, *args, **kwargs)(*args2, **kwargs2) be a valid call?
  856. Returns True *only* if the call is valid or if it is possible for the
  857. call to become valid by adding more positional or keyword arguments.
  858. This function relies on introspection and does not call the function.
  859. Returns None if validity can't be determined.
  860. >>> def add(x, y):
  861. ... return x + y
  862. >>> is_partial_args(add, (1,), {})
  863. True
  864. >>> is_partial_args(add, (1, 2), {})
  865. True
  866. >>> is_partial_args(add, (1, 2, 3), {})
  867. False
  868. >>> is_partial_args(map, (), {})
  869. True
  870. **Implementation notes**
  871. Python 2 relies on ``inspect.getargspec``, which only works for
  872. user-defined functions. Python 3 uses ``inspect.signature``, which
  873. works for many more types of callables.
  874. Many builtins in the standard library are also supported.
  875. """
  876. from . import _signatures as _sigs