本文整理匯總了Python中ast.AnnAssign方法的典型用法代碼示例。如果您正苦於以下問題:Python ast.AnnAssign方法的具體用法?Python ast.AnnAssign怎麽用?Python ast.AnnAssign使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類ast
的用法示例。
在下文中一共展示了ast.AnnAssign方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: expr_stmt_rewrite
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import AnnAssign [as 別名]
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)
示例2: _visit_ann_assign
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import AnnAssign [as 別名]
def _visit_ann_assign(self: 'ASTTagger', node: ast.AnnAssign):
self.symtable.cts.add(ContextType.Annotation)
return node
示例3: check_assignment
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import AnnAssign [as 別名]
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))
示例4: get_var_names_from_assignment
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import AnnAssign [as 別名]
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 []
示例5: extract_all_variable_names
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import AnnAssign [as 別名]
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
示例6: visit_AnnAssign
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import AnnAssign [as 別名]
def visit_AnnAssign(self, ann_assign: ast.AnnAssign) -> None: # type: ignore
self.visit_simple_stmt(ann_assign)
示例7: get_assign_targets
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import AnnAssign [as 別名]
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
示例8: _maybe_ignore_child
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import AnnAssign [as 別名]
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
示例9: visit_AnnAssign
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import AnnAssign [as 別名]
def visit_AnnAssign(self, node: ast.AnnAssign) -> None:
"""
Check assignment annotation.
Raises:
TooComplexAnnotationViolation
"""
self._check_annotations_complexity(node, [node.annotation])
self.generic_visit(node)
示例10: _check_mutable_constant
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import AnnAssign [as 別名]
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))
示例11: is_no_value_annotation
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import AnnAssign [as 別名]
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