Skip to content

Commit 83aef4d

Browse files
bpo-43118: Fix bug in inspect.signature around 'base.__text_signature__' (GH-30285) (#30765)
(cherry picked from commit 881a763) Co-authored-by: Weipeng Hong <[email protected]> Co-authored-by: Weipeng Hong <[email protected]>
1 parent 923c994 commit 83aef4d

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
@@ -4218,6 +4218,17 @@ def func(*args, **kwargs):
42184218
sig = inspect.signature(func)
42194219
self.assertEqual(str(sig), '(self, a, b=1, /, *args, c, d=2, **kwargs)')
42204220

4221+
def test_base_class_have_text_signature(self):
4222+
# see issue 43118
4223+
from test.ann_module7 import BufferedReader
4224+
class MyBufferedReader(BufferedReader):
4225+
"""buffer reader class."""
4226+
4227+
text_signature = BufferedReader.__text_signature__
4228+
self.assertEqual(text_signature, '(raw, buffer_size=DEFAULT_BUFFER_SIZE)')
4229+
sig = inspect.signature(MyBufferedReader)
4230+
self.assertEqual(str(sig), '(raw, buffer_size=8192)')
4231+
42214232

42224233
class NTimesUnwrappable:
42234234
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)