METADATA 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. Metadata-Version: 2.0
  2. Name: funcsigs
  3. Version: 1.0.2
  4. Summary: Python function signatures from PEP362 for Python 2.6, 2.7 and 3.2+
  5. Home-page: http://funcsigs.readthedocs.org
  6. Author: Testing Cabal
  7. Author-email: testing-in-python@lists.idyll.org
  8. License: ASL
  9. Platform: UNKNOWN
  10. Classifier: Development Status :: 4 - Beta
  11. Classifier: Intended Audience :: Developers
  12. Classifier: License :: OSI Approved :: Apache Software License
  13. Classifier: Operating System :: OS Independent
  14. Classifier: Programming Language :: Python
  15. Classifier: Programming Language :: Python :: 2
  16. Classifier: Programming Language :: Python :: 2.6
  17. Classifier: Programming Language :: Python :: 2.7
  18. Classifier: Programming Language :: Python :: 3
  19. Classifier: Programming Language :: Python :: 3.3
  20. Classifier: Programming Language :: Python :: 3.4
  21. Classifier: Programming Language :: Python :: 3.5
  22. Classifier: Programming Language :: Python :: Implementation :: CPython
  23. Classifier: Programming Language :: Python :: Implementation :: PyPy
  24. Classifier: Topic :: Software Development :: Libraries :: Python Modules
  25. Requires-Dist: ordereddict; python_version<"2.7"
  26. .. funcsigs documentation master file, created by
  27. sphinx-quickstart on Fri Apr 20 20:27:52 2012.
  28. You can adapt this file completely to your liking, but it should at least
  29. contain the root `toctree` directive.
  30. Introducing funcsigs
  31. ====================
  32. The Funcsigs Package
  33. --------------------
  34. ``funcsigs`` is a backport of the `PEP 362`_ function signature features from
  35. Python 3.3's `inspect`_ module. The backport is compatible with Python 2.6, 2.7
  36. as well as 3.3 and up. 3.2 was supported by version 0.4, but with setuptools and
  37. pip no longer supporting 3.2, we cannot make any statement about 3.2
  38. compatibility.
  39. Compatibility
  40. `````````````
  41. The ``funcsigs`` backport has been tested against:
  42. * CPython 2.6
  43. * CPython 2.7
  44. * CPython 3.3
  45. * CPython 3.4
  46. * CPython 3.5
  47. * CPython nightlies
  48. * PyPy and PyPy3(currently failing CI)
  49. Continuous integration testing is provided by `Travis CI`_.
  50. Under Python 2.x there is a compatibility issue when a function is assigned to
  51. the ``__wrapped__`` property of a class after it has been constructed.
  52. Similiarily there under PyPy directly passing the ``__call__`` method of a
  53. builtin is also a compatibility issues. Otherwise the functionality is
  54. believed to be uniform between both Python2 and Python3.
  55. Issues
  56. ``````
  57. Source code for ``funcsigs`` is hosted on `GitHub`_. Any bug reports or feature
  58. requests can be made using GitHub's `issues system`_. |build_status| |coverage|
  59. Example
  60. -------
  61. To obtain a `Signature` object, pass the target function to the
  62. ``funcsigs.signature`` function.
  63. .. code-block:: python
  64. >>> from funcsigs import signature
  65. >>> def foo(a, b=None, *args, **kwargs):
  66. ... pass
  67. ...
  68. >>> sig = signature(foo)
  69. >>> sig
  70. <funcsigs.Signature object at 0x...>
  71. >>> sig.parameters
  72. OrderedDict([('a', <Parameter at 0x... 'a'>), ('b', <Parameter at 0x... 'b'>), ('args', <Parameter at 0x... 'args'>), ('kwargs', <Parameter at 0x... 'kwargs'>)])
  73. >>> sig.return_annotation
  74. <class 'funcsigs._empty'>
  75. Introspecting callables with the Signature object
  76. -------------------------------------------------
  77. .. note::
  78. This section of documentation is a direct reproduction of the Python
  79. standard library documentation for the inspect module.
  80. The Signature object represents the call signature of a callable object and its
  81. return annotation. To retrieve a Signature object, use the :func:`signature`
  82. function.
  83. .. function:: signature(callable)
  84. Return a :class:`Signature` object for the given ``callable``::
  85. >>> from funcsigs import signature
  86. >>> def foo(a, *, b:int, **kwargs):
  87. ... pass
  88. >>> sig = signature(foo)
  89. >>> str(sig)
  90. '(a, *, b:int, **kwargs)'
  91. >>> str(sig.parameters['b'])
  92. 'b:int'
  93. >>> sig.parameters['b'].annotation
  94. <class 'int'>
  95. Accepts a wide range of python callables, from plain functions and classes to
  96. :func:`functools.partial` objects.
  97. .. note::
  98. Some callables may not be introspectable in certain implementations of
  99. Python. For example, in CPython, built-in functions defined in C provide
  100. no metadata about their arguments.
  101. .. class:: Signature
  102. A Signature object represents the call signature of a function and its return
  103. annotation. For each parameter accepted by the function it stores a
  104. :class:`Parameter` object in its :attr:`parameters` collection.
  105. Signature objects are *immutable*. Use :meth:`Signature.replace` to make a
  106. modified copy.
  107. .. attribute:: Signature.empty
  108. A special class-level marker to specify absence of a return annotation.
  109. .. attribute:: Signature.parameters
  110. An ordered mapping of parameters' names to the corresponding
  111. :class:`Parameter` objects.
  112. .. attribute:: Signature.return_annotation
  113. The "return" annotation for the callable. If the callable has no "return"
  114. annotation, this attribute is set to :attr:`Signature.empty`.
  115. .. method:: Signature.bind(*args, **kwargs)
  116. Create a mapping from positional and keyword arguments to parameters.
  117. Returns :class:`BoundArguments` if ``*args`` and ``**kwargs`` match the
  118. signature, or raises a :exc:`TypeError`.
  119. .. method:: Signature.bind_partial(*args, **kwargs)
  120. Works the same way as :meth:`Signature.bind`, but allows the omission of
  121. some required arguments (mimics :func:`functools.partial` behavior.)
  122. Returns :class:`BoundArguments`, or raises a :exc:`TypeError` if the
  123. passed arguments do not match the signature.
  124. .. method:: Signature.replace(*[, parameters][, return_annotation])
  125. Create a new Signature instance based on the instance replace was invoked
  126. on. It is possible to pass different ``parameters`` and/or
  127. ``return_annotation`` to override the corresponding properties of the base
  128. signature. To remove return_annotation from the copied Signature, pass in
  129. :attr:`Signature.empty`.
  130. ::
  131. >>> def test(a, b):
  132. ... pass
  133. >>> sig = signature(test)
  134. >>> new_sig = sig.replace(return_annotation="new return anno")
  135. >>> str(new_sig)
  136. "(a, b) -> 'new return anno'"
  137. .. class:: Parameter
  138. Parameter objects are *immutable*. Instead of modifying a Parameter object,
  139. you can use :meth:`Parameter.replace` to create a modified copy.
  140. .. attribute:: Parameter.empty
  141. A special class-level marker to specify absence of default values and
  142. annotations.
  143. .. attribute:: Parameter.name
  144. The name of the parameter as a string. Must be a valid python identifier
  145. name (with the exception of ``POSITIONAL_ONLY`` parameters, which can have
  146. it set to ``None``).
  147. .. attribute:: Parameter.default
  148. The default value for the parameter. If the parameter has no default
  149. value, this attribute is set to :attr:`Parameter.empty`.
  150. .. attribute:: Parameter.annotation
  151. The annotation for the parameter. If the parameter has no annotation,
  152. this attribute is set to :attr:`Parameter.empty`.
  153. .. attribute:: Parameter.kind
  154. Describes how argument values are bound to the parameter. Possible values
  155. (accessible via :class:`Parameter`, like ``Parameter.KEYWORD_ONLY``):
  156. +------------------------+----------------------------------------------+
  157. | Name | Meaning |
  158. +========================+==============================================+
  159. | *POSITIONAL_ONLY* | Value must be supplied as a positional |
  160. | | argument. |
  161. | | |
  162. | | Python has no explicit syntax for defining |
  163. | | positional-only parameters, but many built-in|
  164. | | and extension module functions (especially |
  165. | | those that accept only one or two parameters)|
  166. | | accept them. |
  167. +------------------------+----------------------------------------------+
  168. | *POSITIONAL_OR_KEYWORD*| Value may be supplied as either a keyword or |
  169. | | positional argument (this is the standard |
  170. | | binding behaviour for functions implemented |
  171. | | in Python.) |
  172. +------------------------+----------------------------------------------+
  173. | *VAR_POSITIONAL* | A tuple of positional arguments that aren't |
  174. | | bound to any other parameter. This |
  175. | | corresponds to a ``*args`` parameter in a |
  176. | | Python function definition. |
  177. +------------------------+----------------------------------------------+
  178. | *KEYWORD_ONLY* | Value must be supplied as a keyword argument.|
  179. | | Keyword only parameters are those which |
  180. | | appear after a ``*`` or ``*args`` entry in a |
  181. | | Python function definition. |
  182. +------------------------+----------------------------------------------+
  183. | *VAR_KEYWORD* | A dict of keyword arguments that aren't bound|
  184. | | to any other parameter. This corresponds to a|
  185. | | ``**kwargs`` parameter in a Python function |
  186. | | definition. |
  187. +------------------------+----------------------------------------------+
  188. Example: print all keyword-only arguments without default values::
  189. >>> def foo(a, b, *, c, d=10):
  190. ... pass
  191. >>> sig = signature(foo)
  192. >>> for param in sig.parameters.values():
  193. ... if (param.kind == param.KEYWORD_ONLY and
  194. ... param.default is param.empty):
  195. ... print('Parameter:', param)
  196. Parameter: c
  197. .. method:: Parameter.replace(*[, name][, kind][, default][, annotation])
  198. Create a new Parameter instance based on the instance replaced was invoked
  199. on. To override a :class:`Parameter` attribute, pass the corresponding
  200. argument. To remove a default value or/and an annotation from a
  201. Parameter, pass :attr:`Parameter.empty`.
  202. ::
  203. >>> from funcsigs import Parameter
  204. >>> param = Parameter('foo', Parameter.KEYWORD_ONLY, default=42)
  205. >>> str(param)
  206. 'foo=42'
  207. >>> str(param.replace()) # Will create a shallow copy of 'param'
  208. 'foo=42'
  209. >>> str(param.replace(default=Parameter.empty, annotation='spam'))
  210. "foo:'spam'"
  211. .. class:: BoundArguments
  212. Result of a :meth:`Signature.bind` or :meth:`Signature.bind_partial` call.
  213. Holds the mapping of arguments to the function's parameters.
  214. .. attribute:: BoundArguments.arguments
  215. An ordered, mutable mapping (:class:`collections.OrderedDict`) of
  216. parameters' names to arguments' values. Contains only explicitly bound
  217. arguments. Changes in :attr:`arguments` will reflect in :attr:`args` and
  218. :attr:`kwargs`.
  219. Should be used in conjunction with :attr:`Signature.parameters` for any
  220. argument processing purposes.
  221. .. note::
  222. Arguments for which :meth:`Signature.bind` or
  223. :meth:`Signature.bind_partial` relied on a default value are skipped.
  224. However, if needed, it is easy to include them.
  225. ::
  226. >>> def foo(a, b=10):
  227. ... pass
  228. >>> sig = signature(foo)
  229. >>> ba = sig.bind(5)
  230. >>> ba.args, ba.kwargs
  231. ((5,), {})
  232. >>> for param in sig.parameters.values():
  233. ... if param.name not in ba.arguments:
  234. ... ba.arguments[param.name] = param.default
  235. >>> ba.args, ba.kwargs
  236. ((5, 10), {})
  237. .. attribute:: BoundArguments.args
  238. A tuple of positional arguments values. Dynamically computed from the
  239. :attr:`arguments` attribute.
  240. .. attribute:: BoundArguments.kwargs
  241. A dict of keyword arguments values. Dynamically computed from the
  242. :attr:`arguments` attribute.
  243. The :attr:`args` and :attr:`kwargs` properties can be used to invoke
  244. functions::
  245. def test(a, *, b):
  246. ...
  247. sig = signature(test)
  248. ba = sig.bind(10, b=20)
  249. test(*ba.args, **ba.kwargs)
  250. .. seealso::
  251. :pep:`362` - Function Signature Object.
  252. The detailed specification, implementation details and examples.
  253. Copyright
  254. ---------
  255. *funcsigs* is a derived work of CPython under the terms of the `PSF License
  256. Agreement`_. The original CPython inspect module, its unit tests and
  257. documentation are the copyright of the Python Software Foundation. The derived
  258. work is distributed under the `Apache License Version 2.0`_.
  259. .. _PSF License Agreement: http://docs.python.org/3/license.html#terms-and-conditions-for-accessing-or-otherwise-using-python
  260. .. _Apache License Version 2.0: http://opensource.org/licenses/Apache-2.0
  261. .. _GitHub: https://github.com/testing-cabal/funcsigs
  262. .. _PSF License Agreement: http://docs.python.org/3/license.html#terms-and-conditions-for-accessing-or-otherwise-using-python
  263. .. _Travis CI: http://travis-ci.org/
  264. .. _Read The Docs: http://funcsigs.readthedocs.org/
  265. .. _PEP 362: http://www.python.org/dev/peps/pep-0362/
  266. .. _inspect: http://docs.python.org/3/library/inspect.html#introspecting-callables-with-the-signature-object
  267. .. _issues system: https://github.com/testing-cabal/funcsigs/issues
  268. .. |build_status| image:: https://secure.travis-ci.org/aliles/funcsigs.png?branch=master
  269. :target: http://travis-ci.org/#!/aliles/funcsigs
  270. :alt: Current build status
  271. .. |coverage| image:: https://coveralls.io/repos/aliles/funcsigs/badge.png?branch=master
  272. :target: https://coveralls.io/r/aliles/funcsigs?branch=master
  273. :alt: Coverage status
  274. .. |pypi_version| image:: https://pypip.in/v/funcsigs/badge.png
  275. :target: https://crate.io/packages/funcsigs/
  276. :alt: Latest PyPI version