summaryrefslogtreecommitdiffstats
path: root/Doc/library/inspect.rst
diff options
context:
space:
mode:
authorBatuhan Taskaya <isidentical@gmail.com>2020-12-23 22:45:13 (GMT)
committerGitHub <noreply@github.com>2020-12-23 22:45:13 (GMT)
commiteee1c7745ab4eb4f75153e71aaa2a62018b7625a (patch)
tree53ba864b0213e139d3b4f03ca8815bab24448740 /Doc/library/inspect.rst
parent6b1ac809b9718a369aea67b99077cdd682be2238 (diff)
downloadcpython-eee1c7745ab4eb4f75153e71aaa2a62018b7625a.zip
cpython-eee1c7745ab4eb4f75153e71aaa2a62018b7625a.tar.gz
cpython-eee1c7745ab4eb4f75153e71aaa2a62018b7625a.tar.bz2
bpo-41960: Add globalns and localns parameters to inspect.signature and Signature.from_callable (GH-22583)
Diffstat (limited to 'Doc/library/inspect.rst')
-rw-r--r--Doc/library/inspect.rst22
1 files changed, 19 insertions, 3 deletions
diff --git a/Doc/library/inspect.rst b/Doc/library/inspect.rst
index b53a942..850d601 100644
--- a/Doc/library/inspect.rst
+++ b/Doc/library/inspect.rst
@@ -556,7 +556,7 @@ The Signature object represents the call signature of a callable object and its
return annotation. To retrieve a Signature object, use the :func:`signature`
function.
-.. function:: signature(callable, *, follow_wrapped=True)
+.. function:: signature(callable, *, follow_wrapped=True, globalns=None, localns=None)
Return a :class:`Signature` object for the given ``callable``::
@@ -581,6 +581,9 @@ function.
Raises :exc:`ValueError` if no signature can be provided, and
:exc:`TypeError` if that type of object is not supported.
+ ``globalns`` and ``localns`` are passed into
+ :func:`typing.get_type_hints` when resolving the annotations.
+
A slash(/) in the signature of a function denotes that the parameters prior
to it are positional-only. For more info, see
:ref:`the FAQ entry on positional-only parameters <faq-positional-only-arguments>`.
@@ -590,12 +593,21 @@ function.
``callable`` specifically (``callable.__wrapped__`` will not be used to
unwrap decorated callables.)
+ .. versionadded:: 3.10
+ ``globalns`` and ``localns`` parameters.
+
.. note::
Some callables may not be introspectable in certain implementations of
Python. For example, in CPython, some built-in functions defined in
C provide no metadata about their arguments.
+ .. note::
+
+ Will first try to resolve the annotations, but when it fails and
+ encounters with an error while that operation, the annotations will be
+ returned unchanged (as strings).
+
.. class:: Signature(parameters=None, *, return_annotation=Signature.empty)
@@ -668,11 +680,12 @@ function.
>>> str(new_sig)
"(a, b) -> 'new return anno'"
- .. classmethod:: Signature.from_callable(obj, *, follow_wrapped=True)
+ .. classmethod:: Signature.from_callable(obj, *, follow_wrapped=True, globalns=None, localns=None)
Return a :class:`Signature` (or its subclass) object for a given callable
``obj``. Pass ``follow_wrapped=False`` to get a signature of ``obj``
- without unwrapping its ``__wrapped__`` chain.
+ without unwrapping its ``__wrapped__`` chain. ``globalns`` and
+ ``localns`` will be used as the namespaces when resolving annotations.
This method simplifies subclassing of :class:`Signature`::
@@ -683,6 +696,9 @@ function.
.. versionadded:: 3.5
+ .. versionadded:: 3.10
+ ``globalns`` and ``localns`` parameters.
+
.. class:: Parameter(name, kind, *, default=Parameter.empty, annotation=Parameter.empty)