]> granicus.if.org Git - python/commitdiff
Issue #27989: Tweak inspect.formatannotation() to improve pydoc rendering of function...
authorGuido van Rossum <guido@python.org>
Sat, 22 Oct 2016 14:55:18 +0000 (07:55 -0700)
committerGuido van Rossum <guido@python.org>
Sat, 22 Oct 2016 14:55:18 +0000 (07:55 -0700)
Lib/inspect.py
Lib/test/test_pydoc.py

index ca1d551a697043784182d97996fb051f879e5b63..0fd03827763ec66323c9fdc0b92200964c679cd0 100644 (file)
@@ -1152,6 +1152,8 @@ def getargvalues(frame):
     return ArgInfo(args, varargs, varkw, frame.f_locals)
 
 def formatannotation(annotation, base_module=None):
+    if getattr(annotation, '__module__', None) == 'typing':
+        return repr(annotation).replace('typing.', '')
     if isinstance(annotation, type):
         if annotation.__module__ in ('builtins', base_module):
             return annotation.__qualname__
index aee979b8e8b05d0c0afdd0b5a6cee208efde2434..1f7ab7ffa14dd13e293a7e9677d1e5806f1151b9 100644 (file)
@@ -15,6 +15,7 @@ import string
 import test.support
 import time
 import types
+import typing
 import unittest
 import urllib.parse
 import xml.etree
@@ -815,6 +816,18 @@ class TestDescriptions(unittest.TestCase):
         expected = 'C in module %s object' % __name__
         self.assertIn(expected, pydoc.render_doc(c))
 
+    def test_typing_pydoc(self):
+        def foo(data: typing.List[typing.Any],
+                x: int) -> typing.Iterator[typing.Tuple[int, typing.Any]]:
+            ...
+        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)'
+                         ' -> Iterator[Tuple[int, Any]]')
+        self.assertEqual(pydoc.render_doc(C).splitlines()[2],
+                         'class C\x08C(typing.Mapping)')
+
     def test_builtin(self):
         for name in ('str', 'str.translate', 'builtins.str',
                      'builtins.str.translate'):