Skip to content
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

PEP 484 type comments for vararg and kwarg? #1508

Open
tjprescott opened this issue Apr 12, 2022 · 3 comments
Open

PEP 484 type comments for vararg and kwarg? #1508

tjprescott opened this issue Apr 12, 2022 · 3 comments
Labels
Enhancement ✨ Improvement to a component Good first issue Friendly and approachable by new contributors

Comments

@tjprescott
Copy link

It seems like the Arguments class only has annotations for kwarg and vararg, and does not have any property for getting the type comment.

Per PEP484, this should be supported (https://peps.python.org/pep-0484/#suggested-syntax-for-python-2-7-and-straddling-code)

def embezzle(self, account, funds=1000000, *fake_receipts):
    # type: (str, int, *str) -> None
    """Embezzle funds from account using fake receipts."""
    <code goes here>

How would I obtain the type comment for *fake_receipts?

@AWhetter
Copy link
Contributor

AWhetter commented Apr 13, 2022

For what you want, you access it through type_comment_args.

>>> import astroid
>>> code = """def embezzle(self, account, funds=1000000, *fake_receipts):
...     # type: (str, int, *str) -> None
...     pass
... """
>>> func = astroid.extract_node(code)
>>> func.type_comment_args
[<Name.str l.1 at 0x7fcaf0084c40>, <Name.int l.1 at 0x7fcaf0084f40>, <Name.str l.1 at 0x7fcaf0084e50>]
>>> func.type_comment_args[2]
<Name.str l.1 at 0x7fcaf0084e50>

What isn't possible though is splitting up the arguments, type commenting each, and getting the annotation that way.

def embezzle(
     self,
     account,  # type: str
     funds=1000000,   # type: int
     *fake_receipts,  # type: str
 ):
     # type: (...) -> None
     pass

In that case there's no way to get the type comment. It looks like this was missing from the original representation. We should add both a type_comment_vararg: Optional[NodeNG] field and a type_comment_kwarg: Optional[NodeNG] field to the Arguments node.

@AWhetter AWhetter added the Good first issue Friendly and approachable by new contributors label Apr 13, 2022
@tjprescott
Copy link
Author

Thank you for the workaround! I agree that type_comment_vararg and type_commend_kwarg route would be the best (most consistent) path forward.

@tjprescott
Copy link
Author

tjprescott commented Apr 13, 2022

Also, it seems like this only works for *args and not **kwargs.

    def with_variadic_typehint(
        self,
        *vars, # type: str
        **kwargs # type: Any
    ):
        # type: (*str, **Any) -> None
        pass

[<AssignName.self l.2 at 0x1e658b71240>]

And **kwargs doesn't appear in the kwonlyargs either.

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

No branches or pull requests

2 participants