Skip to content

It does not preserve per-argument type comments #2

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
mitar opened this issue Sep 25, 2017 · 9 comments
Closed

It does not preserve per-argument type comments #2

mitar opened this issue Sep 25, 2017 · 9 comments

Comments

@mitar
Copy link

mitar commented Sep 25, 2017

This strips per-argument type comments (example taken from PEP 484):

code = '''
def send_email(address,     # type: Union[str, List[str]]
               sender,      # type: str
               cc,          # type: Optional[List[str]]
               bcc,         # type: Optional[List[str]]
               subject='',
               body=None    # type: List[str]
               ):
    # type: (...) -> bool
    """Send an email message.  Return True if successful."""
    pass
'''

import typed_ast.ast3
import typed_astunparse

print(typed_astunparse.unparse(typed_ast.ast3.parse(code)))

Output is:

def send_email(address, sender, cc, bcc, subject='', body=None):
    # type: (...) -> bool
    'Send an email message.  Return True if successful.'
    pass
@mbdevpl
Copy link
Owner

mbdevpl commented Sep 25, 2017

Thanks for reporting, will look into that!

@mitar
Copy link
Author

mitar commented Sep 25, 2017

(Also see that the docstring does not use """.)

@mbdevpl
Copy link
Owner

mbdevpl commented Sep 26, 2017

I've verified that the per-argument type comments are indeed not working. I'll fix it.

However, the docstring issue is out of scope for this unparser. There was a similar issue upstream (astunparse): simonpercivall/astunparse#13 however it was closed.

I'm working towards implementing proper docstring unparsing, (as well as arbitrary comment parsing and unparsing) in a different module: https://github.com/mbdevpl/horast however it's not nearly ready yet.

@mbdevpl
Copy link
Owner

mbdevpl commented Sep 26, 2017

And, there seems to be an issue in typed_ast with parsing per-argument type comments in all cases: python/typed_ast#58 so even if I generate valid code, in some cases I actually cannot verify its validity.

@mitar
Copy link
Author

mitar commented Sep 26, 2017

Thanks for looking into this.

@mitar
Copy link
Author

mitar commented Sep 26, 2017

(BTW, I used your project here: nvbn/py-backwards#44)

@mbdevpl
Copy link
Owner

mbdevpl commented Sep 26, 2017

Not minding the issue with typed_ast (hopefully it'll get resolved someday) I added the per-argument type comment unparsing and released https://pypi.python.org/pypi/typed-astunparse/2.0.2

@mbdevpl mbdevpl closed this as completed Sep 26, 2017
@mitar
Copy link
Author

mitar commented Sep 26, 2017

Oho! Thanks!

@mbdevpl
Copy link
Owner

mbdevpl commented Sep 26, 2017

Oh, and BTW, I just found out that docstrings might be properly stored in AST starting from Python 3.7 - if they will be, then I guess astunparse and typed_astunparse will handle them properly. https://bugs.python.org/issue29463

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants