horast icon indicating copy to clipboard operation
horast copied to clipboard

AssertionError: assert path[-2].node is within_node, (path[-2].node, within_node)

Open ZdenekM opened this issue 3 years ago • 0 comments

I was getting an AssertionError because of this comment. With the comment gone, the error was also gone. Also, everything seems to be ok with the comment on its original place and disabled assertions.

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/zdenal/arcor2_repos/arcor2/build-support/.venv/lib/python3.6/site-packages/horast/parser.py", line 19, in parse
    tree = insert_comment_tokens(code, tree, comment_tokens)
  File "/home/zdenal/arcor2_repos/arcor2/build-support/.venv/lib/python3.6/site-packages/horast/ast_comments.py", line 87, in insert_comment_tokens
    tree = insert_comment_token(token, code, tree, nodes=nodes, scopes=scopes)
  File "/home/zdenal/arcor2_repos/arcor2/build-support/.venv/lib/python3.6/site-packages/horast/ast_comments.py", line 61, in insert_comment_token
    path_to_anchor, before_anchor = find_in_ast(code, tree, scope, nodes=nodes, scopes=scopes)
  File "/home/zdenal/arcor2_repos/arcor2/build-support/.venv/lib/python3.6/site-packages/horast/ast_tools.py", line 236, in find_in_ast
    assert path[-2].node is within_node, (path[-2].node, within_node)
AssertionError: (<typed_ast._ast3.ClassDef object at 0x7f5a7fe5ef60>, <typed_ast._ast3.Dict object at 0x7f5a7fea0470>)

While trying to construct a minimal example, I got a NotImplementedError...

>>> import horast

>>> code = """
d = {
    1: 2,
    3: 4,   # comment
}
"""

>>> horast.parse(code)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/zdenal/arcor2_repos/arcor2/build-support/.venv/lib/python3.6/site-packages/horast/parser.py", line 19, in parse
    tree = insert_comment_tokens(code, tree, comment_tokens)
  File "/home/zdenal/arcor2_repos/arcor2/build-support/.venv/lib/python3.6/site-packages/horast/ast_comments.py", line 87, in insert_comment_tokens
    tree = insert_comment_token(token, code, tree, nodes=nodes, scopes=scopes)
  File "/home/zdenal/arcor2_repos/arcor2/build-support/.venv/lib/python3.6/site-packages/horast/ast_comments.py", line 61, in insert_comment_token
    path_to_anchor, before_anchor = find_in_ast(code, tree, scope, nodes=nodes, scopes=scopes)
  File "/home/zdenal/arcor2_repos/arcor2/build-support/.venv/lib/python3.6/site-packages/horast/ast_tools.py", line 222, in find_in_ast
    scopes_containing_target_scope))
NotImplementedError: inconsistent results for target Scope(start=Location(lineno=4, offset=12), end=Location(lineno=4, offset=21)) in:
"""

d = {
    1: 2,
    3: 4,   # comment
}

after 0, before None, within [(<typed_ast._ast3.Module object at 0x7f7caa11acf8>, Scope(start=Location(lineno=1, offset=0), end=Location(lineno=5, offset=1))), (<typed_ast._ast3.Assign object at 0x7f7caa11ae80>, Scope(start=Location(lineno=2, offset=0), end=Location(lineno=5, offset=1))), (<typed_ast._ast3.Dict object at 0x7f7caa11ada0>, Scope(start=Location(lineno=2, offset=4), end=Location(lineno=5, offset=1)))]"""

ZdenekM avatar Oct 26 '20 07:10 ZdenekM