@@ -2106,26 +2106,21 @@ def _signature_strip_non_python_syntax(signature):
2106
2106
Private helper function. Takes a signature in Argument Clinic's
2107
2107
extended signature format.
2108
2108
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
2111
2111
* 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.
2115
2113
"""
2116
2114
2117
2115
if not signature :
2118
- return signature , None , None
2116
+ return signature , None
2119
2117
2120
2118
self_parameter = None
2121
- last_positional_only = None
2122
2119
2123
2120
lines = [l .encode ('ascii' ) for l in signature .split ('\n ' ) if l ]
2124
2121
generator = iter (lines ).__next__
2125
2122
token_stream = tokenize .tokenize (generator )
2126
2123
2127
- delayed_comma = False
2128
- skip_next_comma = False
2129
2124
text = []
2130
2125
add = text .append
2131
2126
@@ -2142,35 +2137,18 @@ def _signature_strip_non_python_syntax(signature):
2142
2137
2143
2138
if type == OP :
2144
2139
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
2159
2141
2160
2142
if (type == ERRORTOKEN ) and (string == '$' ):
2161
2143
assert self_parameter is None
2162
2144
self_parameter = current_parameter
2163
2145
continue
2164
2146
2165
- if delayed_comma :
2166
- delayed_comma = False
2167
- if not ((type == OP ) and (string == ')' )):
2168
- add (', ' )
2169
2147
add (string )
2170
2148
if (string == ',' ):
2171
2149
add (' ' )
2172
2150
clean_signature = '' .join (text )
2173
- return clean_signature , self_parameter , last_positional_only
2151
+ return clean_signature , self_parameter
2174
2152
2175
2153
2176
2154
def _signature_fromstr (cls , obj , s , skip_bound_arg = True ):
@@ -2179,8 +2157,7 @@ def _signature_fromstr(cls, obj, s, skip_bound_arg=True):
2179
2157
"""
2180
2158
Parameter = cls ._parameter_cls
2181
2159
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 )
2184
2161
2185
2162
program = "def foo" + clean_signature + ": pass"
2186
2163
@@ -2269,17 +2246,17 @@ def p(name_node, default_node, default=empty):
2269
2246
parameters .append (Parameter (name , kind , default = default , annotation = empty ))
2270
2247
2271
2248
# 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 ):
2280
2259
p (name , default )
2281
- if i == last_positional_only :
2282
- kind = Parameter .POSITIONAL_OR_KEYWORD
2283
2260
2284
2261
# *args
2285
2262
if f .args .vararg :
0 commit comments