1
1
:mod: `inspect ` --- Inspect live objects
2
2
=======================================
3
3
4
+ .. testsetup :: *
5
+
6
+ import inspect
7
+ from inspect import *
8
+
4
9
.. module :: inspect
5
10
:synopsis: Extract information and source code from live objects.
6
11
@@ -614,13 +619,16 @@ Introspecting callables with the Signature object
614
619
615
620
.. versionadded :: 3.3
616
621
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 `
619
625
function.
620
626
621
627
.. function :: signature(callable, *, follow_wrapped=True, globals=None, locals=None, eval_str=False)
622
628
623
- Return a :class: `Signature ` object for the given *callable *::
629
+ Return a :class: `Signature ` object for the given *callable *:
630
+
631
+ .. doctest ::
624
632
625
633
>>> from inspect import signature
626
634
>>> def foo (a , * , b :int , ** kwargs ):
@@ -629,10 +637,10 @@ function.
629
637
>>> sig = signature(foo)
630
638
631
639
>>> str (sig)
632
- '(a, *, b:int, **kwargs)'
640
+ '(a, *, b: int, **kwargs)'
633
641
634
642
>>> str (sig.parameters[' b' ])
635
- 'b:int'
643
+ 'b: int'
636
644
637
645
>>> sig.parameters[' b' ].annotation
638
646
<class 'int'>
@@ -647,7 +655,7 @@ function.
647
655
(``from __future__ import annotations ``), :func: `signature ` will
648
656
attempt to automatically un-stringize the annotations using
649
657
:func: `get_annotations `. The
650
- *global *, *locals *, and *eval_str * parameters are passed
658
+ *globals *, *locals *, and *eval_str * parameters are passed
651
659
into :func: `get_annotations ` when resolving the
652
660
annotations; see the documentation for :func: `get_annotations `
653
661
for instructions on how to use these parameters.
@@ -680,7 +688,8 @@ function.
680
688
681
689
.. class :: Signature(parameters=None, *, return_annotation=Signature.empty)
682
690
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
684
693
annotation. For each parameter accepted by the function it stores a
685
694
:class: `Parameter ` object in its :attr: `parameters ` collection.
686
695
@@ -690,14 +699,14 @@ function.
690
699
positional-only first, then positional-or-keyword, and that parameters with
691
700
defaults follow parameters without defaults.
692
701
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.
695
704
696
- Signature objects are *immutable *. Use :meth: `Signature.replace ` or
705
+ :class: ` ! Signature` objects are *immutable *. Use :meth: `Signature.replace ` or
697
706
:func: `copy.replace ` to make a modified copy.
698
707
699
708
.. versionchanged :: 3.5
700
- Signature objects are picklable and :term: `hashable `.
709
+ :class: ` ! Signature` objects are now picklable and :term: `hashable `.
701
710
702
711
.. attribute :: Signature.empty
703
712
@@ -734,13 +743,15 @@ function.
734
743
735
744
.. method :: Signature.replace(*[, parameters][, return_annotation])
736
745
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
741
752
:attr: `Signature.empty `.
742
753
743
- ::
754
+ .. doctest ::
744
755
745
756
>>> def test (a , b ):
746
757
... pass
@@ -750,12 +761,12 @@ function.
750
761
>>> str (new_sig)
751
762
"(a, b) -> 'new return anno'"
752
763
753
- Signature objects are also supported by generic function
764
+ :class: ` Signature ` objects are also supported by the generic function
754
765
:func: `copy.replace `.
755
766
756
767
.. method :: format(*, max_width=None)
757
768
758
- Convert signature object to string .
769
+ Create a string representation of the :class: ` Signature ` object .
759
770
760
771
If *max_width * is passed, the method will attempt to fit
761
772
the signature into lines of at most *max_width * characters.
@@ -769,12 +780,14 @@ function.
769
780
Return a :class: `Signature ` (or its subclass) object for a given callable
770
781
*obj *.
771
782
772
- This method simplifies subclassing of :class: `Signature `::
783
+ This method simplifies subclassing of :class: `Signature `:
784
+
785
+ .. testcode ::
773
786
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)
778
791
779
792
Its behavior is otherwise identical to that of :func: `signature `.
780
793
@@ -786,11 +799,12 @@ function.
786
799
787
800
.. class :: Parameter(name, kind, *, default=Parameter.empty, annotation=Parameter.empty)
788
801
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,
790
804
you can use :meth: `Parameter.replace ` or :func: `copy.replace ` to create a modified copy.
791
805
792
806
.. versionchanged :: 3.5
793
- Parameter objects are picklable and :term: `hashable `.
807
+ Parameter objects are now picklable and :term: `hashable `.
794
808
795
809
.. attribute :: Parameter.empty
796
810
@@ -809,7 +823,7 @@ function.
809
823
expressions.
810
824
811
825
.. 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
813
827
``implicit0 ``.
814
828
815
829
.. attribute :: Parameter.default
@@ -859,7 +873,9 @@ function.
859
873
| | definition. |
860
874
+------------------------+----------------------------------------------+
861
875
862
- Example: print all keyword-only arguments without default values::
876
+ Example: print all keyword-only arguments without default values:
877
+
878
+ .. doctest ::
863
879
864
880
>>> def foo (a , b , * , c , d = 10 ):
865
881
... pass
@@ -873,11 +889,13 @@ function.
873
889
874
890
.. attribute :: Parameter.kind.description
875
891
876
- Describes a enum value of Parameter.kind.
892
+ Describes a enum value of :attr: ` Parameter.kind ` .
877
893
878
894
.. versionadded :: 3.8
879
895
880
- Example: print all descriptions of arguments::
896
+ Example: print all descriptions of arguments:
897
+
898
+ .. doctest ::
881
899
882
900
>>> def foo (a , b , * , c , d = 10 ):
883
901
... pass
@@ -892,12 +910,12 @@ function.
892
910
893
911
.. method :: Parameter.replace(*[, name][, kind][, default][, annotation])
894
912
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
897
915
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 `.
899
917
900
- ::
918
+ .. doctest ::
901
919
902
920
>>> from inspect import Parameter
903
921
>>> param = Parameter(' foo' , Parameter.KEYWORD_ONLY , default = 42 )
@@ -908,12 +926,13 @@ function.
908
926
'foo=42'
909
927
910
928
>>> str (param.replace(default = Parameter.empty, annotation = ' spam' ))
911
- "foo:'spam'"
929
+ "foo: 'spam'"
912
930
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 `.
914
933
915
934
.. 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
917
936
to ``None `` if their ``kind `` was set to ``POSITIONAL_ONLY ``.
918
937
This is no longer permitted.
919
938
0 commit comments