Skip to content

Commit

Permalink
[3.11] [3.12] gh-112316: improve docs for inspect.signature and `in…
Browse files Browse the repository at this point in the history
…spect.Signature` (GH-112631) (GH-112649) (#112652)

[3.12] gh-112316: improve docs for `inspect.signature` and `inspect.Signature` (GH-112631) (GH-112649)

(cherry-picked from commit fc9e24b)
(cherry picked from commit 6221482)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
  • Loading branch information
miss-islington and AlexWaygood authored Dec 3, 2023
1 parent db53770 commit 6ab6c85
Showing 1 changed file with 51 additions and 33 deletions.
84 changes: 51 additions & 33 deletions Doc/library/inspect.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
:mod:`inspect` --- Inspect live objects
=======================================

.. testsetup:: *

import inspect
from inspect import *

.. module:: inspect
:synopsis: Extract information and source code from live objects.

Expand Down Expand Up @@ -593,13 +598,16 @@ Introspecting callables with the Signature object

.. versionadded:: 3.3

The Signature object represents the call signature of a callable object and its
return annotation. To retrieve a Signature object, use the :func:`signature`
The :class:`Signature` object represents the call signature of a callable object
and its return annotation. To retrieve a :class:`!Signature` object,
use the :func:`!signature`
function.

.. function:: signature(callable, *, follow_wrapped=True, globals=None, locals=None, eval_str=False)

Return a :class:`Signature` object for the given *callable*::
Return a :class:`Signature` object for the given *callable*:

.. doctest::

>>> from inspect import signature
>>> def foo(a, *, b:int, **kwargs):
Expand All @@ -608,10 +616,10 @@ function.
>>> sig = signature(foo)

>>> str(sig)
'(a, *, b:int, **kwargs)'
'(a, *, b: int, **kwargs)'

>>> str(sig.parameters['b'])
'b:int'
'b: int'

>>> sig.parameters['b'].annotation
<class 'int'>
Expand All @@ -626,7 +634,7 @@ function.
(``from __future__ import annotations``), :func:`signature` will
attempt to automatically un-stringize the annotations using
:func:`get_annotations`. The
*global*, *locals*, and *eval_str* parameters are passed
*globals*, *locals*, and *eval_str* parameters are passed
into :func:`get_annotations` when resolving the
annotations; see the documentation for :func:`get_annotations`
for instructions on how to use these parameters.
Expand Down Expand Up @@ -659,7 +667,8 @@ function.

.. class:: Signature(parameters=None, *, return_annotation=Signature.empty)

A Signature object represents the call signature of a function and its return
A :class:`!Signature` object represents the call signature of a function
and its return
annotation. For each parameter accepted by the function it stores a
:class:`Parameter` object in its :attr:`parameters` collection.

Expand All @@ -669,14 +678,14 @@ function.
positional-only first, then positional-or-keyword, and that parameters with
defaults follow parameters without defaults.

The optional *return_annotation* argument, can be an arbitrary Python object,
is the "return" annotation of the callable.
The optional *return_annotation* argument can be an arbitrary Python object.
It represents the "return" annotation of the callable.

Signature objects are *immutable*. Use :meth:`Signature.replace` to make a
:class:`!Signature` objects are *immutable*. Use :meth:`Signature.replace` to make a
modified copy.

.. versionchanged:: 3.5
Signature objects are picklable and :term:`hashable`.
:class:`!Signature` objects are now picklable and :term:`hashable`.

.. attribute:: Signature.empty

Expand Down Expand Up @@ -713,13 +722,15 @@ function.

.. method:: Signature.replace(*[, parameters][, return_annotation])

Create a new Signature instance based on the instance :meth:`replace` was invoked
on. It is possible to pass different ``parameters`` and/or
``return_annotation`` to override the corresponding properties of the base
signature. To remove return_annotation from the copied Signature, pass in
Create a new :class:`Signature` instance based on the instance
:meth:`replace` was invoked on.
It is possible to pass different *parameters* and/or
*return_annotation* to override the corresponding properties of the base
signature. To remove ``return_annotation`` from the copied
:class:`!Signature`, pass in
:attr:`Signature.empty`.

::
.. doctest::

>>> def test(a, b):
... pass
Expand All @@ -733,12 +744,14 @@ function.
Return a :class:`Signature` (or its subclass) object for a given callable
*obj*.

This method simplifies subclassing of :class:`Signature`::
This method simplifies subclassing of :class:`Signature`:

class MySignature(Signature):
pass
sig = MySignature.from_callable(min)
assert isinstance(sig, MySignature)
.. testcode::

class MySignature(Signature):
pass
sig = MySignature.from_callable(sum)
assert isinstance(sig, MySignature)

Its behavior is otherwise identical to that of :func:`signature`.

Expand All @@ -750,11 +763,12 @@ function.

.. class:: Parameter(name, kind, *, default=Parameter.empty, annotation=Parameter.empty)

Parameter objects are *immutable*. Instead of modifying a Parameter object,
:class:`!Parameter` objects are *immutable*.
Instead of modifying a :class:`!Parameter` object,
you can use :meth:`Parameter.replace` to create a modified copy.

.. versionchanged:: 3.5
Parameter objects are picklable and :term:`hashable`.
Parameter objects are now picklable and :term:`hashable`.

.. attribute:: Parameter.empty

Expand All @@ -773,7 +787,7 @@ function.
expressions.

.. versionchanged:: 3.6
These parameter names are exposed by this module as names like
These parameter names are now exposed by this module as names like
``implicit0``.

.. attribute:: Parameter.default
Expand Down Expand Up @@ -823,7 +837,9 @@ function.
| | definition. |
+------------------------+----------------------------------------------+

Example: print all keyword-only arguments without default values::
Example: print all keyword-only arguments without default values:

.. doctest::

>>> def foo(a, b, *, c, d=10):
... pass
Expand All @@ -837,11 +853,13 @@ function.

.. attribute:: Parameter.kind.description

Describes a enum value of Parameter.kind.
Describes a enum value of :attr:`Parameter.kind`.

.. versionadded:: 3.8

Example: print all descriptions of arguments::
Example: print all descriptions of arguments:

.. doctest::

>>> def foo(a, b, *, c, d=10):
... pass
Expand All @@ -856,12 +874,12 @@ function.

.. method:: Parameter.replace(*[, name][, kind][, default][, annotation])

Create a new Parameter instance based on the instance replaced was invoked
on. To override a :class:`Parameter` attribute, pass the corresponding
Create a new :class:`Parameter` instance based on the instance replaced was invoked
on. To override a :class:`!Parameter` attribute, pass the corresponding
argument. To remove a default value or/and an annotation from a
Parameter, pass :attr:`Parameter.empty`.
:class:`!Parameter`, pass :attr:`Parameter.empty`.

::
.. doctest::

>>> from inspect import Parameter
>>> param = Parameter('foo', Parameter.KEYWORD_ONLY, default=42)
Expand All @@ -872,10 +890,10 @@ function.
'foo=42'

>>> str(param.replace(default=Parameter.empty, annotation='spam'))
"foo:'spam'"
"foo: 'spam'"

.. versionchanged:: 3.4
In Python 3.3 Parameter objects were allowed to have ``name`` set
In Python 3.3 :class:`Parameter` objects were allowed to have ``name`` set
to ``None`` if their ``kind`` was set to ``POSITIONAL_ONLY``.
This is no longer permitted.

Expand Down

0 comments on commit 6ab6c85

Please sign in to comment.