]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-96073: Fix wild replacement in inspect.formatannotation (GH-96074)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 7 Oct 2022 19:56:29 +0000 (12:56 -0700)
committerGitHub <noreply@github.com>
Fri, 7 Oct 2022 19:56:29 +0000 (12:56 -0700)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
(cherry picked from commit d5fea01d9d439b1638cd8e5db19c33909841d86f)

Co-authored-by: Anh71me <iyumelive@gmail.com>
Lib/inspect.py
Lib/test/test_inspect.py
Lib/test/typinganndata/__init__.py [new file with mode: 0644]
Lib/test/typinganndata/ann_module9.py [new file with mode: 0644]
Misc/NEWS.d/next/Library/2022-08-29-12-35-28.gh-issue-96073.WaGstf.rst [new file with mode: 0644]

index 01df575e4bfee425c97b878dae698d72704606ec..60740c63b2ae1923c58ff0d2d3add00ba850696a 100644 (file)
@@ -1356,7 +1356,10 @@ def getargvalues(frame):
 
 def formatannotation(annotation, base_module=None):
     if getattr(annotation, '__module__', None) == 'typing':
-        return repr(annotation).replace('typing.', '')
+        def repl(match):
+            text = match.group()
+            return text.removeprefix('typing.')
+        return re.sub(r'[\w\.]+', repl, repr(annotation))
     if isinstance(annotation, types.GenericAlias):
         return str(annotation)
     if isinstance(annotation, type):
index eaefe946e8963fe73a8a289473752f1b91bdcda9..16fef5cab18d2a9483de4e0c9fea6f5ad40e2d24 100644 (file)
@@ -1418,6 +1418,13 @@ class TestClassesAndFunctions(unittest.TestCase):
         self.assertEqual(inspect.get_annotations(isa.MyClassWithLocalAnnotations, eval_str=True), {'x': int})
 
 
+class TestFormatAnnotation(unittest.TestCase):
+    def test_typing_replacement(self):
+        from test.typinganndata.ann_module9 import ann, ann1
+        self.assertEqual(inspect.formatannotation(ann), 'Union[List[str], int]')
+        self.assertEqual(inspect.formatannotation(ann1), 'Union[List[testModule.typing.A], int]')
+
+
 class TestIsDataDescriptor(unittest.TestCase):
 
     def test_custom_descriptors(self):
diff --git a/Lib/test/typinganndata/__init__.py b/Lib/test/typinganndata/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/Lib/test/typinganndata/ann_module9.py b/Lib/test/typinganndata/ann_module9.py
new file mode 100644 (file)
index 0000000..9522173
--- /dev/null
@@ -0,0 +1,14 @@
+# Test ``inspect.formatannotation``
+# https://github.com/python/cpython/issues/96073
+
+from typing import Union, List
+
+ann = Union[List[str], int]
+
+# mock typing._type_repr behaviour
+class A: ...
+
+A.__module__ = 'testModule.typing'
+A.__qualname__ = 'A'
+
+ann1 = Union[List[A], int]
diff --git a/Misc/NEWS.d/next/Library/2022-08-29-12-35-28.gh-issue-96073.WaGstf.rst b/Misc/NEWS.d/next/Library/2022-08-29-12-35-28.gh-issue-96073.WaGstf.rst
new file mode 100644 (file)
index 0000000..0e6dd8d
--- /dev/null
@@ -0,0 +1 @@
+In :mod:`inspect`, fix overeager replacement of "`typing.`" in formatting annotations.