Whole file

ilevkivskyi/com2ann

The author described this change as Fix keyword-only arguments (#23). It counts as a record because the check below fails on the code as it stood at 7b537d51f and passes on 176c43e00, with nothing else changed between the two runs.

Fix saved2019-06-12
Sharing licenceMIT · LICENSE
Change size+8 10

What the code was meant to do, written into the code itself as a save note

Fix keyword-only arguments (#23)

The change

198198 # Number of non-default positional arguments.
199199 num_non_defs = len(fdef.args.args) - len(fdef.args.defaults)
200200
201- # Number of non-default keyword-only arguments.
202- num_kw_non_defs = (len(fdef.args.kwonlyargs) -
203- len([d for d in fdef.args.kw_defaults if d is not None]))
201+ # Positions of non-default keyword-only arguments.
202+ kw_non_defs = {i for i, d in enumerate(fdef.args.kw_defaults) if d is None}
204203
205- args = self.process_per_arg_comments(fdef, num_non_defs, num_kw_non_defs)
204+ args = self.process_per_arg_comments(fdef, num_non_defs, kw_non_defs)
206205
207206 ret: Optional[str]
208207 if fdef.type_comment:
235234 self.found.append(FunctionData([], ret, fdef.lineno, body_start))
236235 else:
237236 c_args = self.process_function_comment(fdef, f_args,
238- num_non_defs, num_kw_non_defs)
237+ num_non_defs)
239238 if c_args is None:
240239 # There was an error processing comment.
241240 return
244243
245244 def process_per_arg_comments(self, fdef: Function,
246245 num_non_defs: int,
247- num_kw_non_defs: int) -> List[ArgComment]:
246+ kw_non_defs: Set[int]) -> List[ArgComment]:
248247 """Collect information about per-argument function comments.
249248
250249 These comments look like:
275274 assert a.end_col_offset
276275 args.append(ArgComment(a.type_comment,
277276 a.lineno, a.end_col_offset,
278- i >= num_kw_non_defs))
277+ i not in kw_non_defs))
279278 if fdef.args.kwarg and fdef.args.kwarg.type_comment:
280279 kwarg = fdef.args.kwarg
281280 assert kwarg.end_col_offset
286285
287286 def process_function_comment(self, fdef: Function,
288287 f_args: List[str],
289- num_non_defs: int,
290- num_kw_non_defs: int) -> Optional[List[ArgComment]]:
288+ num_non_defs: int) -> Optional[List[ArgComment]]:
291289 """Combine location data for function arguments with types from a comment.
292290
293291 f_args contains already split argument strings from the function type comment,
330328 has_default = True
331329
332330 kwonlyargs = fdef.args.kwonlyargs
333- if a in kwonlyargs and kwonlyargs.index(a) >= num_kw_non_defs:
331+ if a in kwonlyargs and fdef.args.kw_defaults[kwonlyargs.index(a)]:
334332 has_default = True
335333
336334 assert a.end_col_offset

The check that tells the two apart

failpass·src/test_com2ann.py::FunctionTestCase::test_keyword_only_args

Check file src/test_com2ann.py, taken without changes from the fix and copied onto the older code, so the exact same check runs against both versions.

Origin and history

The code before it7b537d51f1adab0ab09eb8766a25c4c5e3819f4e
Broken version dated2019-06-12
Modulecom2ann
Units changedTypeCommentCollector
Fingerprintea1db7948eaa9b80
Checked2026-08-18 by goldset/0.1

Every field above is generated by our program. None of it is written by hand.

Other bugs found in ilevkivskyi/com2ann