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.
Projectilevkivskyi/com2ann
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
| 198 | 198 | # Number of non-default positional arguments. | |
| 199 | 199 | num_non_defs = len(fdef.args.args) - len(fdef.args.defaults) | |
| 200 | 200 | ||
| 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} | |
| 204 | 203 | ||
| 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) | |
| 206 | 205 | ||
| 207 | 206 | ret: Optional[str] | |
| 208 | 207 | if fdef.type_comment: | |
| ⋯ | |||
| 235 | 234 | self.found.append(FunctionData([], ret, fdef.lineno, body_start)) | |
| 236 | 235 | else: | |
| 237 | 236 | c_args = self.process_function_comment(fdef, f_args, | |
| 238 | - | num_non_defs, num_kw_non_defs) | |
| 237 | + | num_non_defs) | |
| 239 | 238 | if c_args is None: | |
| 240 | 239 | # There was an error processing comment. | |
| 241 | 240 | return | |
| ⋯ | |||
| 244 | 243 | ||
| 245 | 244 | def process_per_arg_comments(self, fdef: Function, | |
| 246 | 245 | num_non_defs: int, | |
| 247 | - | num_kw_non_defs: int) -> List[ArgComment]: | |
| 246 | + | kw_non_defs: Set[int]) -> List[ArgComment]: | |
| 248 | 247 | """Collect information about per-argument function comments. | |
| 249 | 248 | ||
| 250 | 249 | These comments look like: | |
| ⋯ | |||
| 275 | 274 | assert a.end_col_offset | |
| 276 | 275 | args.append(ArgComment(a.type_comment, | |
| 277 | 276 | a.lineno, a.end_col_offset, | |
| 278 | - | i >= num_kw_non_defs)) | |
| 277 | + | i not in kw_non_defs)) | |
| 279 | 278 | if fdef.args.kwarg and fdef.args.kwarg.type_comment: | |
| 280 | 279 | kwarg = fdef.args.kwarg | |
| 281 | 280 | assert kwarg.end_col_offset | |
| ⋯ | |||
| 286 | 285 | ||
| 287 | 286 | def process_function_comment(self, fdef: Function, | |
| 288 | 287 | 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]]: | |
| 291 | 289 | """Combine location data for function arguments with types from a comment. | |
| 292 | 290 | ||
| 293 | 291 | f_args contains already split argument strings from the function type comment, | |
| ⋯ | |||
| 330 | 328 | has_default = True | |
| 331 | 329 | ||
| 332 | 330 | 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)]: | |
| 334 | 332 | has_default = True | |
| 335 | 333 | ||
| 336 | 334 | assert a.end_col_offset | |
The check that tells the two apart
fail→pass·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
- 2025-06-02split_function_comment
- 2021-03-07process_assign
- 2019-06-12process_func_def
- 2019-06-12Fix signature wrapping when return type contains commas (#21)
- 2019-06-11Fix crash when type comment appears on continuation line (#17)