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
@@ -593,13 +598,16 @@ Introspecting callables with the Signature object
593
598
594
599
.. versionadded :: 3.3
595
600
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 `
598
604
function.
599
605
600
606
.. function :: signature(callable, *, follow_wrapped=True, globals=None, locals=None, eval_str=False)
601
607
602
- Return a :class: `Signature ` object for the given *callable *::
608
+ Return a :class: `Signature ` object for the given *callable *:
609
+
610
+ .. doctest ::
603
611
604
612
>>> from inspect import signature
605
613
>>> def foo (a , * , b :int , ** kwargs ):
@@ -608,10 +616,10 @@ function.
608
616
>>> sig = signature(foo)
609
617
610
618
>>> str (sig)
611
- '(a, *, b:int, **kwargs)'
619
+ '(a, *, b: int, **kwargs)'
612
620
613
621
>>> str (sig.parameters[' b' ])
614
- 'b:int'
622
+ 'b: int'
615
623
616
624
>>> sig.parameters[' b' ].annotation
617
625
<class 'int'>
@@ -626,7 +634,7 @@ function.
626
634
(``from __future__ import annotations ``), :func: `signature ` will
627
635
attempt to automatically un-stringize the annotations using
628
636
:func: `get_annotations `. The
629
- *global *, *locals *, and *eval_str * parameters are passed
637
+ *globals *, *locals *, and *eval_str * parameters are passed
630
638
into :func: `get_annotations ` when resolving the
631
639
annotations; see the documentation for :func: `get_annotations `
632
640
for instructions on how to use these parameters.
@@ -659,7 +667,8 @@ function.
659
667
660
668
.. class :: Signature(parameters=None, *, return_annotation=Signature.empty)
661
669
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
663
672
annotation. For each parameter accepted by the function it stores a
664
673
:class: `Parameter ` object in its :attr: `parameters ` collection.
665
674
@@ -669,14 +678,14 @@ function.
669
678
positional-only first, then positional-or-keyword, and that parameters with
670
679
defaults follow parameters without defaults.
671
680
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.
674
683
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
676
685
modified copy.
677
686
678
687
.. versionchanged :: 3.5
679
- Signature objects are picklable and :term: `hashable `.
688
+ :class: ` ! Signature` objects are now picklable and :term: `hashable `.
680
689
681
690
.. attribute :: Signature.empty
682
691
@@ -713,13 +722,15 @@ function.
713
722
714
723
.. method :: Signature.replace(*[, parameters][, return_annotation])
715
724
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
720
731
:attr: `Signature.empty `.
721
732
722
- ::
733
+ .. doctest ::
723
734
724
735
>>> def test (a , b ):
725
736
... pass
@@ -733,12 +744,14 @@ function.
733
744
Return a :class: `Signature ` (or its subclass) object for a given callable
734
745
*obj *.
735
746
736
- This method simplifies subclassing of :class: `Signature `::
747
+ This method simplifies subclassing of :class: `Signature `:
737
748
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)
742
755
743
756
Its behavior is otherwise identical to that of :func: `signature `.
744
757
@@ -750,11 +763,12 @@ function.
750
763
751
764
.. class :: Parameter(name, kind, *, default=Parameter.empty, annotation=Parameter.empty)
752
765
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,
754
768
you can use :meth: `Parameter.replace ` to create a modified copy.
755
769
756
770
.. versionchanged :: 3.5
757
- Parameter objects are picklable and :term: `hashable `.
771
+ Parameter objects are now picklable and :term: `hashable `.
758
772
759
773
.. attribute :: Parameter.empty
760
774
@@ -773,7 +787,7 @@ function.
773
787
expressions.
774
788
775
789
.. 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
777
791
``implicit0 ``.
778
792
779
793
.. attribute :: Parameter.default
@@ -823,7 +837,9 @@ function.
823
837
| | definition. |
824
838
+------------------------+----------------------------------------------+
825
839
826
- Example: print all keyword-only arguments without default values::
840
+ Example: print all keyword-only arguments without default values:
841
+
842
+ .. doctest ::
827
843
828
844
>>> def foo (a , b , * , c , d = 10 ):
829
845
... pass
@@ -837,11 +853,13 @@ function.
837
853
838
854
.. attribute :: Parameter.kind.description
839
855
840
- Describes a enum value of Parameter.kind.
856
+ Describes a enum value of :attr: ` Parameter.kind ` .
841
857
842
858
.. versionadded :: 3.8
843
859
844
- Example: print all descriptions of arguments::
860
+ Example: print all descriptions of arguments:
861
+
862
+ .. doctest ::
845
863
846
864
>>> def foo (a , b , * , c , d = 10 ):
847
865
... pass
@@ -856,12 +874,12 @@ function.
856
874
857
875
.. method :: Parameter.replace(*[, name][, kind][, default][, annotation])
858
876
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
861
879
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 `.
863
881
864
- ::
882
+ .. doctest ::
865
883
866
884
>>> from inspect import Parameter
867
885
>>> param = Parameter(' foo' , Parameter.KEYWORD_ONLY , default = 42 )
@@ -872,10 +890,10 @@ function.
872
890
'foo=42'
873
891
874
892
>>> str (param.replace(default = Parameter.empty, annotation = ' spam' ))
875
- "foo:'spam'"
893
+ "foo: 'spam'"
876
894
877
895
.. 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
879
897
to ``None `` if their ``kind `` was set to ``POSITIONAL_ONLY ``.
880
898
This is no longer permitted.
881
899
0 commit comments