[3.11] gh-85267: Improvements to inspect.signature __text_signature__… · python/cpython@bee9051 · GitHub
Skip to content

Commit bee9051

Browse files
authored
[3.11] gh-85267: Improvements to inspect.signature __text_signature__ handling (GH-98796) (#100392)
This makes a couple related changes to inspect.signature's behaviour when parsing a signature from `__text_signature__`. First, `inspect.signature` is documented as only raising ValueError or TypeError. However, in some cases, we could raise RuntimeError. This PR changes that, thereby fixing GH-83685. (Note that the new ValueErrors in RewriteSymbolics are caught and then reraised with a message) Second, `inspect.signature` could randomly drop parameters that it didn't understand (corresponding to `return None` in the `p` function). This is the core issue in GH-85267. I think this is very surprising behaviour and it seems better to fail outright. Third, adding this new failure broke a couple tests. To fix them (and to e.g. allow `inspect.signature(select.epoll.register)` as in GH-85267), I add constant folding of a couple binary operations to RewriteSymbolics. (There's some discussion of making signature expression evaluation arbitrary powerful in GH-68155. I think that's out of scope. The additional constant folding here is pretty straightforward, useful, and not much of a slippery slope) Fourth, while GH-85267 is incorrect about the cause of the issue, it turns out if you had consecutive newlines in __text_signature__, you'd get `tokenize.TokenError`. Finally, the `if name is invalid:` code path was dead, since `parse_name` never returned `invalid`.. (cherry picked from commit 79311cb) Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
1 parent fe828ec commit bee9051

3 files changed

Lines changed: 47 additions & 13 deletions

File tree

Lib/inspect.py

Lines changed: 21 additions & 12 deletions

Lib/test/test_inspect.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2480,7 +2480,7 @@ def p(name): return signature.parameters[name].default
24802480
self.assertEqual(p('f'), False)
24812481
self.assertEqual(p('local'), 3)
24822482
self.assertEqual(p('sys'), sys.maxsize)
2483-
self.assertNotIn('exp', signature.parameters)
2483+
self.assertEqual(p('exp'), sys.maxsize - 1)
24842484

24852485
test_callable(object)
24862486

@@ -4245,10 +4245,29 @@ def func(*args, **kwargs):
42454245
sig = inspect.signature(func)
42464246
self.assertIsNotNone(sig)
42474247
self.assertEqual(str(sig), '(self, /, a, b=1, *args, c, d=2, **kwargs)')
4248+
42484249
func.__text_signature__ = '($self, a, b=1, /, *args, c, d=2, **kwargs)'
42494250
sig = inspect.signature(func)
42504251
self.assertEqual(str(sig), '(self, a, b=1, /, *args, c, d=2, **kwargs)')
42514252

4253+
func.__text_signature__ = '(self, a=1+2, b=4-3, c=1 | 3 | 16)'
4254+
sig = inspect.signature(func)
4255+
self.assertEqual(str(sig), '(self, a=3, b=1, c=19)')
4256+
4257+
func.__text_signature__ = '(self, a=1,\nb=2,\n\n\n c=3)'
4258+
sig = inspect.signature(func)
4259+
self.assertEqual(str(sig), '(self, a=1, b=2, c=3)')
4260+
4261+
func.__text_signature__ = '(self, x=does_not_exist)'
4262+
with self.assertRaises(ValueError):
4263+
inspect.signature(func)
4264+
func.__text_signature__ = '(self, x=sys, y=inspect)'
4265+
with self.assertRaises(ValueError):
4266+
inspect.signature(func)
4267+
func.__text_signature__ = '(self, 123)'
4268+
with self.assertRaises(ValueError):
4269+
inspect.signature(func)
4270+
42524271
def test_base_class_have_text_signature(self):
42534272
# see issue 43118
42544273
from test.ann_module7 import BufferedReader
Lines changed: 6 additions & 0 deletions

0 commit comments

Comments
 (0)