Skip to content

Commit b5e8917

Browse files
committed
Closes #191, closes #174
1 parent 0cb400d commit b5e8917

File tree

7 files changed

+219
-70
lines changed

7 files changed

+219
-70
lines changed

tests/fixtures/noqa.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
def some(): # noqa: Z110, Z113
1818
from my_module import some_function # noqa: Z435
1919

20+
class Nested(object): ... # noqa: 431
21+
2022
def nested(): ... # noqa: Z430
2123

2224

@@ -36,6 +38,10 @@ class BadClass: # noqa: Z306
3638
def some_static():
3739
...
3840

41+
@staticmethod # noqa: Z433
42+
async def some_async_static():
43+
...
44+
3945
def __del__(self, *args, **kwargs): # noqa: Z434
4046
...
4147

tests/test_visitors/test_ast/test_complexity/test_nested/test_nested_classes.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,39 @@
1111
)
1212

1313
nested_class = """
14-
class Parent:
15-
class {0}: ...
14+
class Parent(object):
15+
class {0}(object): ...
1616
"""
1717

1818
nested_class_in_method = """
19-
class Parent:
19+
class Parent(object):
2020
def container(self):
21-
class {0}: ...
21+
class {0}(object): ...
22+
"""
23+
24+
nested_class_in_async_method = """
25+
class Parent(object):
26+
async def container(self):
27+
class {0}(object): ...
2228
"""
2329

2430
nested_class_in_function = """
2531
def container():
26-
class {0}: ...
32+
class {0}(object): ...
33+
"""
34+
35+
nested_class_in_async_function = """
36+
async def container():
37+
class {0}(object): ...
2738
"""
2839

2940

3041
@pytest.mark.parametrize('code', [
3142
nested_class,
3243
nested_class_in_method,
44+
nested_class_in_async_method,
3345
nested_class_in_function,
46+
nested_class_in_async_function,
3447
])
3548
def test_nested_class(assert_errors, parse_ast_tree, code, default_options):
3649
"""Testing that nested classes are restricted."""
@@ -64,7 +77,9 @@ def test_whitelist_nested_classes(
6477
])
6578
@pytest.mark.parametrize('code', [
6679
nested_class_in_method,
80+
nested_class_in_async_method,
6781
nested_class_in_function,
82+
nested_class_in_async_function,
6883
])
6984
def test_whitelist_nested_classes_in_functions(
7085
assert_errors, parse_ast_tree, whitelist_name, code, default_options,
@@ -81,7 +96,10 @@ def test_whitelist_nested_classes_in_functions(
8196
def test_ordinary_class(assert_errors, parse_ast_tree, default_options):
8297
"""Testing that it is possible to write basic classes."""
8398
tree = parse_ast_tree("""
84-
class Ordinary:
99+
class Ordinary(object):
100+
def method(self): ...
101+
102+
class Second(Ordinary):
85103
def method(self): ...
86104
""")
87105

tests/test_visitors/test_ast/test_complexity/test_nested/test_nested_functions.py

Lines changed: 88 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,88 @@
1010
NestedComplexityVisitor,
1111
)
1212

13-
nested_function = """
13+
# Functions:
14+
15+
nested_function_in_function = """
1416
def container():
1517
def {0}(): ...
1618
"""
1719

18-
nested_method = """
19-
class Raw:
20+
nested_function_in_async_function = """
21+
async def container():
22+
def {0}(): ...
23+
"""
24+
25+
nested_async_function_in_async_function = """
26+
async def container():
27+
async def {0}(): ...
28+
"""
29+
30+
nested_async_function_in_function = """
31+
def container():
32+
async def {0}(): ...
33+
"""
34+
35+
# Methods:
36+
37+
nested_function_in_method = """
38+
class Raw(object):
2039
def container(self):
2140
def {0}(): ...
2241
"""
2342

43+
nested_function_in_async_method = """
44+
class Raw(object):
45+
async def container(self):
46+
def {0}(): ...
47+
"""
48+
49+
nested_async_function_in_async_method = """
50+
class Raw(object):
51+
async def container(self):
52+
async def {0}(): ...
53+
"""
54+
55+
nested_async_function_in_method = """
56+
class Raw(object):
57+
def container(self):
58+
async def {0}(): ...
59+
"""
60+
61+
# Lambdas:
62+
63+
lambda_in_function = """
64+
def container():
65+
lazy_value = lambda: 12
66+
"""
67+
68+
lambda_in_async_function = """
69+
async def container():
70+
lazy_value = lambda: 12
71+
"""
72+
73+
lambda_in_method = """
74+
class Raw(object):
75+
def container(self):
76+
lazy_value = lambda: 12
77+
"""
78+
79+
lambda_in_async_method = """
80+
class Raw(object):
81+
def container(self):
82+
lazy_value = lambda: 12
83+
"""
84+
2485

2586
@pytest.mark.parametrize('code', [
26-
nested_function,
27-
nested_method,
87+
nested_function_in_function,
88+
nested_async_function_in_function,
89+
nested_function_in_async_function,
90+
nested_async_function_in_async_function,
91+
nested_function_in_method,
92+
nested_async_function_in_method,
93+
nested_function_in_async_method,
94+
nested_async_function_in_async_method,
2895
])
2996
def test_nested_function(assert_errors, parse_ast_tree, code, default_options):
3097
"""Testing that nested functions are restricted."""
@@ -38,8 +105,14 @@ def test_nested_function(assert_errors, parse_ast_tree, code, default_options):
38105

39106
@pytest.mark.parametrize('whitelist_name', NESTED_FUNCTIONS_WHITELIST)
40107
@pytest.mark.parametrize('code', [
41-
nested_function,
42-
nested_method,
108+
nested_function_in_function,
109+
nested_async_function_in_function,
110+
nested_function_in_async_function,
111+
nested_async_function_in_async_function,
112+
nested_function_in_method,
113+
nested_async_function_in_method,
114+
nested_function_in_async_method,
115+
nested_async_function_in_async_method,
43116
])
44117
def test_whitelist_nested_functions(
45118
assert_errors, parse_ast_tree, whitelist_name, code, default_options,
@@ -53,14 +126,17 @@ def test_whitelist_nested_functions(
53126
assert_errors(visitor, [])
54127

55128

129+
@pytest.mark.parametrize('code', [
130+
lambda_in_function,
131+
lambda_in_async_function,
132+
lambda_in_method,
133+
lambda_in_async_method,
134+
])
56135
def test_lambda_nested_functions(
57-
assert_errors, parse_ast_tree, default_options,
136+
assert_errors, parse_ast_tree, code, default_options,
58137
):
59138
"""Testing that it is possible to nest lambda inside functions."""
60-
tree = parse_ast_tree("""
61-
def container():
62-
lazy_value = lambda: 12
63-
""")
139+
tree = parse_ast_tree(code)
64140

65141
visitor = NestedComplexityVisitor(default_options, tree=tree)
66142
visitor.run()
@@ -83,17 +159,3 @@ def container():
83159
visitor.run()
84160

85161
assert_errors(visitor, [NestedFunctionViolation])
86-
87-
88-
def test_lambda_nested_method(assert_errors, parse_ast_tree, default_options):
89-
"""Testing that it is possible to nest lambda inside methods."""
90-
tree = parse_ast_tree("""
91-
class Raw:
92-
def container(self):
93-
lazy_value = lambda: 12
94-
""")
95-
96-
visitor = NestedComplexityVisitor(default_options, tree=tree)
97-
visitor.run()
98-
99-
assert_errors(visitor, [])

tests/test_visitors/test_ast/test_general/test_wrong_class/test_magic_methods.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,52 @@ class Example(object):
1313
def {0}(): ...
1414
"""
1515

16+
async_magic_method = """
17+
class Example(object):
18+
async def {0}(): ...
19+
"""
20+
1621

22+
@pytest.mark.parametrize('code', [
23+
magic_method,
24+
async_magic_method,
25+
])
1726
@pytest.mark.parametrize('method', BAD_MAGIC_METHODS)
1827
def test_wrong_magic_used(
19-
assert_errors, parse_ast_tree, method, default_options,
28+
assert_errors,
29+
parse_ast_tree,
30+
code,
31+
method,
32+
default_options,
2033
):
2134
"""Testing that some magic methods are restricted."""
22-
tree = parse_ast_tree(magic_method.format(method))
35+
tree = parse_ast_tree(code.format(method))
2336

2437
visitor = WrongClassVisitor(default_options, tree=tree)
2538
visitor.run()
2639

2740
assert_errors(visitor, [BadMagicMethodViolation])
2841

2942

43+
@pytest.mark.parametrize('code', [
44+
magic_method,
45+
async_magic_method,
46+
])
3047
@pytest.mark.parametrize('method', [
3148
'__add__',
3249
'__init__',
3350
'next',
3451
'regular',
3552
])
3653
def test_regular_method_used(
37-
assert_errors, parse_ast_tree, method, default_options,
54+
assert_errors,
55+
parse_ast_tree,
56+
code,
57+
method,
58+
default_options,
3859
):
3960
"""Testing that other methods are working fine."""
40-
tree = parse_ast_tree(magic_method.format(method))
61+
tree = parse_ast_tree(code.format(method))
4162

4263
visitor = WrongClassVisitor(default_options, tree=tree)
4364
visitor.run()

tests/test_visitors/test_ast/test_general/test_wrong_class/test_staticmethod.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,50 @@ class Example(object):
1313
def should_fail(): ...
1414
"""
1515

16+
async_decorated_method = """
17+
class Example(object):
18+
@{0}
19+
async def should_fail(): ...
20+
"""
21+
1622

17-
def test_staticmethod_used(assert_errors, parse_ast_tree, default_options):
23+
@pytest.mark.parametrize('code', [
24+
decorated_method,
25+
async_decorated_method,
26+
])
27+
def test_staticmethod_used(
28+
assert_errors,
29+
parse_ast_tree,
30+
code,
31+
default_options,
32+
):
1833
"""Testing that some built-in functions are restricted as decorators."""
19-
tree = parse_ast_tree(decorated_method.format('staticmethod'))
34+
tree = parse_ast_tree(code.format('staticmethod'))
2035

2136
visitor = WrongClassVisitor(default_options, tree=tree)
2237
visitor.run()
2338

2439
assert_errors(visitor, [StaticMethodViolation])
2540

2641

42+
@pytest.mark.parametrize('code', [
43+
decorated_method,
44+
async_decorated_method,
45+
])
2746
@pytest.mark.parametrize('decorator', [
2847
'classmethod',
2948
'custom',
3049
'with_params(12, 100)',
3150
])
3251
def test_regular_decorator_used(
33-
assert_errors, parse_ast_tree, decorator, default_options,
52+
assert_errors,
53+
parse_ast_tree,
54+
decorator,
55+
code,
56+
default_options,
3457
):
3558
"""Testing that other decorators are allowed."""
36-
tree = parse_ast_tree(decorated_method.format(decorator))
59+
tree = parse_ast_tree(code.format(decorator))
3760

3861
visitor = WrongClassVisitor(default_options, tree=tree)
3962
visitor.run()

0 commit comments

Comments
 (0)