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

ast.Assign need type_comment in Python3.8 #49

Closed
Aurelius84 opened this issue Sep 4, 2020 · 3 comments · Fixed by #50
Closed

ast.Assign need type_comment in Python3.8 #49

Aurelius84 opened this issue Sep 4, 2020 · 3 comments · Fixed by #50

Comments

@Aurelius84
Copy link

Aurelius84 commented Sep 4, 2020

In python3.8.5,I use gast to modify ast_node then convert back into ast bygast_to_ast. But the result is different with original ast.

It works in Python3.5 and Python2.7

The example code:

import ast
import gast
import textwrap
import unittest

def code_gast_ast(source):
    """
    Transform source_code into gast.Node and modify it,
    then back to ast.Node.
    """
    source = textwrap.dedent(source)
    root = gast.parse(source)
    new_root = GastNodeTransformer(root).apply()
    ast_root = gast.gast_to_ast(new_root)
    return ast.dump(ast_root)


def code_ast(source):
    """
    Transform source_code into ast.Node, then dump it.
    """
    source = textwrap.dedent(source)
    root = ast.parse(source)
    return ast.dump(root)


class GastNodeTransformer(gast.NodeTransformer):
    def __init__(self, root):
        self.root = root

    def apply(self):
        return self.generic_visit(self.root)

    def visit_Name(self, node):
        """
        Param in func is ast.Name in PY2, but ast.arg in PY3.
        It will be generally represented by gast.Name in gast.
        """
        if isinstance(node.ctx, gast.Param) and node.id != "self":
            node.id += '_new'

        return node

class TestPythonCompatibility(unittest.TestCase):
    def _check_compatibility(self, source, target):
        source_dump = code_gast_ast(source)
        target_dump = code_ast(target)
        self.assertEqual(source_dump, target_dump)

    def test_call(self):
        source = """
            y = foo(*arg)
        """
        target = """
            y = foo(*arg_new)
        """
        self._check_compatibility(source, target)

# source_dump  gast-> ast
# Module(body=[Assign(targets=[Name(id='y', ctx=Store())], value=Call(func=Name(id='foo', ctx=Load()), args=[Starred(value=Name(id='arg_new', ctx=Load()), ctx=Load())], keywords=[]))], type_ignores=[])

# target_dump ast
# Module(body=[Assign(targets=[Name(id='y', ctx=Store())], value=Call(func=Name(id='foo', ctx=Load()), args=[Starred(value=Name(id='arg_new', ctx=Load()), ctx=Load())], keywords=[]), type_comment=None)], type_ignores=[])

After I modified the defination in gast.py, it works in python3.8

from

('Assign', (('targets', 'value',),
                ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
                (stmt,))),

into

('Assign', (('targets', 'value','type_comment'),
                ('lineno', 'col_offset', 'end_lineno', 'end_col_offset',),
                (stmt,))),
@Aurelius84 Aurelius84 changed the title ast.Assign with type_comment in Python3.8 ast.Assign need type_comment in Python3.8 Sep 4, 2020
serge-sans-paille added a commit that referenced this issue Sep 5, 2020
Do not forget type_comment field!

Fix #49
@serge-sans-paille
Copy link
Owner

Correct! Thanks for spotting this.

serge-sans-paille added a commit that referenced this issue Sep 5, 2020
Do not forget type_comment field!

Fix #49
@Aurelius84
Copy link
Author

Great! Which version will this feature will be cherry picked into? I'm using gast==0.3.3 currently in my repository. But whether it has risk to update from 0.3.3 into 0.4.0

@serge-sans-paille
Copy link
Owner

It'll go in 0.4.1. there's some difference between 0.3.0 and 0.4.x due to python 3.9 support.

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

Successfully merging a pull request may close this issue.

2 participants