Coder Social home page Coder Social logo

mbdevpl / typed-astunparse Goto Github PK

View Code? Open in Web Editor NEW
29.0 4.0 4.0 157 KB

Python 3 AST unparser with type comments support.

License: Apache License 2.0

Python 91.67% Jupyter Notebook 7.34% Shell 0.99%
code-generation code-generator abstact-syntax-tree type-annotations

typed-astunparse's Issues

Neither newlines nor triple quotes preserved… for docstrings

import typed_ast.ast3
import typed_astunparse

code = '''
class Snake(object):
    """
    Om nom nom
    
    and some more nom nom nom
    """
    
    def bite(me: str) -> bool:
        return me == 'cython'
'''
roundtrip = typed_astunparse.unparse(typed_ast.ast3.parse(code))
print(roundtrip)

Outputs:

class Snake(object):
    '\n    Om nom nom\n    \n    and some more nom nom nom\n    '

    def bite(me: str) -> bool:
        return (me == 'cython')

Use-case: I'm writing a docstring transformer

Syntax Error on Python 3.7

I am trying to run this on python 3.7.0b3 and am getting a SyntaxError when I import it. Is async a reserved keyword now?

$ python3.7
Python 3.7.0b3 (v3.7.0b3:4e7efa9c6f, Mar 29 2018, 08:11:44)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import typed_ast.ast3
>>> import typed_astunparse
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/typed_astunparse/__init__.py", line 12, in <module>
    from .unparser import Unparser
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/typed_astunparse/unparser.py", line 248
    def _generic_FunctionDef(self, t, async=False):
                                          ^
SyntaxError: invalid syntax
>>>

It does not preserve per-argument type comments

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

unparsing of complex and/or nested f-strings fails sometimes

It fails due to the fact that sub-strings and values in the expressions in f-strings are unparsed without knowledge what string delimiters should be used, and therefore running

import typed_ast.ast3
import typed_astunparse
print(typed_astunparse.unparse(typed_ast.ast3.parse(
    """f'length of "hello" is {len("hello")}'""", mode='eval')))

prints

f'length of "hello" is {len(\'hello\')}'

but parsing it again

typed_ast.ast3.parse("""f'length of "hello" is {len(\\'hello\\')}'""", mode='eval')

results in an error.

Traceback (most recent call last):

  File "/home/mateusz/.local/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2910, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)

  File "<ipython-input-12-b854d28c44ec>", line 1, in <module>
    typed_ast.ast3.parse("""f'length of "hello" is {len(\\'hello\\')}'""", mode='eval')

  File "/home/mateusz/.local/lib/python3.6/site-packages/typed_ast/ast3.py", line 58, in parse
    return _ast3._parse(source, filename, mode, feature_version)

  File "<unknown>", line 1
SyntaxError: f-string expression part cannot include a backslash

This would be fixed by implementing an f-string unparser that is aware which string delimiters should be used to generate code for each sub-string and expression within an f-string.

per-argument type comments in function definitions are unparsed incorrectly

The way those type comments are unparsed goes against rules of PEP484. This leads to errors when unparsed files are parsed again by typed_ast - no errors with ast.

def function_b8(spam=0,  # type: int
                ham='',  # type: str
                eggs=0.0):  # type: float
    """function with args with type comments"""
    spam, ham, eggs = None, None, None

After parsing and unparsing the above, it looks like:

def function_b8(spam  # type: int
        =0, ham  # type: str
        ='', eggs=0.0):
    # type: float
    'function with args with type comments'
    (spam, ham, eggs) = (None, None, None)

Which is valid code and runs, but it definitely is not a correct unparsing! And moreover, it looks ugly.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.