Skip to content

Expose multi-line function argument type annotation comments #665

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
AWhetter opened this issue Apr 22, 2019 · 1 comment · Fixed by #667
Closed

Expose multi-line function argument type annotation comments #665

AWhetter opened this issue Apr 22, 2019 · 1 comment · Fixed by #667
Labels
Enhancement ✨ Improvement to a component Good first issue Friendly and approachable by new contributors

Comments

@AWhetter
Copy link
Contributor

Feature Request

Say we are parsing the following function:

def sum(
        a,  # type: int
        b,  # type: int
):
    # type: (...) -> int
    return a + b

There is no way to access the type annotation comments for the individual a and b arguments, only the function itself.
typed_ast exposes these type comments through the attribute type_comment on each arg node:

>>> import typed_ast
>>> module = typed_ast.ast3.parse('''def sum(\n    a,  # type: int\n    b,  # type: int\n    ):\n    # type: (...) -> int\n    return a + b''')
>>> module.body[0].args.args
[<typed_ast._ast3.arg object at 0x7f318d7cd0b8>, <typed_ast._ast3.arg object at 0x7f318d7cd0f0>]
>>> module.body[0].args.args[0].type_comment
'int'

Desired behavior

We could add a type_annotation attribute to the AssignName node, but for regular assignments this is redundant information because type comments are exposed on the parent Assign node.
So the parent Arguments node seems like the right place for it to go. I'm proposing the addition of a type_comment_args attribute on the Arguments node that contains a list of the type comments, one for each argument using a None for arguments that are missing a comment.

python -c "from astroid import __pkginfo__; print(__pkginfo__.version)" output

2.2.5

@PCManticore
Copy link
Contributor

This sounds like a sensible suggestion, makes sense having it.

@PCManticore PCManticore added Good first issue Friendly and approachable by new contributors Enhancement ✨ Improvement to a component labels Apr 23, 2019
AWhetter added a commit that referenced this issue May 1, 2019
AWhetter added a commit that referenced this issue May 1, 2019
PCManticore pushed a commit that referenced this issue May 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement ✨ Improvement to a component Good first issue Friendly and approachable by new contributors
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants