Skip to content

Commit

Permalink
Renamings
Browse files Browse the repository at this point in the history
  • Loading branch information
JelleZijlstra committed Oct 1, 2024
1 parent 5831cc4 commit 35ee105
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
13 changes: 8 additions & 5 deletions Doc/library/inspect.rst
Original file line number Diff line number Diff line change
Expand Up @@ -722,10 +722,13 @@ function.
``from __future__ import annotations`` was used), :func:`signature` will
attempt to automatically un-stringize the annotations using
:func:`annotationlib.get_annotations`. The
*globals*, *locals*, *eval_str*, and *annotation_format* parameters are passed
*globals*, *locals*, and *eval_str* parameters are passed
into :func:`!annotationlib.get_annotations` when resolving the
annotations; see the documentation for :func:`!annotationlib.get_annotations`
for instructions on how to use these parameters. For example, use
for instructions on how to use these parameters. A member of the
:class:`annotationlib.Format` enum can be passed to the
*annotation_format* parameter to control the format of the returned
annotations. For example, use
``annotation_format=annotationlib.Format.STRING`` to return annotations in string
format.

Expand Down Expand Up @@ -843,7 +846,7 @@ function.
:class:`Signature` objects are also supported by the generic function
:func:`copy.replace`.

.. method:: format(*, max_width=None, unquote_annotations=False)
.. method:: format(*, max_width=None, quote_annotation_strings=True)

Create a string representation of the :class:`Signature` object.

Expand All @@ -852,9 +855,9 @@ function.
If the signature is longer than *max_width*,
all parameters will be on separate lines.

If *unquote_annotations* is True, :term:`annotations <annotation>`
If *quote_annotation_strings* is False, :term:`annotations <annotation>`
in the signature are displayed without opening and closing quotation
marks. This is useful if the signature was created with the
marks if they are strings. This is useful if the signature was created with the
:attr:`~annotationlib.Format.STRING` format or if
``from __future__ import annotations`` was used.

Expand Down
17 changes: 9 additions & 8 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1320,8 +1320,8 @@ def getargvalues(frame):
args, varargs, varkw = getargs(frame.f_code)
return ArgInfo(args, varargs, varkw, frame.f_locals)

def formatannotation(annotation, base_module=None, *, unquote_annotations=False):
if unquote_annotations and isinstance(annotation, str):
def formatannotation(annotation, base_module=None, *, quote_annotation_strings=True):
if not quote_annotation_strings and isinstance(annotation, str):
return annotation
if getattr(annotation, '__module__', None) == 'typing':
def repl(match):
Expand Down Expand Up @@ -2725,14 +2725,14 @@ def replace(self, *, name=_void, kind=_void,
def __str__(self):
return self._format()

def _format(self, *, unquote_annotations=False):
def _format(self, *, quote_annotation_strings=True):
kind = self.kind
formatted = self._name

# Add annotation and default value
if self._annotation is not _empty:
annotation = formatannotation(self._annotation,
unquote_annotations=unquote_annotations)
quote_annotation_strings=quote_annotation_strings)
formatted = '{}: {}'.format(formatted, annotation)

if self._default is not _empty:
Expand Down Expand Up @@ -3202,15 +3202,15 @@ def __repr__(self):
def __str__(self):
return self.format()

def format(self, *, max_width=None, unquote_annotations=False):
def format(self, *, max_width=None, quote_annotation_strings=True):
"""Create a string representation of the Signature object.
If *max_width* integer is passed,
signature will try to fit into the *max_width*.
If signature is longer than *max_width*,
all parameters will be on separate lines.
If *unquote_annotations* is True, annotations
If *quote_annotation_strings* is False, annotations
in the signature are displayed without opening and closing quotation
marks. This is useful when the signature was created with the
STRING format or when ``from __future__ import annotations`` was used.
Expand All @@ -3219,7 +3219,7 @@ def format(self, *, max_width=None, unquote_annotations=False):
render_pos_only_separator = False
render_kw_only_separator = True
for param in self.parameters.values():
formatted = param._format(unquote_annotations=unquote_annotations)
formatted = param._format(quote_annotation_strings=quote_annotation_strings)

kind = param.kind

Expand Down Expand Up @@ -3256,7 +3256,8 @@ def format(self, *, max_width=None, unquote_annotations=False):
rendered = '(\n {}\n)'.format(',\n '.join(result))

if self.return_annotation is not _empty:
anno = formatannotation(self.return_annotation, unquote_annotations=unquote_annotations)
anno = formatannotation(self.return_annotation,
quote_annotation_strings=quote_annotation_strings)
rendered += ' -> {}'.format(anno)

return rendered
Expand Down
2 changes: 1 addition & 1 deletion Lib/pydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def _getargspec(object):
name = getattr(object, '__name__', '')
# <lambda> function are always single-line and should not be formatted
max_width = (80 - len(name)) if name != '<lambda>' else None
return signature.format(max_width=max_width, unquote_annotations=True)
return signature.format(max_width=max_width, quote_annotation_strings=False)
except (ValueError, TypeError):
argspec = getattr(object, '__text_signature__', None)
if argspec:
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_inspect/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -4631,7 +4631,7 @@ def func(x: 'int') -> 'str': ...
"(x: 'int') -> 'str'"
)
self.assertEqual(
inspect.signature(func).format(unquote_annotations=True),
inspect.signature(func).format(quote_annotation_strings=False),
"(x: int) -> str"
)

Expand Down

0 comments on commit 35ee105

Please sign in to comment.