Skip to content

Commit 4414038

Browse files
authored
Move TestForAnyAll to functional tests (#5436)
* Move test_for_any_all to functional * Convert additional for_any_all tests to functional
1 parent f761b32 commit 4414038

File tree

3 files changed

+89
-247
lines changed

3 files changed

+89
-247
lines changed

tests/extensions/test_for_any_all.py

Lines changed: 0 additions & 247 deletions
This file was deleted.

tests/functional/ext/for_any_all/for_any_all.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,88 @@ def words_contains_word(words):
5959
if word == "word":
6060
return True
6161
return False
62+
63+
def complicated_condition_check(items):
64+
"""Case where we expect not any statement with a more complicated condition"""
65+
for item in items: # [consider-using-any-or-all]
66+
if item % 2 == 0 and (item % 3 == 0 or item > 15):
67+
return False
68+
return True
69+
70+
def is_from_decorator1(node):
71+
"""Case where we expect a particularly long message to be emitted."""
72+
for ancestor in node: # [consider-using-any-or-all]
73+
if (
74+
ancestor.name in ("Exception", "BaseException")
75+
and ancestor.root().name == "Exception"
76+
):
77+
return True
78+
return False
79+
80+
def is_from_decorator2(items):
81+
"""Case where we expect an all statement because of negation in the condition"""
82+
for item in items: # [consider-using-any-or-all]
83+
if not(item % 2 == 0 and (item % 3 == 0 or item > 15)):
84+
return False
85+
return True
86+
87+
def is_from_decorator3(node):
88+
"""Case where we expect a not all statement because of negation in the condition"""
89+
for ancestor in node: # [consider-using-any-or-all]
90+
if not (
91+
ancestor.name in ("Exception", "BaseException")
92+
and ancestor.root().name == "Exception"
93+
):
94+
return True
95+
return False
96+
97+
def no_suggestion_if_not_if():
98+
"""Do not emit if the for loop does not have the pattern we are looking for"""
99+
for val in range(1):
100+
var = val
101+
return var
102+
103+
def no_suggestion_if_not_bool(item):
104+
"""Do not emit if the if-statement does not return a bool"""
105+
for parent in item.parents():
106+
if isinstance(parent, str):
107+
return "True"
108+
return "False"
109+
110+
def print_items(items):
111+
"""Do not emit if there is no If condition in the for loop."""
112+
for item in items:
113+
print(item)
114+
return True
115+
116+
def print_items2(items):
117+
"""Do not emit if anything besides a boolean is returned."""
118+
for item in items:
119+
return item
120+
return True
121+
122+
def print_items3(items):
123+
"""Do not emit if anything besides a boolean is returned."""
124+
for _ in items:
125+
return False
126+
return items
127+
128+
def print_items4(items):
129+
"""Do not emit if there is more logic which can cause side effects
130+
or become less readable in a list comprehension.
131+
"""
132+
for item in items:
133+
if isinstance(item, str):
134+
print(item)
135+
return False
136+
return True
137+
138+
def is_from_decorator(node):
139+
"""Do not emit if the if has an else condition. Generally implies more complicated logic."""
140+
for parent in node.node_ancestors():
141+
if isinstance(parent, str): # pylint: disable=no-else-return
142+
return True
143+
else:
144+
if parent in parent.selected_annotations:
145+
return False
146+
return False

tests/functional/ext/for_any_all/for_any_all.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ consider-using-any-or-all:33:4:35:23:is_from_string:`for` loop could be `any(isi
66
consider-using-any-or-all:40:4:42:23:is_not_from_string:`for` loop could be `not all(isinstance(parent, str) for parent in item.parents())`:UNDEFINED
77
consider-using-any-or-all:49:8:51:28:nested_check:`for` loop could be `not any(item in (1, 2, 3) for item in items)`:UNDEFINED
88
consider-using-any-or-all:58:4:60:23:words_contains_word:`for` loop could be `any(word == 'word' for word in words)`:UNDEFINED
9+
consider-using-any-or-all:65:4:67:24:complicated_condition_check:`for` loop could be `not any(item % 2 == 0 and (item % 3 == 0 or item > 15) for item in items)`:UNDEFINED
10+
consider-using-any-or-all:72:4:77:23:is_from_decorator1:`for` loop could be `any(ancestor.name in ('Exception', 'BaseException') and ancestor.root().name == 'Exception' for ancestor in node)`:UNDEFINED
11+
consider-using-any-or-all:82:4:84:24:is_from_decorator2:`for` loop could be `all(item % 2 == 0 and (item % 3 == 0 or item > 15) for item in items)`:UNDEFINED
12+
consider-using-any-or-all:89:4:94:23:is_from_decorator3:`for` loop could be `not all(ancestor.name in ('Exception', 'BaseException') and ancestor.root().name == 'Exception' for ancestor in node)`:UNDEFINED

0 commit comments

Comments
 (0)