summaryrefslogtreecommitdiffstats
path: root/Doc/library/inspect.rst
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2023-12-03 11:57:15 (GMT)
committerGitHub <noreply@github.com>2023-12-03 11:57:15 (GMT)
commit12083c0299557fb95c448dc32aadf31e3831ef4a (patch)
tree28cecbee385d525b016a38aba1f47f7a6071050e /Doc/library/inspect.rst
parent34d57d5871673ba3ed115a5bd7ede9c63effd05c (diff)
downloadcpython-12083c0299557fb95c448dc32aadf31e3831ef4a.zip
cpython-12083c0299557fb95c448dc32aadf31e3831ef4a.tar.gz
cpython-12083c0299557fb95c448dc32aadf31e3831ef4a.tar.bz2
[3.12] Run more `inspect.rst` code snippets in CI (GH-112654) (#112655)
Run more `inspect.rst` code snippets in CI (GH-112654) (cherry picked from commit 4ed46d224401243399b41c7ceef4532bd249da27) Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Diffstat (limited to 'Doc/library/inspect.rst')
-rw-r--r--Doc/library/inspect.rst66
1 files changed, 38 insertions, 28 deletions
diff --git a/Doc/library/inspect.rst b/Doc/library/inspect.rst
index f5331b5..6971b20 100644
--- a/Doc/library/inspect.rst
+++ b/Doc/library/inspect.rst
@@ -392,7 +392,11 @@ attributes (see :ref:`import-mod-attrs` for module attributes):
Return ``True`` if the object can be used in :keyword:`await` expression.
Can also be used to distinguish generator-based coroutines from regular
- generators::
+ generators:
+
+ .. testcode::
+
+ import types
def gen():
yield
@@ -409,13 +413,15 @@ attributes (see :ref:`import-mod-attrs` for module attributes):
.. function:: isasyncgenfunction(object)
Return ``True`` if the object is an :term:`asynchronous generator` function,
- for example::
+ for example:
- >>> async def agen():
- ... yield 1
- ...
- >>> inspect.isasyncgenfunction(agen)
- True
+ .. doctest::
+
+ >>> async def agen():
+ ... yield 1
+ ...
+ >>> inspect.isasyncgenfunction(agen)
+ True
.. versionadded:: 3.6
@@ -968,18 +974,20 @@ function.
For variable-keyword arguments (``**kwargs``) the default is an
empty dict.
- ::
+ .. doctest::
- >>> def foo(a, b='ham', *args): pass
- >>> ba = inspect.signature(foo).bind('spam')
- >>> ba.apply_defaults()
- >>> ba.arguments
- {'a': 'spam', 'b': 'ham', 'args': ()}
+ >>> def foo(a, b='ham', *args): pass
+ >>> ba = inspect.signature(foo).bind('spam')
+ >>> ba.apply_defaults()
+ >>> ba.arguments
+ {'a': 'spam', 'b': 'ham', 'args': ()}
.. versionadded:: 3.5
The :attr:`args` and :attr:`kwargs` properties can be used to invoke
- functions::
+ functions:
+
+ .. testcode::
def test(a, *, b):
...
@@ -1098,20 +1106,22 @@ Classes and functions
``**`` arguments, if any) to their values from *args* and *kwds*. In case of
invoking *func* incorrectly, i.e. whenever ``func(*args, **kwds)`` would raise
an exception because of incompatible signature, an exception of the same type
- and the same or similar message is raised. For example::
-
- >>> from inspect import getcallargs
- >>> def f(a, b=1, *pos, **named):
- ... pass
- ...
- >>> getcallargs(f, 1, 2, 3) == {'a': 1, 'named': {}, 'b': 2, 'pos': (3,)}
- True
- >>> getcallargs(f, a=2, x=4) == {'a': 2, 'named': {'x': 4}, 'b': 1, 'pos': ()}
- True
- >>> getcallargs(f)
- Traceback (most recent call last):
- ...
- TypeError: f() missing 1 required positional argument: 'a'
+ and the same or similar message is raised. For example:
+
+ .. doctest::
+
+ >>> from inspect import getcallargs
+ >>> def f(a, b=1, *pos, **named):
+ ... pass
+ ...
+ >>> getcallargs(f, 1, 2, 3) == {'a': 1, 'named': {}, 'b': 2, 'pos': (3,)}
+ True
+ >>> getcallargs(f, a=2, x=4) == {'a': 2, 'named': {'x': 4}, 'b': 1, 'pos': ()}
+ True
+ >>> getcallargs(f)
+ Traceback (most recent call last):
+ ...
+ TypeError: f() missing 1 required positional argument: 'a'
.. versionadded:: 3.2