Exemple #1
0
    def visit_AnnAssign(self, node: ast.AnnAssign):
        if isinstance(node.target, ast.Name):
            if isinstance(node.annotation, ast.Name):
                name = node.annotation
                if self.is_const(name):
                    value = ast.Expression(node.value)
                    code = compile(value, self.filename, 'eval')
                    result = eval(code, {}, self.ctx)
                    self.additional_consts.append(result)
                    self.ctx[node.target.id] = result
                    self.constant_symbols[node.target.id] = len(
                        self.additional_consts)
                    return []

            elif isinstance(node.annotation, ast.Subscript):
                subsript = node.annotation
                if isinstance(subsript.value, ast.Name) and self.is_const(
                        subsript.value):
                    node.annotation = subsript.slice.value

                    value = ast.Expression(node.value)
                    code = compile(value, self.filename, 'eval')
                    result = eval(code, {}, self.ctx)
                    self.additional_consts.append(result)
                    # self.ctx[node.target.id] = result
                    self.constant_symbols[node.target.id] = len(
                        self.additional_consts)
                    return []

        return self.generic_visit(node)
Exemple #2
0
 def visit_AnnAssign(self, node: ast.AnnAssign) -> ast.AnnAssign:
     node = cast(ast.AnnAssign, self.generic_visit(node))
     node.annotation = self._visit_annotation(node.annotation)
     return node