Skip to content

Commit

Permalink
Fixes class field cases under other nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolevn committed Mar 22, 2020
1 parent 4f278f5 commit 97ef6cf
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -99,6 +99,7 @@ character combination which is not easy to read
- Fixes `WPS114` not to be so strict
- Fixes `WPS122` not raising for `for` and `async for` definitions
- Fixes `WPS400` raising for `# type: ignore[override]` comments
- Fixes `WPS115` not raising for attributes inside other nodes

### Misc

Expand Down
Expand Up @@ -15,6 +15,12 @@ class Test(object):
{0}: int = None
"""

static_typed_condition_attribute = """
class Test(object):
if sys.version_info > (3, 8):
{0}: int = None
"""

regression423 = """
class MyClass(object):
def action_method(self, request, second):
Expand All @@ -27,6 +33,7 @@ def action_method(self, request, second):
@pytest.mark.parametrize('code', [
static_attribute,
static_typed_attribute,
static_typed_condition_attribute,
])
@pytest.mark.parametrize('non_snake_case_name', [
'Abc',
Expand Down Expand Up @@ -58,6 +65,7 @@ def test_upper_case_class_attributes(
@pytest.mark.parametrize('code', [
static_attribute,
static_typed_attribute,
static_typed_condition_attribute,
])
@pytest.mark.parametrize('snake_case_name', [
'abc',
Expand Down
14 changes: 7 additions & 7 deletions wemake_python_styleguide/visitors/ast/naming.py
Expand Up @@ -110,9 +110,9 @@ def check_function_signature(self, node: AnyFunctionDefAndLambda) -> None:

def check_attribute_name(self, node: ast.ClassDef) -> None:
top_level_assigns = [
sub_node
for sub_node in node.body
if isinstance(sub_node, AssignNodes)
sub
for sub in ast.walk(node)
if isinstance(sub, AssignNodes) and nodes.get_context(sub) is node
]

for assignment in top_level_assigns:
Expand Down Expand Up @@ -167,15 +167,15 @@ def _ensure_complex_naming(
naming.UnreadableNameViolation(node, text=unreadable_sequence),
)

def _ensure_case(self, target: ast.AST) -> None:
if not isinstance(target, ast.Name):
def _ensure_case(self, node: ast.AST) -> None:
if not isinstance(node, ast.Name):
return

if not target.id or not logical.is_upper_case_name(target.id):
if not node.id or not logical.is_upper_case_name(node.id):
return

self._error_callback(
naming.UpperCaseAttributeViolation(target, text=target.id),
naming.UpperCaseAttributeViolation(node, text=node.id),
)


Expand Down

0 comments on commit 97ef6cf

Please sign in to comment.