Skip to content

Commit

Permalink
fixes 1: validate complexity of ann assignments
Browse files Browse the repository at this point in the history
  • Loading branch information
Илья Лебедев committed Feb 26, 2019
1 parent 3121591 commit 451ef83
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
18 changes: 10 additions & 8 deletions flake8_annotations_complexity/ast_helpres.py
Expand Up @@ -18,15 +18,17 @@ def validate_annotations_in_ast_node(node, max_annotations_complexity) -> List[T
f for f in ast.walk(node)
if isinstance(f, ast.FunctionDef)
]
annotations: List[ast.AST] = []
for funcdef in func_defs:
annotations = list(filter(None, [a.annotation for a in funcdef.args.args]))
annotations += list(filter(None, [a.annotation for a in funcdef.args.args]))
if funcdef.returns:
annotations.append(funcdef.returns)
for annotation in annotations:
complexity = get_annotation_compexity(annotation)
if complexity > max_annotations_complexity:
too_difficult_annotations.append((
annotation,
complexity,
))
annotations += [a.annotation for a in ast.walk(node) if isinstance(a, ast.AnnAssign) and a.annotation]
for annotation in annotations:
complexity = get_annotation_compexity(annotation)
if complexity > max_annotations_complexity:
too_difficult_annotations.append((
annotation,
complexity,
))
return too_difficult_annotations
5 changes: 5 additions & 0 deletions tests/test_annotations_complexity.py
Expand Up @@ -25,3 +25,8 @@ def test_ok_for_string_annotations_file():
assert len(errors) == 1
errors = run_validator_for_test_file('string_annotations.py', max_annotations_complexity=1)
assert len(errors) == 2


def test_validates_annotations_complexity_for_annassigments():
errors = run_validator_for_test_file('var_annotation.py')
assert len(errors) == 1
2 changes: 2 additions & 0 deletions tests/test_files/var_annotation.py
@@ -0,0 +1,2 @@

foo: Tuple[List[int], Optional[Dict[str, int]]] = tuple()

0 comments on commit 451ef83

Please sign in to comment.