_signatures.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  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 importlib import import_module
  17. from .functoolz import (is_partial_args, is_arity, has_varargs,
  18. has_keywords, num_required_args)
  19. import builtins
  20. # We mock builtin callables using lists of tuples with lambda functions.
  21. #
  22. # The tuple spec is (num_position_args, lambda_func, keyword_only_args).
  23. #
  24. # num_position_args:
  25. # - The number of positional-only arguments. If not specified,
  26. # all positional arguments are considered positional-only.
  27. #
  28. # lambda_func:
  29. # - lambda function that matches a signature of a builtin, but does
  30. # not include keyword-only arguments.
  31. #
  32. # keyword_only_args: (optional)
  33. # - Tuple of keyword-only arguments.
  34. module_info = {}
  35. module_info[builtins] = dict(
  36. abs=[
  37. lambda x: None],
  38. all=[
  39. lambda iterable: None],
  40. anext=[
  41. lambda aiterator: None,
  42. lambda aiterator, default: 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. module_info[builtins].update(
  229. breakpoint=[
  230. lambda *args, **kws: None],
  231. bytes=[
  232. lambda: None,
  233. lambda int: None,
  234. lambda string, encoding='utf8', errors='strict': None],
  235. compile=[
  236. (0, lambda source, filename, mode, flags=0,
  237. dont_inherit=False, optimize=-1: None)],
  238. max=[
  239. (1, lambda iterable: None, ('default', 'key',)),
  240. (1, lambda arg1, arg2, *args: None, ('key',))],
  241. min=[
  242. (1, lambda iterable: None, ('default', 'key',)),
  243. (1, lambda arg1, arg2, *args: None, ('key',))],
  244. open=[
  245. (0, lambda file, mode='r', buffering=-1, encoding=None,
  246. errors=None, newline=None, closefd=True, opener=None: None)],
  247. sorted=[
  248. (1, lambda iterable: None, ('key', 'reverse'))],
  249. str=[
  250. lambda object='', encoding='utf', errors='strict': None],
  251. )
  252. module_info[builtins]['print'] = [
  253. (0, lambda *args: None, ('sep', 'end', 'file', 'flush',))]
  254. module_info[functools] = dict(
  255. cmp_to_key=[
  256. (0, lambda mycmp: None)],
  257. partial=[
  258. lambda func, *args, **kwargs: None],
  259. partialmethod=[
  260. lambda func, *args, **kwargs: None],
  261. reduce=[
  262. lambda function, sequence: None,
  263. lambda function, sequence, initial: None],
  264. )
  265. module_info[itertools] = dict(
  266. accumulate=[
  267. (0, lambda iterable, func=None: None)],
  268. chain=[
  269. lambda *iterables: None],
  270. combinations=[
  271. (0, lambda iterable, r: None)],
  272. combinations_with_replacement=[
  273. (0, lambda iterable, r: None)],
  274. compress=[
  275. (0, lambda data, selectors: None)],
  276. count=[
  277. lambda start=0, step=1: None],
  278. cycle=[
  279. lambda iterable: None],
  280. dropwhile=[
  281. lambda predicate, iterable: None],
  282. filterfalse=[
  283. lambda function, sequence: None],
  284. groupby=[
  285. (0, lambda iterable, key=None: None)],
  286. ifilter=[
  287. lambda function, sequence: None],
  288. ifilterfalse=[
  289. lambda function, sequence: None],
  290. imap=[
  291. lambda func, sequence, *iterables: None],
  292. islice=[
  293. lambda iterable, stop: None,
  294. lambda iterable, start, stop: None,
  295. lambda iterable, start, stop, step: None],
  296. izip=[
  297. lambda *iterables: None],
  298. izip_longest=[
  299. (0, lambda *iterables: None, ('fillvalue',))],
  300. pairwise=[
  301. lambda iterable: None],
  302. permutations=[
  303. (0, lambda iterable, r=0: None)],
  304. product=[
  305. (0, lambda *iterables: None, ('repeat',))],
  306. repeat=[
  307. (0, lambda object, times=0: None)],
  308. starmap=[
  309. lambda function, sequence: None],
  310. takewhile=[
  311. lambda predicate, iterable: None],
  312. tee=[
  313. lambda iterable: None,
  314. lambda iterable, n: None],
  315. zip_longest=[
  316. (0, lambda *iterables: None, ('fillvalue',))],
  317. )
  318. module_info[operator] = dict(
  319. __abs__=[
  320. lambda a: None],
  321. __add__=[
  322. lambda a, b: None],
  323. __and__=[
  324. lambda a, b: None],
  325. __concat__=[
  326. lambda a, b: None],
  327. __contains__=[
  328. lambda a, b: None],
  329. __delitem__=[
  330. lambda a, b: None],
  331. __delslice__=[
  332. lambda a, b, c: None],
  333. __div__=[
  334. lambda a, b: None],
  335. __eq__=[
  336. lambda a, b: None],
  337. __floordiv__=[
  338. lambda a, b: None],
  339. __ge__=[
  340. lambda a, b: None],
  341. __getitem__=[
  342. lambda a, b: None],
  343. __getslice__=[
  344. lambda a, b, c: None],
  345. __gt__=[
  346. lambda a, b: None],
  347. __iadd__=[
  348. lambda a, b: None],
  349. __iand__=[
  350. lambda a, b: None],
  351. __iconcat__=[
  352. lambda a, b: None],
  353. __idiv__=[
  354. lambda a, b: None],
  355. __ifloordiv__=[
  356. lambda a, b: None],
  357. __ilshift__=[
  358. lambda a, b: None],
  359. __imatmul__=[
  360. lambda a, b: None],
  361. __imod__=[
  362. lambda a, b: None],
  363. __imul__=[
  364. lambda a, b: None],
  365. __index__=[
  366. lambda a: None],
  367. __inv__=[
  368. lambda a: None],
  369. __invert__=[
  370. lambda a: None],
  371. __ior__=[
  372. lambda a, b: None],
  373. __ipow__=[
  374. lambda a, b: None],
  375. __irepeat__=[
  376. lambda a, b: None],
  377. __irshift__=[
  378. lambda a, b: None],
  379. __isub__=[
  380. lambda a, b: None],
  381. __itruediv__=[
  382. lambda a, b: None],
  383. __ixor__=[
  384. lambda a, b: None],
  385. __le__=[
  386. lambda a, b: None],
  387. __lshift__=[
  388. lambda a, b: None],
  389. __lt__=[
  390. lambda a, b: None],
  391. __matmul__=[
  392. lambda a, b: None],
  393. __mod__=[
  394. lambda a, b: None],
  395. __mul__=[
  396. lambda a, b: None],
  397. __ne__=[
  398. lambda a, b: None],
  399. __neg__=[
  400. lambda a: None],
  401. __not__=[
  402. lambda a: None],
  403. __or__=[
  404. lambda a, b: None],
  405. __pos__=[
  406. lambda a: None],
  407. __pow__=[
  408. lambda a, b: None],
  409. __repeat__=[
  410. lambda a, b: None],
  411. __rshift__=[
  412. lambda a, b: None],
  413. __setitem__=[
  414. lambda a, b, c: None],
  415. __setslice__=[
  416. lambda a, b, c, d: None],
  417. __sub__=[
  418. lambda a, b: None],
  419. __truediv__=[
  420. lambda a, b: None],
  421. __xor__=[
  422. lambda a, b: None],
  423. _abs=[
  424. lambda x: None],
  425. _compare_digest=[
  426. lambda a, b: None],
  427. abs=[
  428. lambda a: None],
  429. add=[
  430. lambda a, b: None],
  431. and_=[
  432. lambda a, b: None],
  433. attrgetter=[
  434. lambda attr, *args: None],
  435. concat=[
  436. lambda a, b: None],
  437. contains=[
  438. lambda a, b: None],
  439. countOf=[
  440. lambda a, b: None],
  441. delitem=[
  442. lambda a, b: None],
  443. delslice=[
  444. lambda a, b, c: None],
  445. div=[
  446. lambda a, b: None],
  447. eq=[
  448. lambda a, b: None],
  449. floordiv=[
  450. lambda a, b: None],
  451. ge=[
  452. lambda a, b: None],
  453. getitem=[
  454. lambda a, b: None],
  455. getslice=[
  456. lambda a, b, c: None],
  457. gt=[
  458. lambda a, b: None],
  459. iadd=[
  460. lambda a, b: None],
  461. iand=[
  462. lambda a, b: None],
  463. iconcat=[
  464. lambda a, b: None],
  465. idiv=[
  466. lambda a, b: None],
  467. ifloordiv=[
  468. lambda a, b: None],
  469. ilshift=[
  470. lambda a, b: None],
  471. imatmul=[
  472. lambda a, b: None],
  473. imod=[
  474. lambda a, b: None],
  475. imul=[
  476. lambda a, b: None],
  477. index=[
  478. lambda a: None],
  479. indexOf=[
  480. lambda a, b: None],
  481. inv=[
  482. lambda a: None],
  483. invert=[
  484. lambda a: None],
  485. ior=[
  486. lambda a, b: None],
  487. ipow=[
  488. lambda a, b: None],
  489. irepeat=[
  490. lambda a, b: None],
  491. irshift=[
  492. lambda a, b: None],
  493. is_=[
  494. lambda a, b: None],
  495. is_not=[
  496. lambda a, b: None],
  497. isCallable=[
  498. lambda a: None],
  499. isMappingType=[
  500. lambda a: None],
  501. isNumberType=[
  502. lambda a: None],
  503. isSequenceType=[
  504. lambda a: None],
  505. isub=[
  506. lambda a, b: None],
  507. itemgetter=[
  508. lambda item, *args: None],
  509. itruediv=[
  510. lambda a, b: None],
  511. ixor=[
  512. lambda a, b: None],
  513. le=[
  514. lambda a, b: None],
  515. length_hint=[
  516. lambda obj: None,
  517. lambda obj, default: None],
  518. lshift=[
  519. lambda a, b: None],
  520. lt=[
  521. lambda a, b: None],
  522. matmul=[
  523. lambda a, b: None],
  524. methodcaller=[
  525. lambda name, *args, **kwargs: None],
  526. mod=[
  527. lambda a, b: None],
  528. mul=[
  529. lambda a, b: None],
  530. ne=[
  531. lambda a, b: None],
  532. neg=[
  533. lambda a: None],
  534. not_=[
  535. lambda a: None],
  536. or_=[
  537. lambda a, b: None],
  538. pos=[
  539. lambda a: None],
  540. pow=[
  541. lambda a, b: None],
  542. repeat=[
  543. lambda a, b: None],
  544. rshift=[
  545. lambda a, b: None],
  546. sequenceIncludes=[
  547. lambda a, b: None],
  548. setitem=[
  549. lambda a, b, c: None],
  550. setslice=[
  551. lambda a, b, c, d: None],
  552. sub=[
  553. lambda a, b: None],
  554. truediv=[
  555. lambda a, b: None],
  556. truth=[
  557. lambda a: None],
  558. xor=[
  559. lambda a, b: None],
  560. )
  561. module_info['toolz'] = dict(
  562. curry=[
  563. (0, lambda *args, **kwargs: None)],
  564. excepts=[
  565. (0, lambda exc, func, handler=None: None)],
  566. flip=[
  567. (0, lambda func=None, a=None, b=None: None)],
  568. juxt=[
  569. (0, lambda *funcs: None)],
  570. memoize=[
  571. (0, lambda func=None, cache=None, key=None: None)],
  572. )
  573. module_info['toolz.functoolz'] = dict(
  574. Compose=[
  575. (0, lambda funcs: None)],
  576. InstanceProperty=[
  577. (0, lambda fget=None, fset=None, fdel=None, doc=None,
  578. classval=None: None)],
  579. )
  580. def num_pos_args(sigspec):
  581. """ Return the number of positional arguments. ``f(x, y=1)`` has 1"""
  582. return sum(1 for x in sigspec.parameters.values()
  583. if x.kind == x.POSITIONAL_OR_KEYWORD
  584. and x.default is x.empty)
  585. def get_exclude_keywords(num_pos_only, sigspec):
  586. """ Return the names of position-only arguments if func has **kwargs"""
  587. if num_pos_only == 0:
  588. return ()
  589. has_kwargs = any(x.kind == x.VAR_KEYWORD
  590. for x in sigspec.parameters.values())
  591. if not has_kwargs:
  592. return ()
  593. pos_args = list(sigspec.parameters.values())[:num_pos_only]
  594. return tuple(x.name for x in pos_args)
  595. def signature_or_spec(func):
  596. try:
  597. return inspect.signature(func)
  598. except (ValueError, TypeError):
  599. return None
  600. def expand_sig(sig):
  601. """ Convert the signature spec in ``module_info`` to add to ``signatures``
  602. The input signature spec is one of:
  603. - ``lambda_func``
  604. - ``(num_position_args, lambda_func)``
  605. - ``(num_position_args, lambda_func, keyword_only_args)``
  606. The output signature spec is:
  607. ``(num_position_args, lambda_func, keyword_exclude, sigspec)``
  608. where ``keyword_exclude`` includes keyword only arguments and, if variadic
  609. keywords is present, the names of position-only argument. The latter is
  610. included to support builtins such as ``partial(func, *args, **kwargs)``,
  611. which allows ``func=`` to be used as a keyword even though it's the name
  612. of a positional argument.
  613. """
  614. if isinstance(sig, tuple):
  615. if len(sig) == 3:
  616. num_pos_only, func, keyword_only = sig
  617. assert isinstance(sig[-1], tuple)
  618. else:
  619. num_pos_only, func = sig
  620. keyword_only = ()
  621. sigspec = signature_or_spec(func)
  622. else:
  623. func = sig
  624. sigspec = signature_or_spec(func)
  625. num_pos_only = num_pos_args(sigspec)
  626. keyword_only = ()
  627. keyword_exclude = get_exclude_keywords(num_pos_only, sigspec)
  628. return num_pos_only, func, keyword_only + keyword_exclude, sigspec
  629. signatures = {}
  630. def create_signature_registry(module_info=module_info, signatures=signatures):
  631. for module, info in module_info.items():
  632. if isinstance(module, str):
  633. module = import_module(module)
  634. for name, sigs in info.items():
  635. if hasattr(module, name):
  636. new_sigs = tuple(expand_sig(sig) for sig in sigs)
  637. signatures[getattr(module, name)] = new_sigs
  638. def check_valid(sig, args, kwargs):
  639. """ Like ``is_valid_args`` for the given signature spec"""
  640. num_pos_only, func, keyword_exclude, sigspec = sig
  641. if len(args) < num_pos_only:
  642. return False
  643. if keyword_exclude:
  644. kwargs = dict(kwargs)
  645. for item in keyword_exclude:
  646. kwargs.pop(item, None)
  647. try:
  648. func(*args, **kwargs)
  649. return True
  650. except TypeError:
  651. return False
  652. def _is_valid_args(func, args, kwargs):
  653. """ Like ``is_valid_args`` for builtins in our ``signatures`` registry"""
  654. if func not in signatures:
  655. return None
  656. sigs = signatures[func]
  657. return any(check_valid(sig, args, kwargs) for sig in sigs)
  658. def check_partial(sig, args, kwargs):
  659. """ Like ``is_partial_args`` for the given signature spec"""
  660. num_pos_only, func, keyword_exclude, sigspec = sig
  661. if len(args) < num_pos_only:
  662. pad = (None,) * (num_pos_only - len(args))
  663. args = args + pad
  664. if keyword_exclude:
  665. kwargs = dict(kwargs)
  666. for item in keyword_exclude:
  667. kwargs.pop(item, None)
  668. return is_partial_args(func, args, kwargs, sigspec)
  669. def _is_partial_args(func, args, kwargs):
  670. """ Like ``is_partial_args`` for builtins in our ``signatures`` registry"""
  671. if func not in signatures:
  672. return None
  673. sigs = signatures[func]
  674. return any(check_partial(sig, args, kwargs) for sig in sigs)
  675. def check_arity(n, sig):
  676. num_pos_only, func, keyword_exclude, sigspec = sig
  677. if keyword_exclude or num_pos_only > n:
  678. return False
  679. return is_arity(n, func, sigspec)
  680. def _is_arity(n, func):
  681. if func not in signatures:
  682. return None
  683. sigs = signatures[func]
  684. checks = [check_arity(n, sig) for sig in sigs]
  685. if all(checks):
  686. return True
  687. elif any(checks):
  688. return None
  689. return False
  690. def check_varargs(sig):
  691. num_pos_only, func, keyword_exclude, sigspec = sig
  692. return has_varargs(func, sigspec)
  693. def _has_varargs(func):
  694. if func not in signatures:
  695. return None
  696. sigs = signatures[func]
  697. checks = [check_varargs(sig) for sig in sigs]
  698. if all(checks):
  699. return True
  700. elif any(checks):
  701. return None
  702. return False
  703. def check_keywords(sig):
  704. num_pos_only, func, keyword_exclude, sigspec = sig
  705. if keyword_exclude:
  706. return True
  707. return has_keywords(func, sigspec)
  708. def _has_keywords(func):
  709. if func not in signatures:
  710. return None
  711. sigs = signatures[func]
  712. checks = [check_keywords(sig) for sig in sigs]
  713. if all(checks):
  714. return True
  715. elif any(checks):
  716. return None
  717. return False
  718. def check_required_args(sig):
  719. num_pos_only, func, keyword_exclude, sigspec = sig
  720. return num_required_args(func, sigspec)
  721. def _num_required_args(func):
  722. if func not in signatures:
  723. return None
  724. sigs = signatures[func]
  725. vals = [check_required_args(sig) for sig in sigs]
  726. val = vals[0]
  727. if all(x == val for x in vals):
  728. return val
  729. return None