Python ast.AnnAssign() Examples

The following are 11 code examples of ast.AnnAssign(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module ast , or try the search function .
Example #1
Source File: helper.py    From YAPyPy with MIT License 6 votes vote down vote up
def expr_stmt_rewrite(lhs, ann, aug, aug_exp, rhs: t.Optional[list]):
    if rhs:
        as_store(lhs)
        *init, end = rhs
        for each in init:
            as_store(each)
        return ast.Assign([lhs, *init], end)

    if ann:
        as_store(lhs)
        anno, value = ann
        return ast.AnnAssign(lhs, anno, value, 1)

    if aug_exp:
        as_store(lhs)
        return ast.AugAssign(lhs, aug(), aug_exp)

    # NO AS STORE HERE!
    return ast.Expr(lhs) 
Example #2
Source File: symbol_analyzer.py    From YAPyPy with MIT License 5 votes vote down vote up
def _visit_ann_assign(self: 'ASTTagger', node: ast.AnnAssign):
    self.symtable.cts.add(ContextType.Annotation)
    return node 
Example #3
Source File: flake8_builtins.py    From flake8-builtins with GNU General Public License v2.0 5 votes vote down vote up
def check_assignment(self, statement):
        msg = self.assign_msg
        if type(statement.__flake8_builtins_parent) is ast.ClassDef:
            msg = self.class_attribute_msg

        if isinstance(statement, ast.Assign):
            stack = list(statement.targets)
        else:  # This is `ast.AnnAssign` or `ast.NamedExpr`:
            stack = [statement.target]

        while stack:
            item = stack.pop()
            if isinstance(item, (ast.Tuple, ast.List)):
                stack.extend(list(item.elts))
            elif isinstance(item, ast.Name) and \
                    item.id in BUILTINS:
                yield self.error(item, message=msg, variable=item.id)
            elif PY3 and isinstance(item, ast.Starred):
                if hasattr(item.value, 'id') and item.value.id in BUILTINS:
                    yield self.error(
                        statement,
                        message=msg,
                        variable=item.value.id,
                    )
                elif hasattr(item.value, 'elts'):
                    stack.extend(list(item.value.elts)) 
Example #4
Source File: ast_helpers.py    From flake8-variables-names with MIT License 5 votes vote down vote up
def get_var_names_from_assignment(
    assignment_node: Union[ast.Assign, ast.AnnAssign],
) -> List[Tuple[str, ast.AST]]:
    if isinstance(assignment_node, ast.AnnAssign):
        if isinstance(assignment_node.target, ast.Name):
            return [(assignment_node.target.id, assignment_node.target)]
    elif isinstance(assignment_node, ast.Assign):
        names = [t for t in assignment_node.targets if isinstance(t, ast.Name)]
        return [(n.id, n) for n in names]
    return [] 
Example #5
Source File: ast_helpers.py    From flake8-variables-names with MIT License 5 votes vote down vote up
def extract_all_variable_names(ast_tree: ast.AST) -> List[Tuple[str, ast.AST]]:
    var_info: List[Tuple[str, ast.AST]] = []
    assignments = [n for n in ast.walk(ast_tree) if isinstance(n, ast.Assign)]
    var_info += flat([get_var_names_from_assignment(a) for a in assignments])
    ann_assignments = [n for n in ast.walk(ast_tree) if isinstance(n, ast.AnnAssign)]
    var_info += flat([get_var_names_from_assignment(a) for a in ann_assignments])
    funcdefs = [n for n in ast.walk(ast_tree) if isinstance(n, ast.FunctionDef)]
    var_info += flat([get_var_names_from_funcdef(f) for f in funcdefs])
    fors = [n for n in ast.walk(ast_tree) if isinstance(n, ast.For)]
    var_info += flat([get_var_names_from_for(f) for f in fors])
    return var_info 
Example #6
Source File: liveness.py    From kappa with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def visit_AnnAssign(self, ann_assign: ast.AnnAssign) -> None:  # type: ignore
        self.visit_simple_stmt(ann_assign) 
Example #7
Source File: functions.py    From wemake-python-styleguide with MIT License 5 votes vote down vote up
def get_assign_targets(
    node: Union[AnyAssignWithWalrus, ast.AugAssign],
) -> List[ast.expr]:
    """Returns list of assign targets without knowing the type of assign."""
    if isinstance(node, (ast.AnnAssign, ast.AugAssign, NamedExpr)):
        return [node.target]
    return node.targets 
Example #8
Source File: jones.py    From wemake-python-styleguide with MIT License 5 votes vote down vote up
def _maybe_ignore_child(self, node: ast.AST) -> bool:
        if isinstance(node, ast.AnnAssign):
            self._to_ignore.extend(ast.walk(node.annotation))

        return node in self._to_ignore 
Example #9
Source File: annotations.py    From wemake-python-styleguide with MIT License 5 votes vote down vote up
def visit_AnnAssign(self, node: ast.AnnAssign) -> None:
        """
        Check assignment annotation.

        Raises:
            TooComplexAnnotationViolation

        """
        self._check_annotations_complexity(node, [node.annotation])
        self.generic_visit(node) 
Example #10
Source File: modules.py    From wemake-python-styleguide with MIT License 5 votes vote down vote up
def _check_mutable_constant(self, node: AnyAssign) -> None:
        if not isinstance(get_context(node), ast.Module):
            return

        if isinstance(node, ast.AnnAssign):
            targets = [node.target]
        else:
            targets = node.targets

        for target in targets:
            if not isinstance(target, ast.Name) or not is_constant(target.id):
                continue

            if isinstance(node.value, self._mutable_nodes):
                self.add_violation(MutableModuleConstantViolation(target)) 
Example #11
Source File: predicates.py    From wemake-python-styleguide with MIT License 5 votes vote down vote up
def is_no_value_annotation(node: ast.AST) -> bool:
    """Check that variable has annotation without value."""
    return isinstance(node, ast.AnnAssign) and not node.value