]> granicus.if.org Git - python/commitdiff
bpo-32018: Fix inspect.signature repr to follow PEP 8 (#4408)
authorDong-hee Na <donghee.na92@gmail.com>
Wed, 15 Nov 2017 18:30:59 +0000 (03:30 +0900)
committerYury Selivanov <yury@magic.io>
Wed, 15 Nov 2017 18:30:59 +0000 (13:30 -0500)
Lib/inspect.py
Lib/test/test_inspect.py
Lib/test/test_pydoc.py
Misc/NEWS.d/next/Library/2017-11-16-02-32-41.bpo-32018.YMQ7Q2.rst [new file with mode: 0644]

index 6d6fde9ee4040137b3207f3b8cb1cbfa3936babc..69f2b6209f65c6eb33994fea95ba10b120c5ed0e 100644 (file)
@@ -2521,11 +2521,14 @@ class Parameter:
 
         # Add annotation and default value
         if self._annotation is not _empty:
-            formatted = '{}:{}'.format(formatted,
+            formatted = '{}: {}'.format(formatted,
                                        formatannotation(self._annotation))
 
         if self._default is not _empty:
-            formatted = '{}={}'.format(formatted, repr(self._default))
+            if self._annotation is not _empty:
+                formatted = '{} = {}'.format(formatted, repr(self._default))
+            else:
+                formatted = '{}={}'.format(formatted, repr(self._default))
 
         if kind == _VAR_POSITIONAL:
             formatted = '*' + formatted
index e64215d4677720ac7a79a6e46fc785067e6f52ea..13a86b12dd3d514c02954b6fd78d34bc177d2bde 100644 (file)
@@ -2875,12 +2875,12 @@ class TestSignatureObject(unittest.TestCase):
         def foo(a:int=1, *, b, c=None, **kwargs) -> 42:
             pass
         self.assertEqual(str(inspect.signature(foo)),
-                         '(a:int=1, *, b, c=None, **kwargs) -> 42')
+                         '(a: int = 1, *, b, c=None, **kwargs) -> 42')
 
         def foo(a:int=1, *args, b, c=None, **kwargs) -> 42:
             pass
         self.assertEqual(str(inspect.signature(foo)),
-                         '(a:int=1, *args, b, c=None, **kwargs) -> 42')
+                         '(a: int = 1, *args, b, c=None, **kwargs) -> 42')
 
         def foo():
             pass
index 52830b49aea31f8be2ed55d09a7bde3bedc7e83d..2fa089320fdc7dff448d2e6e6d52fa430aed6aa8 100644 (file)
@@ -824,7 +824,7 @@ class TestDescriptions(unittest.TestCase):
         T = typing.TypeVar('T')
         class C(typing.Generic[T], typing.Mapping[int, str]): ...
         self.assertEqual(pydoc.render_doc(foo).splitlines()[-1],
-                         'f\x08fo\x08oo\x08o(data:List[Any], x:int)'
+                         'f\x08fo\x08oo\x08o(data: List[Any], x: int)'
                          ' -> Iterator[Tuple[int, Any]]')
         self.assertEqual(pydoc.render_doc(C).splitlines()[2],
                          'class C\x08C(typing.Mapping)')
diff --git a/Misc/NEWS.d/next/Library/2017-11-16-02-32-41.bpo-32018.YMQ7Q2.rst b/Misc/NEWS.d/next/Library/2017-11-16-02-32-41.bpo-32018.YMQ7Q2.rst
new file mode 100644 (file)
index 0000000..aa8a47c
--- /dev/null
@@ -0,0 +1,2 @@
+inspect.signature should follow PEP 8, if the parameter has an annotation and a
+default value. Patch by Dong-hee Na.