Skip to content

Commit ae05982

Browse files
authored
Issue 3310: _SELF name should not trigger WPS117 (wemake-services#3335)
1 parent 2c4b96e commit ae05982

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ This version introduces `wps` CLI tool.
3131
- Fixes `WPS432`, now it shows literal num, #1402
3232
- Fixes `WPS226`, now it points to the first string literal occurrence, #3267
3333
- Fixes `WPS605` false-positive on `@staticmethod`, #3292
34+
- Fixes `_SELF` name should not trigger `WPS117`, #3310
3435

3536
## 1.0.0
3637

tests/test_visitors/test_ast/test_naming/conftest.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,28 @@ def container():
273273
),
274274
)
275275

276+
_VARIABLES = (
277+
frozenset(
278+
(
279+
variable_def,
280+
variable_typed_def,
281+
variable_typed,
282+
unpacking_variables,
283+
unpacking_star_variables,
284+
for_variable,
285+
for_star_variable,
286+
with_variable,
287+
with_star_variable,
288+
exception,
289+
match_variable,
290+
match_as_explicit,
291+
match_inner,
292+
match_star,
293+
),
294+
)
295+
| _FOREIGN_NAMING_PATTERNS
296+
)
297+
276298
_ATTRIBUTES = (
277299
frozenset(
278300
(
@@ -391,3 +413,9 @@ def factory(template: str, var_name: str) -> None:
391413
pytest.skip('"_" cannot be used as "case" target')
392414

393415
return factory
416+
417+
418+
@pytest.fixture(params=_VARIABLES)
419+
def simple_variables_template(request):
420+
"""Parametrized fixture that contains all variable naming templates."""
421+
return request.param

tests/test_visitors/test_ast/test_naming/test_naming_rules/test_reserved_argument.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from wemake_python_styleguide.constants import SPECIAL_ARGUMENT_NAMES_WHITELIST
66
from wemake_python_styleguide.violations.naming import (
77
ReservedArgumentNameViolation,
8+
TrailingUnderscoreViolation,
89
)
910
from wemake_python_styleguide.visitors.ast.naming.validation import (
1011
WrongNameVisitor,
@@ -75,3 +76,40 @@ def test_correct_argument_name(
7576
visitor.run()
7677

7778
assert_errors(visitor, [])
79+
80+
81+
@pytest.mark.parametrize(
82+
'variable_name',
83+
[
84+
'_SELF',
85+
'_Self',
86+
'Self',
87+
'_CLS',
88+
'cLs',
89+
'_clS',
90+
'_MCS',
91+
'mcS',
92+
'_mCs',
93+
'__self__',
94+
'__cls__',
95+
'__mcs__',
96+
'self_',
97+
'cls_',
98+
'mcs_',
99+
],
100+
)
101+
def test_reserved_argument_name_variations(
102+
assert_errors,
103+
parse_ast_tree,
104+
default_options,
105+
simple_variables_template,
106+
mode,
107+
variable_name,
108+
):
109+
"""Ensures variations of special names are allowed for simple variables."""
110+
tree = parse_ast_tree(mode(simple_variables_template.format(variable_name)))
111+
112+
visitor = WrongNameVisitor(default_options, tree=tree)
113+
visitor.run()
114+
115+
assert_errors(visitor, [], ignored_types=TrailingUnderscoreViolation)

wemake_python_styleguide/visitors/ast/naming/validation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def _ensure_reserved_name(
113113
if is_first_argument:
114114
return
115115

116-
if not logical.is_wrong_name(name, SPECIAL_ARGUMENT_NAMES_WHITELIST):
116+
if name not in SPECIAL_ARGUMENT_NAMES_WHITELIST:
117117
return
118118

119119
self._error_callback(

0 commit comments

Comments
 (0)