Skip to content

Commit 881a763

Browse files
authored
bpo-43118: Fix bug in inspect.signature around 'base.__text_signature__' (GH-30285)
1 parent 00b2b57 commit 881a763

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

Lib/inspect.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2511,9 +2511,9 @@ def _signature_from_callable(obj, *,
25112511
pass
25122512
else:
25132513
if text_sig:
2514-
# If 'obj' class has a __text_signature__ attribute:
2514+
# If 'base' class has a __text_signature__ attribute:
25152515
# return a signature based on it
2516-
return _signature_fromstr(sigcls, obj, text_sig)
2516+
return _signature_fromstr(sigcls, base, text_sig)
25172517

25182518
# No '__text_signature__' was found for the 'obj' class.
25192519
# Last option is to check if its '__init__' is

Lib/test/ann_module7.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Tests class have ``__text_signature__``
2+
3+
from __future__ import annotations
4+
5+
DEFAULT_BUFFER_SIZE = 8192
6+
7+
class BufferedReader(object):
8+
"""BufferedReader(raw, buffer_size=DEFAULT_BUFFER_SIZE)\n--\n\n
9+
Create a new buffered reader using the given readable raw IO object.
10+
"""
11+
pass

Lib/test/test_inspect.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4151,6 +4151,17 @@ def func(*args, **kwargs):
41514151
sig = inspect.signature(func)
41524152
self.assertEqual(str(sig), '(self, a, b=1, /, *args, c, d=2, **kwargs)')
41534153

4154+
def test_base_class_have_text_signature(self):
4155+
# see issue 43118
4156+
from test.ann_module7 import BufferedReader
4157+
class MyBufferedReader(BufferedReader):
4158+
"""buffer reader class."""
4159+
4160+
text_signature = BufferedReader.__text_signature__
4161+
self.assertEqual(text_signature, '(raw, buffer_size=DEFAULT_BUFFER_SIZE)')
4162+
sig = inspect.signature(MyBufferedReader)
4163+
self.assertEqual(str(sig), '(raw, buffer_size=8192)')
4164+
41544165

41554166
class NTimesUnwrappable:
41564167
def __init__(self, n):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix a bug in :func:`inspect.signature` that was causing it to fail on some
2+
subclasses of classes with a ``__text_signature__`` referencing module
3+
globals. Patch by Weipeng Hong.

0 commit comments

Comments
 (0)