Skip to content

Commit 3279b58

Browse files
committed
gh-62172: Document formatannotation, formatannotationrelativeto, and getlineno in inspect
1 parent 7745710 commit 3279b58

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

Doc/library/inspect.rst

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,6 +1313,31 @@ Classes and functions
13131313
This function was inadvertently marked as deprecated in Python 3.5.
13141314

13151315

1316+
.. function:: formatannotation(annotation, base_module=None, *, quote_annotation_strings=True)
1317+
1318+
Format *annotation* for pretty-printing, as used internally when
1319+
rendering :class:`Signature` and :class:`Parameter` objects as strings.
1320+
1321+
If *annotation* is a :class:`type` whose ``__module__`` is ``'builtins'``
1322+
or equal to *base_module*, only its :attr:`~definition.__qualname__` is
1323+
returned; otherwise the module name is included as a prefix. ``typing``
1324+
module prefixes are always stripped from the ``repr`` of *annotation*,
1325+
and a :class:`~typing.ForwardRef` is rendered as its forward-referenced
1326+
argument string.
1327+
1328+
If *quote_annotation_strings* is false, an *annotation* that is already
1329+
a :class:`str` is returned unchanged instead of being re-quoted, to avoid
1330+
double-quoting stringified annotations.
1331+
1332+
1333+
.. function:: formatannotationrelativeto(object)
1334+
1335+
Return a version of :func:`formatannotation` that uses the
1336+
``__module__`` of *object* as the *base_module*, so annotations
1337+
referring to classes defined in the same module as *object* are
1338+
formatted without a module prefix.
1339+
1340+
13161341
.. function:: formatargvalues(args[, varargs, varkw, locals, formatarg, formatvarargs, formatvarkw, formatvalue])
13171342

13181343
Format a pretty argument spec from the four values returned by
@@ -1544,6 +1569,15 @@ line.
15441569
.. versionchanged:: 3.11
15451570
A :class:`Traceback` object is returned instead of a named tuple.
15461571

1572+
1573+
.. function:: getlineno(frame)
1574+
1575+
Get the current line number of *frame* from its :attr:`~frame.f_lineno`
1576+
attribute. This function exists as a separate entry point so that how
1577+
the line number is obtained can be optimized independently of its
1578+
callers; currently it returns ``frame.f_lineno`` directly.
1579+
1580+
15471581
.. function:: getouterframes(frame, context=1)
15481582

15491583
Get a list of :class:`FrameInfo` objects for a frame and all outer frames.

0 commit comments

Comments
 (0)