Skip to content

Strange behavior with tokens of import nodes #27

@jrfaller

Description

@jrfaller

Hi!

I tried the following code to parse import statements :

source = 'import asttokens as at'
atok = asttokens.ASTTokens(source, parse=True)

I get the following AST:

Module
  Import
    alias

Which is OK. My purpose is to gather the position of the identifiers asttokens and at in the code. Therefore I was trying to get the tokens composing the alias node, which sounds logic because asttokens as at is and alias definition. However when I ask for the tokens composing the alias node, using list(atok.get_tokens(atok.tree.body[0].names[0])) I get the following result :

[Token(type=1, string=u'import', start=(1, 0), end=(1, 6), line=u'import asttokens as at', index=0, startpos=0, endpos=6)]

Which is was not what I was expecting, I was expecting to have the ['asttokens', 'as', 'at'] tokens.

Is there something I am missing?

Best regards.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions