summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2022-01-22 17:28:48 (GMT)
committerGitHub <noreply@github.com>2022-01-22 17:28:48 (GMT)
commit83aef4d34022f293336f606dba8598cc7ac8f9f2 (patch)
tree73a1d5f670c3e55d4900dd673b35d4cf236dbe15
parent923c994400b3f1c67f95d25c703e131890a16912 (diff)
downloadcpython-83aef4d34022f293336f606dba8598cc7ac8f9f2.zip
cpython-83aef4d34022f293336f606dba8598cc7ac8f9f2.tar.gz
cpython-83aef4d34022f293336f606dba8598cc7ac8f9f2.tar.bz2
bpo-43118: Fix bug in inspect.signature around 'base.__text_signature__' (GH-30285) (#30765)
(cherry picked from commit 881a763cfe07ef4a5806ec78f13a9bc99e8909dc) Co-authored-by: Weipeng Hong <hongweichen8888@sina.com> Co-authored-by: Weipeng Hong <hongweichen8888@sina.com>
-rw-r--r--Lib/inspect.py4
-rw-r--r--Lib/test/ann_module7.py11
-rw-r--r--Lib/test/test_inspect.py11
-rw-r--r--Misc/NEWS.d/next/Library/2021-12-29-14-42-09.bpo-43118.BoVi_5.rst3
4 files changed, 27 insertions, 2 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py
index 6d43d8d..c5881cc 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -2511,9 +2511,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/ann_module7.py b/Lib/test/ann_module7.py
new file mode 100644
index 0000000..8f890cd
--- /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 545dab5..28e4f5b 100644
--- a/Lib/test/test_inspect.py
+++ b/Lib/test/test_inspect.py
@@ -4218,6 +4218,17 @@ class TestSignatureDefinitions(unittest.TestCase):
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 test.ann_module7 import BufferedReader
+ class MyBufferedReader(BufferedReader):
+ """buffer reader class."""
+
+ text_signature = BufferedReader.__text_signature__
+ self.assertEqual(text_signature, '(raw, buffer_size=DEFAULT_BUFFER_SIZE)')
+ sig = inspect.signature(MyBufferedReader)
+ self.assertEqual(str(sig), '(raw, buffer_size=8192)')
+
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 0000000..a37c22c
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-12-29-14-42-09.bpo-43118.BoVi_5.rst
@@ -0,0 +1,3 @@
+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.