From cb7b6226eb89e67f6a909d0985af345395a0cf1a Mon Sep 17 00:00:00 2001 From: hongweipeng Date: Wed, 29 Dec 2021 14:46:54 +0800 Subject: [PATCH 1/3] in inspect.signature,use 'base' to help parse 'base.__text_signature__'. --- Lib/inspect.py | 4 ++-- Lib/test/test_inspect.py | 10 ++++++++++ .../Library/2021-12-29-14-42-09.bpo-43118.BoVi_5.rst | 2 ++ 3 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2021-12-29-14-42-09.bpo-43118.BoVi_5.rst diff --git a/Lib/inspect.py b/Lib/inspect.py index 1eb2f2b575ef45..f46f2c80bbc6e8 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -2412,9 +2412,9 @@ def _signature_from_callable(obj, *, pass else: if text_sig: - # If 'obj' class has a __text_signature__ attribute: + # If 'base' class has a __text_signature__ attribute: # return a signature based on it - return _signature_fromstr(sigcls, obj, text_sig) + return _signature_fromstr(sigcls, base, text_sig) # No '__text_signature__' was found for the 'obj' class. # Last option is to check if its '__init__' is diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index 33665cf3c79ebf..72ccea88af5a84 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -4148,6 +4148,16 @@ def func(*args, **kwargs): sig = inspect.signature(func) self.assertEqual(str(sig), '(self, a, b=1, /, *args, c, d=2, **kwargs)') + def test_base_class_have_text_signature(self): + # see issue 43118 + from io import BufferedReader + class MyBufferedReader(BufferedReader): + """buffer reader class.""" + + self.assertTrue(hasattr(BufferedReader, '__text_signature__')) + sig = inspect.signature(MyBufferedReader) + self.assertIsNotNone(sig) + class NTimesUnwrappable: def __init__(self, n): diff --git a/Misc/NEWS.d/next/Library/2021-12-29-14-42-09.bpo-43118.BoVi_5.rst b/Misc/NEWS.d/next/Library/2021-12-29-14-42-09.bpo-43118.BoVi_5.rst new file mode 100644 index 00000000000000..82a1c25736d397 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-12-29-14-42-09.bpo-43118.BoVi_5.rst @@ -0,0 +1,2 @@ +In :func:`inspect.signature`, if the 'base' class has ``__text_signature__`` +attribute, use 'base' to help parse it. Patch by Weipeng Hong. From 7e723bfd638f581239f3fab0c01b1fc23d17381c Mon Sep 17 00:00:00 2001 From: hongweipeng Date: Wed, 19 Jan 2022 16:39:04 +0800 Subject: [PATCH 2/3] code review --- Lib/test/ann_module7.py | 11 +++++++++++ Lib/test/test_inspect.py | 7 ++++--- .../Library/2021-12-29-14-42-09.bpo-43118.BoVi_5.rst | 5 +++-- 3 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 Lib/test/ann_module7.py diff --git a/Lib/test/ann_module7.py b/Lib/test/ann_module7.py new file mode 100644 index 00000000000000..8f890cd28025be --- /dev/null +++ b/Lib/test/ann_module7.py @@ -0,0 +1,11 @@ +# Tests class have ``__text_signature__`` + +from __future__ import annotations + +DEFAULT_BUFFER_SIZE = 8192 + +class BufferedReader(object): + """BufferedReader(raw, buffer_size=DEFAULT_BUFFER_SIZE)\n--\n\n + Create a new buffered reader using the given readable raw IO object. + """ + pass diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index 72ccea88af5a84..866b3c92c9f56c 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -4150,13 +4150,14 @@ def func(*args, **kwargs): def test_base_class_have_text_signature(self): # see issue 43118 - from io import BufferedReader + from .ann_module7 import BufferedReader class MyBufferedReader(BufferedReader): """buffer reader class.""" - self.assertTrue(hasattr(BufferedReader, '__text_signature__')) + text_signature = BufferedReader.__text_signature__ + self.assertEqual(text_signature, '(raw, buffer_size=DEFAULT_BUFFER_SIZE)') sig = inspect.signature(MyBufferedReader) - self.assertIsNotNone(sig) + self.assertEqual(str(sig), '(raw, buffer_size=8192)') class NTimesUnwrappable: diff --git a/Misc/NEWS.d/next/Library/2021-12-29-14-42-09.bpo-43118.BoVi_5.rst b/Misc/NEWS.d/next/Library/2021-12-29-14-42-09.bpo-43118.BoVi_5.rst index 82a1c25736d397..a37c22cd78c098 100644 --- a/Misc/NEWS.d/next/Library/2021-12-29-14-42-09.bpo-43118.BoVi_5.rst +++ b/Misc/NEWS.d/next/Library/2021-12-29-14-42-09.bpo-43118.BoVi_5.rst @@ -1,2 +1,3 @@ -In :func:`inspect.signature`, if the 'base' class has ``__text_signature__`` -attribute, use 'base' to help parse it. Patch by Weipeng Hong. +Fix a bug in :func:`inspect.signature` that was causing it to fail on some +subclasses of classes with a ``__text_signature__`` referencing module +globals. Patch by Weipeng Hong. From ff16589b18a4b0596c25853d254f6bd36343044b Mon Sep 17 00:00:00 2001 From: hongweipeng Date: Thu, 20 Jan 2022 09:46:46 +0800 Subject: [PATCH 3/3] code review --- Lib/test/test_inspect.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py index 866b3c92c9f56c..3d5b585fb01e8b 100644 --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -4150,7 +4150,7 @@ def func(*args, **kwargs): def test_base_class_have_text_signature(self): # see issue 43118 - from .ann_module7 import BufferedReader + from test.ann_module7 import BufferedReader class MyBufferedReader(BufferedReader): """buffer reader class."""