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
@@ -614,13 +619,16 @@ Introspecting callables with the Signature object
614619
615620.. versionadded :: 3.3
616621
617- The Signature object represents the call signature of a callable object and its
618- return annotation. To retrieve a Signature object, use the :func: `signature `
622+ The :class: `Signature ` object represents the call signature of a callable object
623+ and its return annotation. To retrieve a :class: `!Signature ` object,
624+ use the :func: `!signature `
619625function.
620626
621627.. function :: signature(callable, *, follow_wrapped=True, globals=None, locals=None, eval_str=False)
622628
623- Return a :class: `Signature ` object for the given *callable *::
629+ Return a :class: `Signature ` object for the given *callable *:
630+
631+ .. doctest ::
624632
625633 >>> from inspect import signature
626634 >>> def foo (a , * , b :int , ** kwargs ):
@@ -629,10 +637,10 @@ function.
629637 >>> sig = signature(foo)
630638
631639 >>> str (sig)
632- '(a, *, b:int, **kwargs)'
640+ '(a, *, b: int, **kwargs)'
633641
634642 >>> str (sig.parameters[' b' ])
635- 'b:int'
643+ 'b: int'
636644
637645 >>> sig.parameters[' b' ].annotation
638646 <class 'int'>
@@ -647,7 +655,7 @@ function.
647655 (``from __future__ import annotations ``), :func: `signature ` will
648656 attempt to automatically un-stringize the annotations using
649657 :func: `get_annotations `. The
650- *global *, *locals *, and *eval_str * parameters are passed
658+ *globals *, *locals *, and *eval_str * parameters are passed
651659 into :func: `get_annotations ` when resolving the
652660 annotations; see the documentation for :func: `get_annotations `
653661 for instructions on how to use these parameters.
@@ -680,7 +688,8 @@ function.
680688
681689.. class :: Signature(parameters=None, *, return_annotation=Signature.empty)
682690
683- A Signature object represents the call signature of a function and its return
691+ A :class: `!Signature ` object represents the call signature of a function
692+ and its return
684693 annotation. For each parameter accepted by the function it stores a
685694 :class: `Parameter ` object in its :attr: `parameters ` collection.
686695
@@ -690,14 +699,14 @@ function.
690699 positional-only first, then positional-or-keyword, and that parameters with
691700 defaults follow parameters without defaults.
692701
693- The optional *return_annotation * argument, can be an arbitrary Python object,
694- is the "return" annotation of the callable.
702+ The optional *return_annotation * argument can be an arbitrary Python object.
703+ It represents the "return" annotation of the callable.
695704
696- Signature objects are *immutable *. Use :meth: `Signature.replace ` or
705+ :class: ` ! Signature` objects are *immutable *. Use :meth: `Signature.replace ` or
697706 :func: `copy.replace ` to make a modified copy.
698707
699708 .. versionchanged :: 3.5
700- Signature objects are picklable and :term: `hashable `.
709+ :class: ` ! Signature` objects are now picklable and :term: `hashable `.
701710
702711 .. attribute :: Signature.empty
703712
@@ -734,13 +743,15 @@ function.
734743
735744 .. method :: Signature.replace(*[, parameters][, return_annotation])
736745
737- Create a new Signature instance based on the instance :meth: `replace ` was invoked
738- on. It is possible to pass different ``parameters `` and/or
739- ``return_annotation `` to override the corresponding properties of the base
740- signature. To remove return_annotation from the copied Signature, pass in
746+ Create a new :class: `Signature ` instance based on the instance
747+ :meth: `replace ` was invoked on.
748+ It is possible to pass different *parameters * and/or
749+ *return_annotation * to override the corresponding properties of the base
750+ signature. To remove ``return_annotation `` from the copied
751+ :class: `!Signature `, pass in
741752 :attr: `Signature.empty `.
742753
743- ::
754+ .. doctest ::
744755
745756 >>> def test (a , b ):
746757 ... pass
@@ -750,12 +761,12 @@ function.
750761 >>> str (new_sig)
751762 "(a, b) -> 'new return anno'"
752763
753- Signature objects are also supported by generic function
764+ :class: ` Signature ` objects are also supported by the generic function
754765 :func: `copy.replace `.
755766
756767 .. method :: format(*, max_width=None)
757768
758- Convert signature object to string .
769+ Create a string representation of the :class: ` Signature ` object .
759770
760771 If *max_width * is passed, the method will attempt to fit
761772 the signature into lines of at most *max_width * characters.
@@ -769,12 +780,14 @@ function.
769780 Return a :class: `Signature ` (or its subclass) object for a given callable
770781 *obj *.
771782
772- This method simplifies subclassing of :class: `Signature `::
783+ This method simplifies subclassing of :class: `Signature `:
784+
785+ .. testcode ::
773786
774- class MySignature(Signature):
775- pass
776- sig = MySignature.from_callable(min )
777- assert isinstance(sig, MySignature)
787+ class MySignature(Signature):
788+ pass
789+ sig = MySignature.from_callable(sum )
790+ assert isinstance(sig, MySignature)
778791
779792 Its behavior is otherwise identical to that of :func: `signature `.
780793
@@ -786,11 +799,12 @@ function.
786799
787800.. class :: Parameter(name, kind, *, default=Parameter.empty, annotation=Parameter.empty)
788801
789- Parameter objects are *immutable *. Instead of modifying a Parameter object,
802+ :class: `!Parameter ` objects are *immutable *.
803+ Instead of modifying a :class: `!Parameter ` object,
790804 you can use :meth: `Parameter.replace ` or :func: `copy.replace ` to create a modified copy.
791805
792806 .. versionchanged :: 3.5
793- Parameter objects are picklable and :term: `hashable `.
807+ Parameter objects are now picklable and :term: `hashable `.
794808
795809 .. attribute :: Parameter.empty
796810
@@ -809,7 +823,7 @@ function.
809823 expressions.
810824
811825 .. versionchanged :: 3.6
812- These parameter names are exposed by this module as names like
826+ These parameter names are now exposed by this module as names like
813827 ``implicit0 ``.
814828
815829 .. attribute :: Parameter.default
@@ -859,7 +873,9 @@ function.
859873 | | definition. |
860874 +------------------------+----------------------------------------------+
861875
862- Example: print all keyword-only arguments without default values::
876+ Example: print all keyword-only arguments without default values:
877+
878+ .. doctest ::
863879
864880 >>> def foo (a , b , * , c , d = 10 ):
865881 ... pass
@@ -873,11 +889,13 @@ function.
873889
874890 .. attribute :: Parameter.kind.description
875891
876- Describes a enum value of Parameter.kind.
892+ Describes a enum value of :attr: ` Parameter.kind ` .
877893
878894 .. versionadded :: 3.8
879895
880- Example: print all descriptions of arguments::
896+ Example: print all descriptions of arguments:
897+
898+ .. doctest ::
881899
882900 >>> def foo (a , b , * , c , d = 10 ):
883901 ... pass
@@ -892,12 +910,12 @@ function.
892910
893911 .. method :: Parameter.replace(*[, name][, kind][, default][, annotation])
894912
895- Create a new Parameter instance based on the instance replaced was invoked
896- on. To override a :class: `Parameter ` attribute, pass the corresponding
913+ Create a new :class: ` Parameter ` instance based on the instance replaced was invoked
914+ on. To override a :class: `! Parameter ` attribute, pass the corresponding
897915 argument. To remove a default value or/and an annotation from a
898- Parameter, pass :attr: `Parameter.empty `.
916+ :class: ` ! Parameter` , pass :attr: `Parameter.empty `.
899917
900- ::
918+ .. doctest ::
901919
902920 >>> from inspect import Parameter
903921 >>> param = Parameter(' foo' , Parameter.KEYWORD_ONLY , default = 42 )
@@ -908,12 +926,13 @@ function.
908926 'foo=42'
909927
910928 >>> str (param.replace(default = Parameter.empty, annotation = ' spam' ))
911- "foo:'spam'"
929+ "foo: 'spam'"
912930
913- Parameter objects are also supported by generic function :func: `copy.replace `.
931+ :class: `Parameter ` objects are also supported by the generic function
932+ :func: `copy.replace `.
914933
915934 .. versionchanged :: 3.4
916- In Python 3.3 Parameter objects were allowed to have ``name `` set
935+ In Python 3.3 :class: ` Parameter ` objects were allowed to have ``name `` set
917936 to ``None `` if their ``kind `` was set to ``POSITIONAL_ONLY ``.
918937 This is no longer permitted.
919938
0 commit comments