Skip to content

Commit 762b957

Browse files
corona101st1
authored andcommitted
bpo-32018: Fix inspect.signature repr to follow PEP 8 (#4408)
1 parent f8a4c03 commit 762b957

File tree

4 files changed

+10
-5
lines changed

4 files changed

+10
-5
lines changed

Lib/inspect.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2521,11 +2521,14 @@ def __str__(self):
25212521

25222522
# Add annotation and default value
25232523
if self._annotation is not _empty:
2524-
formatted = '{}:{}'.format(formatted,
2524+
formatted = '{}: {}'.format(formatted,
25252525
formatannotation(self._annotation))
25262526

25272527
if self._default is not _empty:
2528-
formatted = '{}={}'.format(formatted, repr(self._default))
2528+
if self._annotation is not _empty:
2529+
formatted = '{} = {}'.format(formatted, repr(self._default))
2530+
else:
2531+
formatted = '{}={}'.format(formatted, repr(self._default))
25292532

25302533
if kind == _VAR_POSITIONAL:
25312534
formatted = '*' + formatted

Lib/test/test_inspect.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2875,12 +2875,12 @@ def test_signature_str(self):
28752875
def foo(a:int=1, *, b, c=None, **kwargs) -> 42:
28762876
pass
28772877
self.assertEqual(str(inspect.signature(foo)),
2878-
'(a:int=1, *, b, c=None, **kwargs) -> 42')
2878+
'(a: int = 1, *, b, c=None, **kwargs) -> 42')
28792879

28802880
def foo(a:int=1, *args, b, c=None, **kwargs) -> 42:
28812881
pass
28822882
self.assertEqual(str(inspect.signature(foo)),
2883-
'(a:int=1, *args, b, c=None, **kwargs) -> 42')
2883+
'(a: int = 1, *args, b, c=None, **kwargs) -> 42')
28842884

28852885
def foo():
28862886
pass

Lib/test/test_pydoc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ def foo(data: typing.List[typing.Any],
824824
T = typing.TypeVar('T')
825825
class C(typing.Generic[T], typing.Mapping[int, str]): ...
826826
self.assertEqual(pydoc.render_doc(foo).splitlines()[-1],
827-
'f\x08fo\x08oo\x08o(data:List[Any], x:int)'
827+
'f\x08fo\x08oo\x08o(data: List[Any], x: int)'
828828
' -> Iterator[Tuple[int, Any]]')
829829
self.assertEqual(pydoc.render_doc(C).splitlines()[2],
830830
'class C\x08C(typing.Mapping)')
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
inspect.signature should follow PEP 8, if the parameter has an annotation and a
2+
default value. Patch by Dong-hee Na.

0 commit comments

Comments
 (0)