Run more inspect.rst code snippets in CI (#112654)

This commit is contained in:
Alex Waygood 2023-12-03 11:50:22 +00:00 committed by GitHub
parent a971574b73
commit 4ed46d2244
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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. Return ``True`` if the object can be used in :keyword:`await` expression.
Can also be used to distinguish generator-based coroutines from regular Can also be used to distinguish generator-based coroutines from regular
generators:: generators:
.. testcode::
import types
def gen(): def gen():
yield yield
@ -409,13 +413,15 @@ attributes (see :ref:`import-mod-attrs` for module attributes):
.. function:: isasyncgenfunction(object) .. function:: isasyncgenfunction(object)
Return ``True`` if the object is an :term:`asynchronous generator` function, Return ``True`` if the object is an :term:`asynchronous generator` function,
for example:: for example:
>>> async def agen(): .. doctest::
... yield 1
... >>> async def agen():
>>> inspect.isasyncgenfunction(agen) ... yield 1
True ...
>>> inspect.isasyncgenfunction(agen)
True
.. versionadded:: 3.6 .. versionadded:: 3.6
@ -985,18 +991,20 @@ function.
For variable-keyword arguments (``**kwargs``) the default is an For variable-keyword arguments (``**kwargs``) the default is an
empty dict. empty dict.
:: .. doctest::
>>> def foo(a, b='ham', *args): pass >>> def foo(a, b='ham', *args): pass
>>> ba = inspect.signature(foo).bind('spam') >>> ba = inspect.signature(foo).bind('spam')
>>> ba.apply_defaults() >>> ba.apply_defaults()
>>> ba.arguments >>> ba.arguments
{'a': 'spam', 'b': 'ham', 'args': ()} {'a': 'spam', 'b': 'ham', 'args': ()}
.. versionadded:: 3.5 .. versionadded:: 3.5
The :attr:`args` and :attr:`kwargs` properties can be used to invoke The :attr:`args` and :attr:`kwargs` properties can be used to invoke
functions:: functions:
.. testcode::
def test(a, *, b): def test(a, *, b):
... ...
@ -1115,20 +1123,22 @@ Classes and functions
``**`` arguments, if any) to their values from *args* and *kwds*. In case of ``**`` arguments, if any) to their values from *args* and *kwds*. In case of
invoking *func* incorrectly, i.e. whenever ``func(*args, **kwds)`` would raise invoking *func* incorrectly, i.e. whenever ``func(*args, **kwds)`` would raise
an exception because of incompatible signature, an exception of the same type an exception because of incompatible signature, an exception of the same type
and the same or similar message is raised. For example:: and the same or similar message is raised. For example:
>>> from inspect import getcallargs .. doctest::
>>> def f(a, b=1, *pos, **named):
... pass >>> from inspect import getcallargs
... >>> def f(a, b=1, *pos, **named):
>>> getcallargs(f, 1, 2, 3) == {'a': 1, 'named': {}, 'b': 2, 'pos': (3,)} ... pass
True ...
>>> getcallargs(f, a=2, x=4) == {'a': 2, 'named': {'x': 4}, 'b': 1, 'pos': ()} >>> getcallargs(f, 1, 2, 3) == {'a': 1, 'named': {}, 'b': 2, 'pos': (3,)}
True True
>>> getcallargs(f) >>> getcallargs(f, a=2, x=4) == {'a': 2, 'named': {'x': 4}, 'b': 1, 'pos': ()}
Traceback (most recent call last): True
... >>> getcallargs(f)
TypeError: f() missing 1 required positional argument: 'a' Traceback (most recent call last):
...
TypeError: f() missing 1 required positional argument: 'a'
.. versionadded:: 3.2 .. versionadded:: 3.2