-
-
Notifications
You must be signed in to change notification settings - Fork 54
[2.7, 3.5] Support the per-argument function comment syntax #5
Conversation
Adds a type_comments list of strings to the arguments object containing the per-argument type comments of a function, if any.
Puts per-argument type comments into the annotation slot of the argument as a string expression.
@gnprice take a look when you have time! |
Looking now! Eager to see this in. :-) |
a `type_ignores` field which contains a list of lines which have been | ||
`# type: ignored`. | ||
type comment. Per-argument function comments are put into the annotation | ||
field of each argument. `parse` has been augmented so it can parse |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This paragraph is probably best turned into bullets at this point -- the four sentences are fairly independent of each other, and the transition from "per-argument function comments" to "parse
has been augmented" is particularly abrupt.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. Changed.
Done reading. Looks good! I have a number of comments above, but I think they are all small, and I think the only one that affects the behavior is about a syntax-error case. |
Python 2 per-argument type comments are now converted into Python 3 annotations.
|
||
args = [convert_arg(arg) for arg in n.args] | ||
def get_type_comment(i): | ||
if i < len(n.type_comments): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Huh, will this work if there are no type comments -- do we end up with an empty sequence for n.type_comments
, or None
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's an empty sequence.
Done reading the new commit too. |
582aa47
to
314d17d
Compare
Pushed new commit with feedback. |
- `parse` has been augmented so it can parse function signature types when | ||
called with `mode=func_type`. | ||
- `Module` has a `type_ignores` field which contains a list of | ||
lines which have been `# type: ignored`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, it looks like the "d" is on the wrong side of these backticks. A pre-existing nit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ooh, let me fix that.
Finished reading through the new version -- just a couple of comments which are all on documentation. (I wish I knew a reasonable way to get GitHub to tell me what the recent comments were.) |
-- type_comments is used to support the per-argument type comment syntax. | ||
-- It is either an empty list or a list with length equal to the number of | ||
-- args (including varargs and kwargs, if present) and with members set to the | ||
-- string of each arg's type comment, if present, or None otherwise. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this aligned funny, or is GitHub displaying it in a surprising way?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The file uses tabs. Fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool. :)
Thanks for the detailed review! |
PEP clarifications here: python/typing#237 |
This adds support for the (Python 2) per-argument type comment syntax now that python/typed_ast#5 has landed. It only works when using `--fast-parser`; without that these comments will be ignore. The code works by converting Python 2.7 per-argument type comments to Python 3 annotations in the typed_ast's conversion module -- this allows them to be mixed with the # type: (...) -> None syntax. Fixes #1102.
For Python 2.7:
Adds a type_comments list of strings to the arguments object containing
the per-argument type comments of a function, if any.
For Python 3.5:
Puts per-argument type comments into the annotation slot of the
argument as a string expression.
This change contains some autogenerated files:
Python/graminit.c
is autogenerated fromGrammar/Grammar
Include/Python-ast.h
andPython/Python-ast.c
are autogenerated fromParser/Python.asdl