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.NodeTransformer doc bug #47780

Closed
daishiharada mannequin opened this issue Aug 9, 2008 · 7 comments
Closed

ast.NodeTransformer doc bug #47780

daishiharada mannequin opened this issue Aug 9, 2008 · 7 comments
Labels
3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes docs Documentation in the Doc dir stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@daishiharada
Copy link
Mannequin

daishiharada mannequin commented Aug 9, 2008

BPO 3530
Nosy @birkenfeld, @terryjreedy, @benjaminp, @mitsuhiko, @pablogsal, @miss-islington
PRs
  • bpo-3530: Use fix_missing_locations when node transformer adds nodes #17172
  • [3.8] bpo-3530: Add advice on when to correctly use fix_missing_locations in the AST docs (GH-17172) #17972
  • [3.7] bpo-3530: Add advice on when to correctly use fix_missing_locations in the AST docs (GH-17172) #17973
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2020-01-12.20:40:00.310>
    created_at = <Date 2008-08-09.02:26:13.514>
    labels = ['type-bug', '3.8', '3.9', '3.7', 'library', 'docs']
    title = 'ast.NodeTransformer doc bug'
    updated_at = <Date 2020-01-12.20:44:37.216>
    user = 'https://bugs.python.org/daishiharada'

    bugs.python.org fields:

    activity = <Date 2020-01-12.20:44:37.216>
    actor = 'miss-islington'
    assignee = 'docs@python'
    closed = True
    closed_date = <Date 2020-01-12.20:40:00.310>
    closer = 'pablogsal'
    components = ['Documentation', 'Library (Lib)']
    creation = <Date 2008-08-09.02:26:13.514>
    creator = 'daishiharada'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 3530
    keywords = ['patch']
    message_count = 7.0
    messages = ['70923', '71209', '245968', '356786', '359872', '359874', '359875']
    nosy_count = 8.0
    nosy_names = ['georg.brandl', 'terry.reedy', 'daishiharada', 'benjamin.peterson', 'aronacher', 'docs@python', 'pablogsal', 'miss-islington']
    pr_nums = ['17172', '17972', '17973']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue3530'
    versions = ['Python 2.7', 'Python 3.7', 'Python 3.8', 'Python 3.9']

    @daishiharada
    Copy link
    Mannequin Author

    daishiharada mannequin commented Aug 9, 2008

    I am testing python 2.6 from SVN version: 40110

    I tried the following, based on the documentation
    and example in the ast module. I would expect the
    second 'compile' to succeed also, instead of
    throwing an exception.

    Python 2.6b2+ (unknown, Aug  6 2008, 18:05:08) 
    [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import ast
    >>> a = ast.parse('foo', mode='eval')
    >>> x = compile(a, '<unknown>', mode='eval')
    >>> class RewriteName(ast.NodeTransformer):
    ...     def visit_Name(self, node):
    ...         return ast.copy_location(ast.Subscript(
    ...             value=ast.Name(id='data', ctx=ast.Load()),
    ...             slice=ast.Index(value=ast.Str(s=node.id)),
    ...             ctx=node.ctx
    ...         ), node)
    ... 
    >>> a2 = RewriteName().visit(a)
    >>> x2 = compile(a2, '<unknown>', mode='eval')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: required field "lineno" missing from expr
    >>>

    @daishiharada daishiharada mannequin added the stdlib Python modules in the Lib dir label Aug 9, 2008
    @mitsuhiko
    Copy link
    Member

    This is actually not a bug. copy_location does not work recursively.
    For this example it's more useful to use the "fix_missing_locations"
    function which traverses the tree and copies the locations from the
    parent node to the child nodes:

    import ast
    a = ast.parse('foo', mode='eval')
    x = compile(a, '<unknown>', mode='eval')
    
    class RewriteName(ast.NodeTransformer):
        def visit_Name(self, node):
            return ast.Subscript(
                value=ast.Name(id='data', ctx=ast.Load()),
                slice=ast.Index(value=ast.Str(s=node.id)),
                ctx=node.ctx
            )
    
    a2 = ast.fix_missing_locations(RewriteName().visit(a))

    @terryjreedy
    Copy link
    Member

    I am reopening this as a doc bug because RewriteName is a copy (with 'ast.' prefixes added) of a buggy example in the doc. The bug is that the new .value Name and Str attributes do not get the required 'lineno' and 'col_offset' attributes. As Armin said, copy_location is not recursive and does not fix children of the node it fixes. Also, the recursive .visit method does not recurse into children of replacement nodes (and if it did, the new Str node would still not be fixed).

    The fix could be to reuse the Name node and add another copy_location call: the following works.

        def visit_Name(self, node):
            return ast.copy_location(
                ast.Subscript(
                    value=node,
                    slice=ast.Index(value=ast.copy_location(
                        ast.Str(s=node.id), node)),
                    ctx=node.ctx),
                node)

    but I think this illustrates that comment in the fix_missing_locations() entry that locations are "tedious to fill in for generated nodes". So I think the doc fix should use Armin's version of RewriteName and say to call fix_missing_locations on the result of .visit if new nodes are added. (I checked that his code still works in 3.5).

    The entry for NodeTransformer might mention that .visit does not recurse into replacement nodes.

    The missing lineno error came up in this python-list thread:
    https://mail.python.org/pipermail/python-list/2015-June/693316.html

    @terryjreedy terryjreedy added the docs Documentation in the Doc dir label Jun 29, 2015
    @terryjreedy terryjreedy reopened this Jun 29, 2015
    @terryjreedy terryjreedy changed the title ast.NodeTransformer bug ast.NodeTransformer doc bug Jun 29, 2015
    @terryjreedy terryjreedy added the type-bug An unexpected behavior, bug, or error label Jun 29, 2015
    @terryjreedy
    Copy link
    Member

    I re-verified the problem, its presence in the doc, and the fix with 3.9.

    @terryjreedy terryjreedy added 3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes labels Nov 17, 2019
    @pablogsal
    Copy link
    Member

    New changeset 6680f4a by Pablo Galindo (Batuhan Taşkaya) in branch 'master':
    bpo-3530: Add advice on when to correctly use fix_missing_locations in the AST docs (GH-17172)
    6680f4a

    @miss-islington
    Copy link
    Contributor

    New changeset e222b4c by Miss Islington (bot) in branch '3.7':
    bpo-3530: Add advice on when to correctly use fix_missing_locations in the AST docs (GH-17172)
    e222b4c

    @miss-islington
    Copy link
    Contributor

    New changeset ef0af30 by Miss Islington (bot) in branch '3.8':
    bpo-3530: Add advice on when to correctly use fix_missing_locations in the AST docs (GH-17172)
    ef0af30

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes docs Documentation in the Doc dir stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    5 participants