Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6ab6c85

Browse files
[3.11] [3.12] gh-112316: improve docs for inspect.signature and inspect.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 <[email protected]>
1 parent db53770 commit 6ab6c85

File tree

1 file changed

+51
-33
lines changed

1 file changed

+51
-33
lines changed

Doc/library/inspect.rst

Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
:mod:`inspect` --- Inspect live objects
22
=======================================
33

4+
.. testsetup:: *
5+
6+
import inspect
7+
from inspect import *
8+
49
.. module:: inspect
510
:synopsis: Extract information and source code from live objects.
611

@@ -593,13 +598,16 @@ Introspecting callables with the Signature object
593598

594599
.. versionadded:: 3.3
595600

596-
The Signature object represents the call signature of a callable object and its
597-
return annotation. To retrieve a Signature object, use the :func:`signature`
601+
The :class:`Signature` object represents the call signature of a callable object
602+
and its return annotation. To retrieve a :class:`!Signature` object,
603+
use the :func:`!signature`
598604
function.
599605

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

602-
Return a :class:`Signature` object for the given *callable*::
608+
Return a :class:`Signature` object for the given *callable*:
609+
610+
.. doctest::
603611

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

610618
>>> str(sig)
611-
'(a, *, b:int, **kwargs)'
619+
'(a, *, b: int, **kwargs)'
612620

613621
>>> str(sig.parameters['b'])
614-
'b:int'
622+
'b: int'
615623

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

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

662-
A Signature object represents the call signature of a function and its return
670+
A :class:`!Signature` object represents the call signature of a function
671+
and its return
663672
annotation. For each parameter accepted by the function it stores a
664673
:class:`Parameter` object in its :attr:`parameters` collection.
665674

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

672-
The optional *return_annotation* argument, can be an arbitrary Python object,
673-
is the "return" annotation of the callable.
681+
The optional *return_annotation* argument can be an arbitrary Python object.
682+
It represents the "return" annotation of the callable.
674683

675-
Signature objects are *immutable*. Use :meth:`Signature.replace` to make a
684+
:class:`!Signature` objects are *immutable*. Use :meth:`Signature.replace` to make a
676685
modified copy.
677686

678687
.. versionchanged:: 3.5
679-
Signature objects are picklable and :term:`hashable`.
688+
:class:`!Signature` objects are now picklable and :term:`hashable`.
680689

681690
.. attribute:: Signature.empty
682691

@@ -713,13 +722,15 @@ function.
713722

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

716-
Create a new Signature instance based on the instance :meth:`replace` was invoked
717-
on. It is possible to pass different ``parameters`` and/or
718-
``return_annotation`` to override the corresponding properties of the base
719-
signature. To remove return_annotation from the copied Signature, pass in
725+
Create a new :class:`Signature` instance based on the instance
726+
:meth:`replace` was invoked on.
727+
It is possible to pass different *parameters* and/or
728+
*return_annotation* to override the corresponding properties of the base
729+
signature. To remove ``return_annotation`` from the copied
730+
:class:`!Signature`, pass in
720731
:attr:`Signature.empty`.
721732

722-
::
733+
.. doctest::
723734

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

736-
This method simplifies subclassing of :class:`Signature`::
747+
This method simplifies subclassing of :class:`Signature`:
737748

738-
class MySignature(Signature):
739-
pass
740-
sig = MySignature.from_callable(min)
741-
assert isinstance(sig, MySignature)
749+
.. testcode::
750+
751+
class MySignature(Signature):
752+
pass
753+
sig = MySignature.from_callable(sum)
754+
assert isinstance(sig, MySignature)
742755

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

@@ -750,11 +763,12 @@ function.
750763

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

753-
Parameter objects are *immutable*. Instead of modifying a Parameter object,
766+
:class:`!Parameter` objects are *immutable*.
767+
Instead of modifying a :class:`!Parameter` object,
754768
you can use :meth:`Parameter.replace` to create a modified copy.
755769

756770
.. versionchanged:: 3.5
757-
Parameter objects are picklable and :term:`hashable`.
771+
Parameter objects are now picklable and :term:`hashable`.
758772

759773
.. attribute:: Parameter.empty
760774

@@ -773,7 +787,7 @@ function.
773787
expressions.
774788

775789
.. versionchanged:: 3.6
776-
These parameter names are exposed by this module as names like
790+
These parameter names are now exposed by this module as names like
777791
``implicit0``.
778792

779793
.. attribute:: Parameter.default
@@ -823,7 +837,9 @@ function.
823837
| | definition. |
824838
+------------------------+----------------------------------------------+
825839

826-
Example: print all keyword-only arguments without default values::
840+
Example: print all keyword-only arguments without default values:
841+
842+
.. doctest::
827843

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

838854
.. attribute:: Parameter.kind.description
839855

840-
Describes a enum value of Parameter.kind.
856+
Describes a enum value of :attr:`Parameter.kind`.
841857

842858
.. versionadded:: 3.8
843859

844-
Example: print all descriptions of arguments::
860+
Example: print all descriptions of arguments:
861+
862+
.. doctest::
845863

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

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

859-
Create a new Parameter instance based on the instance replaced was invoked
860-
on. To override a :class:`Parameter` attribute, pass the corresponding
877+
Create a new :class:`Parameter` instance based on the instance replaced was invoked
878+
on. To override a :class:`!Parameter` attribute, pass the corresponding
861879
argument. To remove a default value or/and an annotation from a
862-
Parameter, pass :attr:`Parameter.empty`.
880+
:class:`!Parameter`, pass :attr:`Parameter.empty`.
863881

864-
::
882+
.. doctest::
865883

866884
>>> from inspect import Parameter
867885
>>> param = Parameter('foo', Parameter.KEYWORD_ONLY, default=42)
@@ -872,10 +890,10 @@ function.
872890
'foo=42'
873891

874892
>>> str(param.replace(default=Parameter.empty, annotation='spam'))
875-
"foo:'spam'"
893+
"foo: 'spam'"
876894

877895
.. versionchanged:: 3.4
878-
In Python 3.3 Parameter objects were allowed to have ``name`` set
896+
In Python 3.3 :class:`Parameter` objects were allowed to have ``name`` set
879897
to ``None`` if their ``kind`` was set to ``POSITIONAL_ONLY``.
880898
This is no longer permitted.
881899

0 commit comments

Comments
 (0)