From 41b817230b0d75e31d465ff698c291977fd3e119 Mon Sep 17 00:00:00 2001 From: Anh71me Date: Sat, 8 Oct 2022 02:23:06 +0800 Subject: [PATCH] GH-96073: Fix wild replacement in inspect.formatannotation (#96074) Co-authored-by: Jelle Zijlstra --- Lib/inspect.py | 5 ++++- Lib/test/test_inspect.py | 7 +++++++ Lib/test/typinganndata/__init__.py | 0 Lib/test/typinganndata/ann_module9.py | 14 ++++++++++++++ .../2022-08-29-12-35-28.gh-issue-96073.WaGstf.rst | 1 + 5 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 Lib/test/typinganndata/__init__.py create mode 100644 Lib/test/typinganndata/ann_module9.py create mode 100644 Misc/NEWS.d/next/Library/2022-08-29-12-35-28.gh-issue-96073.WaGstf.rst diff --git a/Lib/inspect.py b/Lib/inspect.py index 8a107a894909eb..585875a30c35c3 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -1433,7 +1433,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): diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index 710b609ce550d6..61fed323dceae4 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -1421,6 +1421,13 @@ def wrapper(a, b): 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 index 00000000000000..e69de29bb2d1d6 diff --git a/Lib/test/typinganndata/ann_module9.py b/Lib/test/typinganndata/ann_module9.py new file mode 100644 index 00000000000000..952217393e1ff7 --- /dev/null +++ b/Lib/test/typinganndata/ann_module9.py @@ -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 index 00000000000000..0e6dd8d360cbc9 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-08-29-12-35-28.gh-issue-96073.WaGstf.rst @@ -0,0 +1 @@ +In :mod:`inspect`, fix overeager replacement of "`typing.`" in formatting annotations.