Beispiel #1
0
    def visit_UnaryOp(self, node: ast.UnaryOp) -> Union[ast.UnaryOp, ast.Constant]:
        """Evaluate unary operation and return ast.Constant if operand is bound.

        Args:
            node: Unary operation to evaluate.

        Returns:
            Evaluated value.
        """
        node.operand = self.visit(node.operand)
        if isinstance(node.operand, (ast.Constant, ast.Num)):
            val = ast.Constant(n=self._match_ops(node.op, self._unary_ops, node.operand.n))
            return ast.copy_location(val, node)
        return node
Beispiel #2
0
    def visit_UnaryOp(
            self,
            ast_node_UnaryOp: ast.UnaryOp) -> Union[ast.UnaryOp, ast.Constant]:
        """
        Evaluate unary operation and return ast.UnaryOp or ast.Constant if
        operand is bound.

        :param ast_node_UnaryOp: unary operation to evaluate.
        :return: evaluated value.
        """
        ast_node_UnaryOp.operand = self.visit(ast_node_UnaryOp.operand)
        if isinstance(ast_node_UnaryOp.operand, ast.Constant):
            new_ast_node_UnaryOp = ast.Constant(value=self._match_operator(
                ast_node_UnaryOp.op, self._ast_math_UnaryOperators,
                ast_node_UnaryOp.operand.value))
            return ast.copy_location(new_ast_node_UnaryOp, ast_node_UnaryOp)

        return ast_node_UnaryOp