Skip to content

Commit f743b96

Browse files
barsovichoksobolevn
authored andcommitted
Bug, variables from blacklisted names looks like _{BLACKLISTED NAMES} are ignored (#19)
Closes #15
1 parent 3f08f24 commit f743b96

File tree

5 files changed

+115
-3
lines changed

5 files changed

+115
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#### joe made this: http://goel.io/joe
22
#### python ####
33
# Byte-compiled / optimized / DLL files
4+
.pytest_cache
45
__pycache__/
56
*.py[cod]
67
*$py.class

tests/fixtures/wrong_variable.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
This file contains all broken variable names.
5+
"""
6+
_data = 1
7+
_result = 1
8+
__author__ = 'John Doe' # error here
9+
10+
x = 1 # error here
11+
12+
13+
def fixture():
14+
__author__ = 'John Doe' # no error, because not module metadata
15+
16+
17+
def check_function_args(data, t, *a, **vals): # 4 errors here
18+
result = data + t # error here for `result`
19+
return result
20+
21+
22+
with open('missing.txt') as f: # error here
23+
contents = f.read() # error here
24+
print(contents)
25+
26+
27+
for item in range(1, 3): # error here
28+
continue
29+
30+
31+
for _ in range(1, 3): # should not raise error here
32+
continue
33+
34+
35+
try:
36+
1 / 0
37+
except ZeroDivisionError as e: # TODO: error here
38+
pass
39+
40+
41+
class Fixture(object):
42+
data = 'data' # error here
43+
result: int # error here
44+
45+
def __init__(self, value): # error here
46+
self.var = value # error here only for `var`, not for `value`
47+
self.x = value # error here only for `x`, not for `value`
48+
49+
50+
val = Fixture() # error here
51+
print(val.var) # no error here
52+
print(val.x) # no error here
53+
54+
if val:
55+
__author__ = 'John' # no error here since it's a rare use of module meta
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import subprocess
4+
5+
6+
def test_wrong_variables_in_fixture(absolute_path):
7+
"""End-to-End test to check variable rules."""
8+
filename = absolute_path('fixtures', 'wrong_variable.py')
9+
process = subprocess.Popen(
10+
['flake8', filename],
11+
stdout=subprocess.PIPE,
12+
stderr=subprocess.PIPE,
13+
)
14+
stdout, _ = process.communicate()
15+
16+
assert stdout.count(b'WPS120') == 8
17+
assert stdout.count(b'WPS121') == 3
18+
assert stdout.count(b'WPS122') == 3
19+
assert stdout.count(b'WPS123') == 2
20+
assert stdout.count(b'WPS124') == 1
21+
assert stdout.count(b'WPS125') == 1
22+
assert stdout.count(b'WPS126') == 1

tests/test_visitors/test_wrong_name/test_variable_names.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,22 @@
1515
{0} = 'test'
1616
"""
1717

18+
underscore_variable_test1 = """
19+
_{0} = 'test'
20+
"""
21+
22+
underscore_variable_test2 = """
23+
{0}_ = 'test'
24+
"""
25+
26+
underscore_variable_test3 = """
27+
__{0} = 'test'
28+
"""
29+
30+
underscore_variable_test4 = """
31+
{0}__ = 'test'
32+
"""
33+
1834
for_variable_test = """
1935
for {0} in []:
2036
print()
@@ -39,6 +55,10 @@
3955
for_variable_test,
4056
with_variable_test,
4157
exception_test,
58+
underscore_variable_test1,
59+
underscore_variable_test2,
60+
underscore_variable_test3,
61+
underscore_variable_test4,
4262
])
4363
def test_wrong_variable_names(
4464
assert_errors, parse_ast_tree, bad_name, code,
@@ -77,6 +97,10 @@ def test_too_short_variable_names(
7797
for_variable_test,
7898
with_variable_test,
7999
exception_test,
100+
underscore_variable_test1,
101+
underscore_variable_test2,
102+
underscore_variable_test3,
103+
underscore_variable_test4,
80104
])
81105
def test_correct_variable_name(
82106
assert_errors, parse_ast_tree, code, correct_name,

wemake_python_styleguide/helpers/variables.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,22 @@ def is_wrong_variable_name(name: str, to_check: Iterable[str]) -> bool:
99
1010
>>> is_wrong_variable_name('wrong', ['wrong'])
1111
True
12-
1312
>>> is_wrong_variable_name('correct', ['wrong'])
1413
False
15-
14+
>>> is_wrong_variable_name('__wrong', ['wrong'])
15+
True
1616
"""
17-
return name in to_check
17+
for name_to_check in to_check:
18+
choices_to_check = [
19+
name_to_check,
20+
'_{0}'.format(name_to_check),
21+
'{0}_'.format(name_to_check),
22+
'__{0}'.format(name_to_check),
23+
'{0}__'.format(name_to_check),
24+
]
25+
if name in choices_to_check:
26+
return True
27+
return False
1828

1929

2030
def is_too_short_variable_name(

0 commit comments

Comments
 (0)