• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

CityOfZion / neo3-boa / e084b44c-1a5f-4649-92cd-d3ed06099181

05 Mar 2024 05:58PM UTC coverage: 92.023% (-0.08%) from 92.107%
e084b44c-1a5f-4649-92cd-d3ed06099181

push

circleci

Mirella de Medeiros
CU-86drpnc9z - Drop support to Python 3.10

20547 of 22328 relevant lines covered (92.02%)

1.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.44
/boa3/internal/compiler/codegenerator/initstatementsvisitor.py
1
import ast
2✔
2

3
from boa3.internal import constants
2✔
4
from boa3.internal.analyser.astanalyser import IAstAnalyser
2✔
5
from boa3.internal.model.symbol import ISymbol
2✔
6

7

8
class InitStatementsVisitor(IAstAnalyser):
2✔
9
    """
10
    This class is separate the instructions that should be included in the '_deploy" internal method from those
11
    from the '_initialize' internal method.
12

13
    The methods with the name starting with 'visit_' are implementations of methods from the :class:`NodeVisitor` class.
14
    These methods are used to walk through the Python abstract syntax tree.
15

16
    """
17

18
    def __init__(self, symbols: dict[str, ISymbol], fail_fast: bool = True):
2✔
19
        super().__init__(ast.parse(""), log=True, fail_fast=fail_fast)
2✔
20
        self.symbols = symbols.copy()
2✔
21

22
        self._deploy_instructions: list[ast.AST] = []
2✔
23
        self._init_instructions: list[ast.AST] = []
2✔
24

25
    @classmethod
2✔
26
    def separate_global_statements(cls,
2✔
27
                                   symbol_table: dict[str, ISymbol],
28
                                   statements: list[ast.AST]) -> tuple[list[ast.AST], list[ast.AST]]:
29

30
        visitor = InitStatementsVisitor(symbol_table)
2✔
31

32
        root_ast = ast.parse("")
2✔
33
        root_ast.body = statements
2✔
34
        visitor.visit(root_ast)
2✔
35

36
        return visitor._deploy_instructions, visitor._init_instructions
2✔
37

38
    def get_symbol_id(self, node: ast.AST, parent: ast.AST) -> str:
2✔
39
        symbol_id = self.visit(node)
2✔
40
        # filter to find the imported variables
41
        if symbol_id not in self.symbols and hasattr(parent, 'origin') and isinstance(parent.origin, ast.AST):
2✔
42
            symbol_id = '{0}{2}{1}'.format(parent.origin.__hash__(), symbol_id, constants.VARIABLE_NAME_SEPARATOR)
2✔
43
        return symbol_id
2✔
44

45
    def append_symbol(self, symbol: ISymbol, node: ast.AST):
2✔
46
        if self._should_be_on_deploy(symbol):
2✔
47
            if node not in self._deploy_instructions:
2✔
48
                self._deploy_instructions.append(node)
2✔
49
        else:
50
            if node not in self._init_instructions:
2✔
51
                self._init_instructions.append(node)
2✔
52

53
    def _should_be_on_deploy(self, symbol: ISymbol) -> bool:
2✔
54
        return hasattr(symbol, 'is_reassigned') and symbol.is_reassigned
2✔
55

56
    # region AST visitors
57

58
    def visit_Assign(self, node: ast.Assign):
2✔
59
        deploy_var_nodes = []
2✔
60
        init_var_nodes = []
2✔
61

62
        deploy_symbol = None
2✔
63
        init_symbol = None
2✔
64
        for target in node.targets:
2✔
65
            target_id = self.get_symbol_id(target, node)
2✔
66
            symbol = self.get_symbol(target_id)
2✔
67

68
            if self._should_be_on_deploy(symbol):
2✔
69
                deploy_var_nodes.append(target)
2✔
70
                deploy_symbol = symbol
2✔
71
            else:
72
                init_var_nodes.append(target)
2✔
73
                init_symbol = symbol
2✔
74

75
        if len(deploy_var_nodes) == len(node.targets) or len(init_var_nodes) == len(node.targets):
2✔
76
            self.append_symbol(deploy_symbol if deploy_symbol is not None else init_symbol,
×
77
                               node)
78
        else:
79
            deploy_ast = ast.Assign(targets=deploy_var_nodes,
2✔
80
                                    value=node.value)
81
            ast.copy_location(deploy_ast, node)
2✔
82

83
            init_ast = ast.Assign(targets=init_var_nodes,
2✔
84
                                  value=node.value)
85
            ast.copy_location(init_ast, node)
2✔
86

87
            if hasattr(node, 'origin'):
2✔
88
                deploy_ast.origin = node.origin
2✔
89
                init_ast.origin = node.origin
2✔
90

91
            self.append_symbol(deploy_symbol, deploy_ast)
2✔
92
            self.append_symbol(init_symbol, init_ast)
2✔
93

94
    def visit_AnnAssign(self, node: ast.AnnAssign):
2✔
95
        var_id = self.get_symbol_id(node.target, node)
2✔
96
        symbol = self.get_symbol(var_id)
2✔
97
        self.append_symbol(symbol, node)
2✔
98

99
    def visit_AugAssign(self, node: ast.AugAssign):
2✔
100
        var_id = self.get_symbol_id(node.target, node)
2✔
101
        symbol = self.get_symbol(var_id)
2✔
102
        self.append_symbol(symbol, node)
2✔
103

104
    def visit_Name(self, name: ast.Name) -> str:
2✔
105
        """
106
        Visitor of a name node
107

108
        :param name: the python ast name identifier node
109
        :return: the identifier of the name
110
        """
111
        return name.id
2✔
112

113
    # endregion
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc