_signatures.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. """Internal module for better introspection of builtins.
  2. The main functions are ``is_builtin_valid_args``, ``is_builtin_partial_args``,
  3. and ``has_unknown_args``. Other functions in this module support these three.
  4. Notably, we create a ``signatures`` registry to enable introspection of
  5. builtin functions in any Python version. This includes builtins that
  6. have more than one valid signature. Currently, the registry includes
  7. builtins from ``builtins``, ``functools``, ``itertools``, and ``operator``
  8. modules. More can be added as requested. We don't guarantee full coverage.
  9. Everything in this module should be regarded as implementation details.
  10. Users should try to not use this module directly.
  11. """
  12. import functools
  13. import inspect
  14. import itertools
  15. import operator
  16. from .compatibility import PY3, import_module
  17. from .functoolz import (is_partial_args, is_arity, has_varargs,
  18. has_keywords, num_required_args)
  19. if PY3: # pragma: py2 no cover
  20. import builtins
  21. else: # pragma: py3 no cover
  22. import __builtin__ as builtins
  23. # We mock builtin callables using lists of tuples with lambda functions.
  24. #
  25. # The tuple spec is (num_position_args, lambda_func, keyword_only_args).
  26. #
  27. # num_position_args:
  28. # - The number of positional-only arguments. If not specified,
  29. # all positional arguments are considered positional-only.
  30. #
  31. # lambda_func:
  32. # - lambda function that matches a signature of a builtin, but does
  33. # not include keyword-only arguments.
  34. #
  35. # keyword_only_args: (optional)
  36. # - Tuple of keyword-only argumemts.
  37. module_info = {}
  38. module_info[builtins] = dict(
  39. abs=[
  40. lambda x: None],
  41. all=[
  42. lambda iterable: None],
  43. any=[
  44. lambda iterable: None],
  45. apply=[
  46. lambda object: None,
  47. lambda object, args: None,
  48. lambda object, args, kwargs: None],
  49. ascii=[
  50. lambda obj: None],
  51. bin=[
  52. lambda number: None],
  53. bool=[
  54. lambda x=False: None],
  55. buffer=[
  56. lambda object: None,
  57. lambda object, offset: None,
  58. lambda object, offset, size: None],
  59. bytearray=[
  60. lambda: None,
  61. lambda int: None,
  62. lambda string, encoding='utf8', errors='strict': None],
  63. callable=[
  64. lambda obj: None],
  65. chr=[
  66. lambda i: None],
  67. classmethod=[
  68. lambda function: None],
  69. cmp=[
  70. lambda x, y: None],
  71. coerce=[
  72. lambda x, y: None],
  73. complex=[
  74. lambda real=0, imag=0: None],
  75. delattr=[
  76. lambda obj, name: None],
  77. dict=[
  78. lambda **kwargs: None,
  79. lambda mapping, **kwargs: None],
  80. dir=[
  81. lambda: None,
  82. lambda object: None],
  83. divmod=[
  84. lambda x, y: None],
  85. enumerate=[
  86. (0, lambda iterable, start=0: None)],
  87. eval=[
  88. lambda source: None,
  89. lambda source, globals: None,
  90. lambda source, globals, locals: None],
  91. execfile=[
  92. lambda filename: None,
  93. lambda filename, globals: None,
  94. lambda filename, globals, locals: None],
  95. file=[
  96. (0, lambda name, mode='r', buffering=-1: None)],
  97. filter=[
  98. lambda function, iterable: None],
  99. float=[
  100. lambda x=0.0: None],
  101. format=[
  102. lambda value: None,
  103. lambda value, format_spec: None],
  104. frozenset=[
  105. lambda: None,
  106. lambda iterable: None],
  107. getattr=[
  108. lambda object, name: None,
  109. lambda object, name, default: None],
  110. globals=[
  111. lambda: None],
  112. hasattr=[
  113. lambda obj, name: None],
  114. hash=[
  115. lambda obj: None],
  116. hex=[
  117. lambda number: None],
  118. id=[
  119. lambda obj: None],
  120. input=[
  121. lambda: None,
  122. lambda prompt: None],
  123. int=[
  124. lambda x=0: None,
  125. (0, lambda x, base=10: None)],
  126. intern=[
  127. lambda string: None],
  128. isinstance=[
  129. lambda obj, class_or_tuple: None],
  130. issubclass=[
  131. lambda cls, class_or_tuple: None],
  132. iter=[
  133. lambda iterable: None,
  134. lambda callable, sentinel: None],
  135. len=[
  136. lambda obj: None],
  137. list=[
  138. lambda: None,
  139. lambda iterable: None],
  140. locals=[
  141. lambda: None],
  142. long=[
  143. lambda x=0: None,
  144. (0, lambda x, base=10: None)],
  145. map=[
  146. lambda func, sequence, *iterables: None],
  147. memoryview=[
  148. (0, lambda object: None)],
  149. next=[
  150. lambda iterator: None,
  151. lambda iterator, default: None],
  152. object=[
  153. lambda: None],
  154. oct=[
  155. lambda number: None],
  156. ord=[
  157. lambda c: None],
  158. pow=[
  159. lambda x, y: None,
  160. lambda x, y, z: None],
  161. property=[
  162. lambda fget=None, fset=None, fdel=None, doc=None: None],
  163. range=[
  164. lambda stop: None,
  165. lambda start, stop: None,
  166. lambda start, stop, step: None],
  167. raw_input=[
  168. lambda: None,
  169. lambda prompt: None],
  170. reduce=[
  171. lambda function, sequence: None,
  172. lambda function, sequence, initial: None],
  173. reload=[
  174. lambda module: None],
  175. repr=[
  176. lambda obj: None],
  177. reversed=[
  178. lambda sequence: None],
  179. round=[
  180. (0, lambda number, ndigits=0: None)],
  181. set=[
  182. lambda: None,
  183. lambda iterable: None],
  184. setattr=[
  185. lambda obj, name, value: None],
  186. slice=[
  187. lambda stop: None,
  188. lambda start, stop: None,
  189. lambda start, stop, step: None],
  190. staticmethod=[
  191. lambda function: None],
  192. sum=[
  193. lambda iterable: None,
  194. lambda iterable, start: None],
  195. super=[
  196. lambda type: None,
  197. lambda type, obj: None],
  198. tuple=[
  199. lambda: None,
  200. lambda iterable: None],
  201. type=[
  202. lambda object: None,
  203. lambda name, bases, dict: None],
  204. unichr=[
  205. lambda i: None],
  206. unicode=[
  207. lambda object: None,
  208. lambda string='', encoding='utf8', errors='strict': None],
  209. vars=[
  210. lambda: None,
  211. lambda object: None],
  212. xrange=[
  213. lambda stop: None,
  214. lambda start, stop: None,
  215. lambda start, stop, step: None],
  216. zip=[
  217. lambda *iterables: None],
  218. __build_class__=[
  219. (2, lambda func, name, *bases, **kwds: None, ('metaclass',))],
  220. __import__=[
  221. (0, lambda name, globals=None, locals=None, fromlist=None,
  222. level=None: None)],
  223. )
  224. module_info[builtins]['exec'] = [
  225. lambda source: None,
  226. lambda source, globals: None,
  227. lambda source, globals, locals: None]
  228. if PY3: # pragma: py2 no cover
  229. module_info[builtins].update(
  230. bytes=[
  231. lambda: None,
  232. lambda int: None,
  233. lambda string, encoding='utf8', errors='strict': None],
  234. compile=[
  235. (0, lambda source, filename, mode, flags=0,
  236. dont_inherit=False, optimize=-1: None)],
  237. max=[
  238. (1, lambda iterable: None, ('default', 'key',)),
  239. (1, lambda arg1, arg2, *args: None, ('key',))],
  240. min=[
  241. (1, lambda iterable: None, ('default', 'key',)),
  242. (1, lambda arg1, arg2, *args: None, ('key',))],
  243. open=[
  244. (0, lambda file, mode='r', buffering=-1, encoding=None,
  245. errors=None, newline=None, closefd=True, opener=None: None)],
  246. sorted=[
  247. (1, lambda iterable: None, ('key', 'reverse'))],
  248. str=[
  249. lambda object='', encoding='utf', errors='strict': None],
  250. )
  251. module_info[builtins]['print'] = [
  252. (0, lambda *args: None, ('sep', 'end', 'file', 'flush',))]
  253. else: # pragma: py3 no cover
  254. module_info[builtins].update(
  255. bytes=[
  256. lambda object='': None],
  257. compile=[
  258. (0, lambda source, filename, mode, flags=0,
  259. dont_inherit=False: None)],
  260. max=[
  261. (1, lambda iterable, *args: None, ('key',))],
  262. min=[
  263. (1, lambda iterable, *args: None, ('key',))],
  264. open=[
  265. (0, lambda file, mode='r', buffering=-1: None)],
  266. sorted=[
  267. lambda iterable, cmp=None, key=None, reverse=False: None],
  268. str=[
  269. lambda object='': None],
  270. )
  271. module_info[builtins]['print'] = [
  272. (0, lambda *args: None, ('sep', 'end', 'file',))]
  273. module_info[functools] = dict(
  274. cmp_to_key=[
  275. (0, lambda mycmp: None)],
  276. partial=[
  277. lambda func, *args, **kwargs: None],
  278. partialmethod=[
  279. lambda func, *args, **kwargs: None],
  280. reduce=[
  281. lambda function, sequence: None,
  282. lambda function, sequence, initial: None],
  283. )
  284. module_info[itertools] = dict(
  285. accumulate=[
  286. (0, lambda iterable, func=None: None)],
  287. chain=[
  288. lambda *iterables: None],
  289. combinations=[
  290. (0, lambda iterable, r: None)],
  291. combinations_with_replacement=[
  292. (0, lambda iterable, r: None)],
  293. compress=[
  294. (0, lambda data, selectors: None)],
  295. count=[
  296. lambda start=0, step=1: None],
  297. cycle=[
  298. lambda iterable: None],
  299. dropwhile=[
  300. lambda predicate, iterable: None],
  301. filterfalse=[
  302. lambda function, sequence: None],
  303. groupby=[
  304. (0, lambda iterable, key=None: None)],
  305. ifilter=[
  306. lambda function, sequence: None],
  307. ifilterfalse=[
  308. lambda function, sequence: None],
  309. imap=[
  310. lambda func, sequence, *iterables: None],
  311. islice=[
  312. lambda iterable, stop: None,
  313. lambda iterable, start, stop: None,
  314. lambda iterable, start, stop, step: None],
  315. izip=[
  316. lambda *iterables: None],
  317. izip_longest=[
  318. (0, lambda *iterables: None, ('fillvalue',))],
  319. permutations=[
  320. (0, lambda iterable, r=0: None)],
  321. repeat=[
  322. (0, lambda object, times=0: None)],
  323. starmap=[
  324. lambda function, sequence: None],
  325. takewhile=[
  326. lambda predicate, iterable: None],
  327. tee=[
  328. lambda iterable: None,
  329. lambda iterable, n: None],
  330. zip_longest=[
  331. (0, lambda *iterables: None, ('fillvalue',))],
  332. )
  333. if PY3: # pragma: py2 no cover
  334. module_info[itertools].update(
  335. product=[
  336. (0, lambda *iterables: None, ('repeat',))],
  337. )
  338. else: # pragma: py3 no cover
  339. module_info[itertools].update(
  340. product=[
  341. lambda *iterables: None],
  342. )
  343. module_info[operator] = dict(
  344. __abs__=[
  345. lambda a: None],
  346. __add__=[
  347. lambda a, b: None],
  348. __and__=[
  349. lambda a, b: None],
  350. __concat__=[
  351. lambda a, b: None],
  352. __contains__=[
  353. lambda a, b: None],
  354. __delitem__=[
  355. lambda a, b: None],
  356. __delslice__=[
  357. lambda a, b, c: None],
  358. __div__=[
  359. lambda a, b: None],
  360. __eq__=[
  361. lambda a, b: None],
  362. __floordiv__=[
  363. lambda a, b: None],
  364. __ge__=[
  365. lambda a, b: None],
  366. __getitem__=[
  367. lambda a, b: None],
  368. __getslice__=[
  369. lambda a, b, c: None],
  370. __gt__=[
  371. lambda a, b: None],
  372. __iadd__=[
  373. lambda a, b: None],
  374. __iand__=[
  375. lambda a, b: None],
  376. __iconcat__=[
  377. lambda a, b: None],
  378. __idiv__=[
  379. lambda a, b: None],
  380. __ifloordiv__=[
  381. lambda a, b: None],
  382. __ilshift__=[
  383. lambda a, b: None],
  384. __imatmul__=[
  385. lambda a, b: None],
  386. __imod__=[
  387. lambda a, b: None],
  388. __imul__=[
  389. lambda a, b: None],
  390. __index__=[
  391. lambda a: None],
  392. __inv__=[
  393. lambda a: None],
  394. __invert__=[
  395. lambda a: None],
  396. __ior__=[
  397. lambda a, b: None],
  398. __ipow__=[
  399. lambda a, b: None],
  400. __irepeat__=[
  401. lambda a, b: None],
  402. __irshift__=[
  403. lambda a, b: None],
  404. __isub__=[
  405. lambda a, b: None],
  406. __itruediv__=[
  407. lambda a, b: None],
  408. __ixor__=[
  409. lambda a, b: None],
  410. __le__=[
  411. lambda a, b: None],
  412. __lshift__=[
  413. lambda a, b: None],
  414. __lt__=[
  415. lambda a, b: None],
  416. __matmul__=[
  417. lambda a, b: None],
  418. __mod__=[
  419. lambda a, b: None],
  420. __mul__=[
  421. lambda a, b: None],
  422. __ne__=[
  423. lambda a, b: None],
  424. __neg__=[
  425. lambda a: None],
  426. __not__=[
  427. lambda a: None],
  428. __or__=[
  429. lambda a, b: None],
  430. __pos__=[
  431. lambda a: None],
  432. __pow__=[
  433. lambda a, b: None],
  434. __repeat__=[
  435. lambda a, b: None],
  436. __rshift__=[
  437. lambda a, b: None],
  438. __setitem__=[
  439. lambda a, b, c: None],
  440. __setslice__=[
  441. lambda a, b, c, d: None],
  442. __sub__=[
  443. lambda a, b: None],
  444. __truediv__=[
  445. lambda a, b: None],
  446. __xor__=[
  447. lambda a, b: None],
  448. _abs=[
  449. lambda x: None],
  450. _compare_digest=[
  451. lambda a, b: None],
  452. abs=[
  453. lambda a: None],
  454. add=[
  455. lambda a, b: None],
  456. and_=[
  457. lambda a, b: None],
  458. attrgetter=[
  459. lambda attr, *args: None],
  460. concat=[
  461. lambda a, b: None],
  462. contains=[
  463. lambda a, b: None],
  464. countOf=[
  465. lambda a, b: None],
  466. delitem=[
  467. lambda a, b: None],
  468. delslice=[
  469. lambda a, b, c: None],
  470. div=[
  471. lambda a, b: None],
  472. eq=[
  473. lambda a, b: None],
  474. floordiv=[
  475. lambda a, b: None],
  476. ge=[
  477. lambda a, b: None],
  478. getitem=[
  479. lambda a, b: None],
  480. getslice=[
  481. lambda a, b, c: None],
  482. gt=[
  483. lambda a, b: None],
  484. iadd=[
  485. lambda a, b: None],
  486. iand=[
  487. lambda a, b: None],
  488. iconcat=[
  489. lambda a, b: None],
  490. idiv=[
  491. lambda a, b: None],
  492. ifloordiv=[
  493. lambda a, b: None],
  494. ilshift=[
  495. lambda a, b: None],
  496. imatmul=[
  497. lambda a, b: None],
  498. imod=[
  499. lambda a, b: None],
  500. imul=[
  501. lambda a, b: None],
  502. index=[
  503. lambda a: None],
  504. indexOf=[
  505. lambda a, b: None],
  506. inv=[
  507. lambda a: None],
  508. invert=[
  509. lambda a: None],
  510. ior=[
  511. lambda a, b: None],
  512. ipow=[
  513. lambda a, b: None],
  514. irepeat=[
  515. lambda a, b: None],
  516. irshift=[
  517. lambda a, b: None],
  518. is_=[
  519. lambda a, b: None],
  520. is_not=[
  521. lambda a, b: None],
  522. isCallable=[
  523. lambda a: None],
  524. isMappingType=[
  525. lambda a: None],
  526. isNumberType=[
  527. lambda a: None],
  528. isSequenceType=[
  529. lambda a: None],
  530. isub=[
  531. lambda a, b: None],
  532. itemgetter=[
  533. lambda item, *args: None],
  534. itruediv=[
  535. lambda a, b: None],
  536. ixor=[
  537. lambda a, b: None],
  538. le=[
  539. lambda a, b: None],
  540. length_hint=[
  541. lambda obj: None,
  542. lambda obj, default: None],
  543. lshift=[
  544. lambda a, b: None],
  545. lt=[
  546. lambda a, b: None],
  547. matmul=[
  548. lambda a, b: None],
  549. methodcaller=[
  550. lambda name, *args, **kwargs: None],
  551. mod=[
  552. lambda a, b: None],
  553. mul=[
  554. lambda a, b: None],
  555. ne=[
  556. lambda a, b: None],
  557. neg=[
  558. lambda a: None],
  559. not_=[
  560. lambda a: None],
  561. or_=[
  562. lambda a, b: None],
  563. pos=[
  564. lambda a: None],
  565. pow=[
  566. lambda a, b: None],
  567. repeat=[
  568. lambda a, b: None],
  569. rshift=[
  570. lambda a, b: None],
  571. sequenceIncludes=[
  572. lambda a, b: None],
  573. setitem=[
  574. lambda a, b, c: None],
  575. setslice=[
  576. lambda a, b, c, d: None],
  577. sub=[
  578. lambda a, b: None],
  579. truediv=[
  580. lambda a, b: None],
  581. truth=[
  582. lambda a: None],
  583. xor=[
  584. lambda a, b: None],
  585. )
  586. module_info['toolz'] = dict(
  587. curry=[
  588. (0, lambda *args, **kwargs: None)],
  589. excepts=[
  590. (0, lambda exc, func, handler=None: None)],
  591. flip=[
  592. (0, lambda func=None, a=None, b=None: None)],
  593. juxt=[
  594. (0, lambda *funcs: None)],
  595. memoize=[
  596. (0, lambda func=None, cache=None, key=None: None)],
  597. )
  598. module_info['toolz.functoolz'] = dict(
  599. Compose=[
  600. (0, lambda funcs: None)],
  601. InstanceProperty=[
  602. (0, lambda fget=None, fset=None, fdel=None, doc=None,
  603. classval=None: None)],
  604. )
  605. if PY3: # pragma: py2 no cover
  606. def num_pos_args(sigspec):
  607. """ Return the number of positional arguments. ``f(x, y=1)`` has 1"""
  608. return sum(1 for x in sigspec.parameters.values()
  609. if x.kind == x.POSITIONAL_OR_KEYWORD
  610. and x.default is x.empty)
  611. def get_exclude_keywords(num_pos_only, sigspec):
  612. """ Return the names of position-only arguments if func has **kwargs"""
  613. if num_pos_only == 0:
  614. return ()
  615. has_kwargs = any(x.kind == x.VAR_KEYWORD
  616. for x in sigspec.parameters.values())
  617. if not has_kwargs:
  618. return ()
  619. pos_args = list(sigspec.parameters.values())[:num_pos_only]
  620. return tuple(x.name for x in pos_args)
  621. def signature_or_spec(func):
  622. try:
  623. return inspect.signature(func)
  624. except (ValueError, TypeError) as e:
  625. return e
  626. else: # pragma: py3 no cover
  627. def num_pos_args(sigspec):
  628. """ Return the number of positional arguments. ``f(x, y=1)`` has 1"""
  629. if sigspec.defaults:
  630. return len(sigspec.args) - len(sigspec.defaults)
  631. return len(sigspec.args)
  632. def get_exclude_keywords(num_pos_only, sigspec):
  633. """ Return the names of position-only arguments if func has **kwargs"""
  634. if num_pos_only == 0:
  635. return ()
  636. has_kwargs = sigspec.keywords is not None
  637. if not has_kwargs:
  638. return ()
  639. return tuple(sigspec.args[:num_pos_only])
  640. def signature_or_spec(func):
  641. try:
  642. return inspect.getargspec(func)
  643. except TypeError as e:
  644. return e
  645. def expand_sig(sig):
  646. """ Convert the signature spec in ``module_info`` to add to ``signatures``
  647. The input signature spec is one of:
  648. - ``lambda_func``
  649. - ``(num_position_args, lambda_func)``
  650. - ``(num_position_args, lambda_func, keyword_only_args)``
  651. The output signature spec is:
  652. ``(num_position_args, lambda_func, keyword_exclude, sigspec)``
  653. where ``keyword_exclude`` includes keyword only arguments and, if variadic
  654. keywords is present, the names of position-only argument. The latter is
  655. included to support builtins such as ``partial(func, *args, **kwargs)``,
  656. which allows ``func=`` to be used as a keyword even though it's the name
  657. of a positional argument.
  658. """
  659. if isinstance(sig, tuple):
  660. if len(sig) == 3:
  661. num_pos_only, func, keyword_only = sig
  662. assert isinstance(sig[-1], tuple)
  663. else:
  664. num_pos_only, func = sig
  665. keyword_only = ()
  666. sigspec = signature_or_spec(func)
  667. else:
  668. func = sig
  669. sigspec = signature_or_spec(func)
  670. num_pos_only = num_pos_args(sigspec)
  671. keyword_only = ()
  672. keyword_exclude = get_exclude_keywords(num_pos_only, sigspec)
  673. return (num_pos_only, func, keyword_only + keyword_exclude, sigspec)
  674. signatures = {}
  675. def create_signature_registry(module_info=module_info, signatures=signatures):
  676. for module, info in module_info.items():
  677. if isinstance(module, str):
  678. module = import_module(module)
  679. for name, sigs in info.items():
  680. if hasattr(module, name):
  681. new_sigs = tuple(expand_sig(sig) for sig in sigs)
  682. signatures[getattr(module, name)] = new_sigs
  683. def check_valid(sig, args, kwargs):
  684. """ Like ``is_valid_args`` for the given signature spec"""
  685. num_pos_only, func, keyword_exclude, sigspec = sig
  686. if len(args) < num_pos_only:
  687. return False
  688. if keyword_exclude:
  689. kwargs = dict(kwargs)
  690. for item in keyword_exclude:
  691. kwargs.pop(item, None)
  692. try:
  693. func(*args, **kwargs)
  694. return True
  695. except TypeError:
  696. return False
  697. def _is_valid_args(func, args, kwargs):
  698. """ Like ``is_valid_args`` for builtins in our ``signatures`` registry"""
  699. if func not in signatures:
  700. return None
  701. sigs = signatures[func]
  702. return any(check_valid(sig, args, kwargs) for sig in sigs)
  703. def check_partial(sig, args, kwargs):
  704. """ Like ``is_partial_args`` for the given signature spec"""
  705. num_pos_only, func, keyword_exclude, sigspec = sig
  706. if len(args) < num_pos_only:
  707. pad = (None,) * (num_pos_only - len(args))
  708. args = args + pad
  709. if keyword_exclude:
  710. kwargs = dict(kwargs)
  711. for item in keyword_exclude:
  712. kwargs.pop(item, None)
  713. return is_partial_args(func, args, kwargs, sigspec)
  714. def _is_partial_args(func, args, kwargs):
  715. """ Like ``is_partial_args`` for builtins in our ``signatures`` registry"""
  716. if func not in signatures:
  717. return None
  718. sigs = signatures[func]
  719. return any(check_partial(sig, args, kwargs) for sig in sigs)
  720. def check_arity(n, sig):
  721. num_pos_only, func, keyword_exclude, sigspec = sig
  722. if keyword_exclude or num_pos_only > n:
  723. return False
  724. return is_arity(n, func, sigspec)
  725. def _is_arity(n, func):
  726. if func not in signatures:
  727. return None
  728. sigs = signatures[func]
  729. checks = [check_arity(n, sig) for sig in sigs]
  730. if all(checks):
  731. return True
  732. elif any(checks):
  733. return None
  734. return False
  735. def check_varargs(sig):
  736. num_pos_only, func, keyword_exclude, sigspec = sig
  737. return has_varargs(func, sigspec)
  738. def _has_varargs(func):
  739. if func not in signatures:
  740. return None
  741. sigs = signatures[func]
  742. checks = [check_varargs(sig) for sig in sigs]
  743. if all(checks):
  744. return True
  745. elif any(checks): # pragma: py2 no cover
  746. return None
  747. return False
  748. def check_keywords(sig):
  749. num_pos_only, func, keyword_exclude, sigspec = sig
  750. if keyword_exclude:
  751. return True
  752. return has_keywords(func, sigspec)
  753. def _has_keywords(func):
  754. if func not in signatures:
  755. return None
  756. sigs = signatures[func]
  757. checks = [check_keywords(sig) for sig in sigs]
  758. if all(checks):
  759. return True
  760. elif any(checks):
  761. return None
  762. return False
  763. def check_required_args(sig):
  764. num_pos_only, func, keyword_exclude, sigspec = sig
  765. return num_required_args(func, sigspec)
  766. def _num_required_args(func):
  767. if func not in signatures:
  768. return None
  769. sigs = signatures[func]
  770. vals = [check_required_args(sig) for sig in sigs]
  771. val = vals[0]
  772. if all(x == val for x in vals):
  773. return val
  774. return None