Skip to content
This repository was archived by the owner on Jul 5, 2023. It is now read-only.

Commit 582aa47

Browse files
committed
Respond to review feedback
1 parent 4c8a2ec commit 582aa47

File tree

4 files changed

+41
-18
lines changed

4 files changed

+41
-18
lines changed

ast27/Python/ast.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,12 @@ ast_for_arguments(struct compiling *c, const node *n)
765765
defaults = (n_defaults ? asdl_seq_new(n_defaults, c->c_arena) : NULL);
766766
if (!defaults && n_defaults)
767767
return NULL;
768+
/* type_comments will be lazily initialized if needed. If there are no
769+
per-argument type comments, it will remain NULL. Otherwise, it will be
770+
an asdl_seq with length equal to the number of args (including varargs
771+
and kwargs, if present) and with members set to the string of each arg's
772+
type comment, if present, or NULL otherwise.
773+
*/
768774

769775
/* fpdef: NAME | '(' fplist ')'
770776
fplist: fpdef (',' fpdef)* [',']
@@ -839,7 +845,9 @@ ast_for_arguments(struct compiling *c, const node *n)
839845
asdl_seq_SET(args, k++, name);
840846

841847
}
842-
i += 1 + (TYPE(CHILD(n, i + 1)) == COMMA); /* the name and the comma, if present */
848+
i += 1; /* the name */
849+
if (TYPE(CHILD(n, i + 1)) == COMMA)
850+
i += 1; /* the comma, if present */
843851
if (parenthesized && Py_Py3kWarningFlag &&
844852
!ast_warn(c, ch, "parenthesized argument names "
845853
"are invalid in 3.x"))
@@ -853,15 +861,19 @@ ast_for_arguments(struct compiling *c, const node *n)
853861
vararg = NEW_IDENTIFIER(CHILD(n, i+1));
854862
if (!vararg)
855863
return NULL;
856-
i += 2 + (TYPE(CHILD(n, i + 2)) == COMMA);
864+
i += 2; /* the star and the name */
865+
if (TYPE(CHILD(n, i + 2)) == COMMA)
866+
i += 1; /* the comma, if present */
857867
break;
858868
case DOUBLESTAR:
859869
if (!forbidden_check(c, CHILD(n, i+1), STR(CHILD(n, i+1))))
860870
return NULL;
861871
kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
862872
if (!kwarg)
863873
return NULL;
864-
i += 2 + (TYPE(CHILD(n, i + 2)) == COMMA);
874+
i += 2; /* the double star and the name */
875+
if (TYPE(CHILD(n, i + 2)) == COMMA)
876+
i += 1; /* the comma, if present */
865877
break;
866878
case TYPE_COMMENT:
867879
assert(l < k + !!vararg + !!kwarg);

ast35/Python/ast.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,16 +1320,18 @@ handle_keywordonly_args(struct compiling *c, const node *n, int start,
13201320
if (!arg)
13211321
goto error;
13221322
asdl_seq_SET(kwonlyargs, j++, arg);
1323-
i += 1 + (TYPE(CHILD(n, i + 1)) == COMMA); /* the name and the comma, if present */
1323+
i += 1; /* the name */
1324+
if (TYPE(CHILD(n, i + 1)) == COMMA)
1325+
i += 1; /* the comma, if present */
13241326
break;
1325-
case DOUBLESTAR:
1326-
return i;
13271327
case TYPE_COMMENT:
13281328
/* arg will be equal to the last argument processed */
13291329
if (!set_arg_comment_annotation(c, arg, ch))
13301330
return -1;
13311331
i += 1;
13321332
break;
1333+
case DOUBLESTAR:
1334+
return i;
13331335
default:
13341336
ast_error(c, ch, "unexpected node");
13351337
goto error;
@@ -1457,10 +1459,12 @@ ast_for_arguments(struct compiling *c, const node *n)
14571459
if (!arg)
14581460
return NULL;
14591461
asdl_seq_SET(posargs, k++, arg);
1460-
i += 1 + (TYPE(CHILD(n, i + 1)) == COMMA); /* the name and the comma, if present */
1462+
i += 1; /* the name */
1463+
if (TYPE(CHILD(n, i + 1)) == COMMA)
1464+
i += 1; /* the comma, if present */
14611465
break;
14621466
case STAR:
1463-
if (i+1 >= NCH(n)) {
1467+
if (i+1 >= NCH(n) || TYPE(CHILD(n, i+1)) == TYPE_COMMENT) {
14641468
ast_error(c, CHILD(n, i),
14651469
"named arguments must follow bare *");
14661470
return NULL;
@@ -1486,7 +1490,9 @@ ast_for_arguments(struct compiling *c, const node *n)
14861490
if (!vararg)
14871491
return NULL;
14881492

1489-
i += 2 + (TYPE(CHILD(n, i + 2)) == COMMA);
1493+
i += 2; /* the star and the name */
1494+
if (TYPE(CHILD(n, i + 2)) == COMMA)
1495+
i += 1; /* the comma, if present */
14901496

14911497
if (TYPE(CHILD(n, i)) == TYPE_COMMENT) {
14921498
if (!set_arg_comment_annotation(c, vararg, CHILD(n, i)))
@@ -1511,7 +1517,9 @@ ast_for_arguments(struct compiling *c, const node *n)
15111517
kwarg = ast_for_arg(c, ch);
15121518
if (!kwarg)
15131519
return NULL;
1514-
i += 2 + (TYPE(CHILD(n, i + 2)) == COMMA);
1520+
i += 2; /* the double star and the name */
1521+
if (TYPE(CHILD(n, i + 2)) == COMMA)
1522+
i += 1; /* the comma, if present */
15151523
break;
15161524
case TYPE_COMMENT:
15171525
assert(i);

typed_ast/ast35.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@
1010
that `ast35` provides PEP 484 type comment information as part of the
1111
AST.
1212
13-
In particular, the `FunctionDef`, `Assign`, `For`, and `With` classes all
14-
have a `type_comment` field which contains a `str` with the text of the
15-
type comment. Per-argument function comments are put into the annotation
16-
field of each argument. `parse` has been augmented so it can parse
17-
function signature types when called with `mode=func_type`. Finally,
18-
`Module` has a `type_ignores` field which contains a list of lines which
19-
have been `# type: ignored`.
13+
In particular:
14+
- The `FunctionDef`, `Assign`, `For`, and `With` classes all have a
15+
`type_comment` field which contains a `str` with the text of the type
16+
comment.
17+
- Per-argument function comments are put into the annotation field of each
18+
argument.
19+
- `parse` has been augmented so it can parse function signature types when
20+
called with `mode=func_type`.
21+
- `Module` has a `type_ignores` field which contains a list of
22+
lines which have been `# type: ignored`.
2023
2124
An abstract syntax tree can be generated by using the `parse()`
2225
function from this module. The result will be a tree of objects whose

typed_ast/conversions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def get_type_comment(i):
196196
kwarg = None
197197
if n.kwarg is not None:
198198
kwarg = ast35.arg(n.kwarg,
199-
get_type_comment(len(args) + 1 + (1 if n.vararg is not None else 0)),
199+
get_type_comment(len(args) + 1 + (0 if n.vararg is None else 1)),
200200
lineno=-1, col_offset=-1)
201201

202202
defaults = self.visit(n.defaults)

0 commit comments

Comments
 (0)