File: test_astroid.py

package info (click to toggle)
python-asttokens 2.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 600 kB
  • sloc: python: 3,468; makefile: 28
file content (39 lines) | stat: -rw-r--r-- 1,044 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# -*- coding: utf-8 -*-
from __future__ import unicode_literals, print_function

import astroid

from asttokens import ASTTokens
from asttokens.astroid_compat import astroid_node_classes
from . import test_mark_tokens


class TestAstroid(test_mark_tokens.TestMarkTokens):

  is_astroid_test = True
  module = astroid

  nodes_classes = astroid_node_classes.NodeNG
  context_classes = [
    (astroid.Name, astroid.DelName, astroid.AssignName),
    (astroid.Attribute, astroid.DelAttr, astroid.AssignAttr),
  ]

  @staticmethod
  def iter_fields(node):
    """
    Yield a tuple of ``(fieldname, value)`` for each field
    that is present on *node*.

    Similar to ast.iter_fields, but for astroid and ignores context
    """
    for field in node._astroid_fields + node._other_fields:
      if field == 'ctx':
        continue
      yield field, getattr(node, field)

  @staticmethod
  def create_asttokens(source):
    builder = astroid.builder.AstroidBuilder()
    tree = builder.string_build(source)
    return ASTTokens(source, tree=tree)