8000 gh-102378: don't bother stripping `/` from __text_signature__ (#102379) · python/cpython@71cf7c3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 71cf7c3

Browse files
authored
gh-102378: don't bother stripping / from __text_signature__ (#102379)
1 parent c6858d1 commit 71cf7c3

File tree

3 files changed

+25
-56
lines changed

3 files changed

+25
-56
lines changed

Lib/inspect.py

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2106,26 +2106,21 @@ def _signature_strip_non_python_syntax(signature):
21062106
Private helper function. Takes a signature in Argument Clinic's
21072107
extended signature format.
21082108
2109-
Returns a tuple of three things:
2110-
* that signature re-rendered in standard Python syntax,
2109+
Returns a tuple of two things:
2110+
* that signature re-rendered in standard Python syntax, and
21112111
* the index of the "self" parameter (generally 0), or None if
2112-
the function does not have a "self" parameter, and
2113-
* the index of the last "positional only" parameter,
2114-
or None if the signature has no positional-only parameters.
2112+
the function does not have a "self" parameter.
21152113
"""
21162114

21172115
if not signature:
2118-
return signature, None, None
2116+
return signature, None
21192117

21202118
self_parameter = None
2121-
last_positional_only = None
21222119

21232120
lines = [l.encode('ascii') for l in signature.split('\n') if l]
21242121
generator = iter(lines).__next__
21252122
token_stream = tokenize.tokenize(generator)
21262123

2127-
delayed_comma = False
2128-
skip_next_comma = False
21292124
text = []
21302125
add = text.append
21312126

@@ -2142,35 +2137,18 @@ def _signature_strip_non_python_syntax(signature):
21422137

21432138
if type == OP:
21442139
if string == ',':
2145-
if skip_next_comma:
2146-
skip_next_comma = False
2147-
else:
2148-
assert not delayed_comma
2149-
delayed_comma = True
2150-
current_parameter += 1
2151-
continue
2152-
2153-
if string == '/':
2154-
assert not skip_next_comma
2155-
assert last_positional_only is None
2156-
skip_next_comma = True
2157-
last_positional_only = current_parameter - 1
2158-
continue
2140+
current_parameter += 1
21592141

21602142
if (type == ERRORTOKEN) and (string == '$'):
21612143
assert self_parameter is None
21622144
self_parameter = current_parameter
21632145
continue
21642146

2165-
if delayed_comma:
2166-
delayed_comma = False
2167-
if not ((type == OP) and (string == ')')):
2168-
add(', ')
21692147
add(string)
21702148
if (string == ','):
21712149
add(' ')
21722150
clean_signature = ''.join(text)
2173-
return clean_signature, self_parameter, last_positional_only
2151+
return clean_signature, self_parameter
21742152

21752153

21762154
def _signature_fromstr(cls, obj, s, skip_bound_arg=True):
@@ -2179,8 +2157,7 @@ def _signature_fromstr(cls, obj, s, skip_bound_arg=True):
21792157
"""
21802158
Parameter = cls._parameter_cls
21812159

2182-
clean_signature, self_parameter, last_positional_only = \
2183-
_signature_strip_non_python_syntax(s)
2160+
clean_signature, self_parameter = _signature_strip_non_python_syntax(s)
21842161

21852162
program = "def foo" + clean_signature + ": pass"
21862163

@@ -2269,17 +2246,17 @@ def p(name_node, default_node, default=empty):
22692246
parameters.append(Parameter(name, kind, default=default, annotation=empty))
22702247

22712248
# non-keyword-only parameters
2272-
args = reversed(f.args.args)
2273-
defaults = reversed(f.args.defaults)
2274-
iter = itertools.zip_longest(args, defaults, fillvalue=None)
2275-
if last_positional_only is not None:
2276-
kind = Parameter.POSITIONAL_ONLY
2277-
else:
2278-
kind = Parameter.POSITIONAL_OR_KEYWORD
2279-
for i, (name, default) in enumerate(reversed(list(iter))):
2249+
total_non_kw_args = len(f.args.posonlyargs) + len(f.args.args)
2250+
required_non_kw_args = total_non_kw_args - len(f.args.defaults)
2251+
defaults = itertools.chain(itertools.repeat(None, required_non_kw_args), f.args.defaults)
2252+
2253+
kind = Parameter.POSITIONAL_ONLY
2254+
for (name, default) in zip(f.args.posonlyargs, defaults):
2255+
p(name, default)
2256+
2257+
kind = Parameter.POSITIONAL_OR_KEYWORD
2258+
for (name, default) in zip(f.args.args, defaults):
22802259
p(name, default)
2281-
if i == last_positional_only:
2282-
kind = Parameter.POSITIONAL_OR_KEYWORD
22832260

22842261
# *args
22852262
if f.args.vararg:

Lib/test/test_inspect.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4230,56 +4230,47 @@ def foo(a): pass
42304230

42314231
class TestSignaturePrivateHelpers(unittest.TestCase):
42324232
def _strip_non_python_syntax(self, input,
4233-
clean_signature, self_parameter, last_positional_only):
4233+
clean_signature, self_parameter):
42344234
computed_clean_signature, \
4235-
computed_self_parameter, \
4236-
computed_last_positional_only = \
4235+
computed_self_parameter = \
42374236
inspect._signature_strip_non_python_syntax(input)
42384237
self.assertEqual(computed_clean_signature, clean_signature)
42394238
self.assertEqual(computed_self_parameter, self_parameter)
4240-
self.assertEqual(computed_last_positional_only, last_positional_only)
42414239

42424240
def test_signature_strip_non_python_syntax(self):
42434241
self._strip_non_python_syntax(
42444242
"($module, /, path, mode, *, dir_fd=None, " +
42454243
"effective_ids=False,\n follow_symlinks=True)",
4246-
"(module, path, mode, *, dir_fd=None, " +
4244+
"(module, /, path, mode, *, dir_fd=None, " +
42474245
"effective_ids=False, follow_symlinks=True)",
4248-
0,
42494246
0)
42504247

42514248
self._strip_non_python_syntax(
42524249
"($module, word, salt, /)",
4253-
"(module, word, salt)",
4254-
0,
4255-
2)
4250+
"(module, word, salt, /)",
4251+
0)
42564252

42574253
self._strip_non_python_syntax(
42584254
"(x, y=None, z=None, /)",
4259-
"(x, y=None, z=None)",
4260-
None,
4261-
2)
4255+
"(x, y=None, z=None, /)",
4256+
None)
42624257

42634258
self._strip_non_python_syntax(
42644259
"(x, y=None, z=None)",
42654260
"(x, y=None, z=None)",
4266-
None,
42674261
None)
42684262

42694263
self._strip_non_python_syntax(
42704264
"(x,\n y=None,\n z = None )",
42714265
"(x, y=None, z=None)",
4272-
None,
42734266
None)
42744267

42754268
self._strip_non_python_syntax(
42764269
"",
42774270
"",
4278-
None,
42794271
None)
42804272

42814273
self._strip_non_python_syntax(
4282-
None,
42834274
None,
42844275
None,
42854276
None)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Private helper method ``inspect._signature_strip_non_python_syntax`` will no longer strip ``/`` from the input string.

0 commit comments

Comments
 (0)
0