@@ -17,19 +17,30 @@ def function():
1717"""
1818
1919function_with_nested_function_and_returns = """
20- def function(): # has two returns
20+ def function(): # has one return and a nested function
2121 def factory(): # has one return
2222 return 1
2323 return factory
2424"""
2525
26+ function_with_nested_class_and_returns = """
27+ def function(): # has one return (we do not count returns in nested objects)
28+ def inner_function(x):
29+ return x
30+ class Factory:
31+ def method(self):
32+ return 1
33+ return inner_function(Factory())
34+ """
35+
2636
2737@pytest .mark .parametrize (
2838 'code' ,
2939 [
3040 function_without_returns ,
3141 function_with_returns ,
3242 function_with_nested_function_and_returns ,
43+ function_with_nested_class_and_returns ,
3344 ],
3445)
3546def test_returns_correct_count (
@@ -52,7 +63,6 @@ def test_returns_correct_count(
5263 'code' ,
5364 [
5465 function_with_returns ,
55- function_with_nested_function_and_returns ,
5666 ],
5767)
5868def test_returns_wrong_count (
@@ -72,3 +82,27 @@ def test_returns_wrong_count(
7282
7383 assert_errors (visitor , [TooManyReturnsViolation ])
7484 assert_error_text (visitor , '2' , option_values .max_returns )
85+
86+
87+ @pytest .mark .parametrize (
88+ 'code' ,
89+ [
90+ function_with_nested_function_and_returns ,
91+ function_with_nested_class_and_returns ,
92+ ],
93+ )
94+ def test_returns_in_nested_objects_correct_count (
95+ assert_errors ,
96+ parse_ast_tree ,
97+ options ,
98+ code ,
99+ mode ,
100+ ):
101+ """Testing that returns in nested functions and classes are not counted."""
102+ tree = parse_ast_tree (mode (code ))
103+
104+ option_values = options (max_returns = 1 )
105+ visitor = FunctionComplexityVisitor (option_values , tree = tree )
106+ visitor .run ()
107+
108+ assert_errors (visitor , [])
0 commit comments