Skip to content

Commit

Permalink
Version 0.6.3, closes #450
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolevn committed Jan 18, 2019
1 parent 300f257 commit 4a77d81
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 2 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ We used to have incremental versioning before `0.1.0`.
- Adds `safety` and other dependency checks to CI process


## Version 0.6.3

### Bugfixes

- Fixes a [crash](https://github.com/wemake-services/wemake-python-styleguide/issues/450) with `dict`s with just values and no keys


## Version 0.6.2

### Bugfixes
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build-backend = "poetry.masonry.api"

[tool.poetry]
name = "wemake-python-styleguide"
version = "0.6.2"
version = "0.6.3"
description = "The strictest and most opinionated python linter ever"

license = "MIT"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@
}
"""

correct_simple_regression450 = """
was_crashing = {**some_other_dict}
"""

correct_multiline_regression450 = """
was_crashing = {
**some_other_dict,
**some_very_other_dict,
**third_dict,
}
"""


# Wrong:

Expand Down Expand Up @@ -198,6 +210,13 @@
}
"""

wrong_regression450 = """
some_dict = {
**one,
**two, **three,
}
"""


@pytest.mark.parametrize('code', [
correct_multiline_string,
Expand All @@ -214,6 +233,10 @@
correct_next_line_set,
correct_next_line_tuple,
correct_nested_collections,
# Regressions:
correct_simple_regression450,
correct_multiline_regression450,
])
def test_correct_collection_indentation(
assert_errors,
Expand Down Expand Up @@ -247,6 +270,9 @@ def test_correct_collection_indentation(
wrong_dict_indentation2,
wrong_dict_indentation3,
wrong_dict_indentation4,
# Regressions:
wrong_regression450,
])
def test_wrong_collection_indentation(
assert_errors,
Expand Down
31 changes: 31 additions & 0 deletions wemake_python_styleguide/logics/collections.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-

import ast
from typing import List, Sequence


def normalize_dict_elements(node: ast.Dict) -> Sequence[ast.AST]:
"""
Normalizes ``dict`` elements and enforces consistent order.
We had a problem that some ``dict`` objects might not have some keys.
Example::
some_dict = {**one, **two}
This ``dict`` contains two values and zero keys.
This function will normalize this structure to use
values instead of missing keys.
See also:
https://github.com/wemake-services/wemake-python-styleguide/issues/450
"""
elements: List[ast.AST] = []
for dict_key, dict_value in zip(node.keys, node.values):
if dict_key is None:
elements.append(dict_value)
else:
elements.append(dict_key)
return elements
6 changes: 5 additions & 1 deletion wemake_python_styleguide/visitors/ast/statements.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ast
from typing import ClassVar, List, Optional, Sequence, Union

from wemake_python_styleguide.logics.collections import normalize_dict_elements
from wemake_python_styleguide.logics.functions import get_all_arguments
from wemake_python_styleguide.logics.nodes import is_doc_string
from wemake_python_styleguide.types import AnyFunctionDef, AnyNodes, final
Expand Down Expand Up @@ -215,7 +216,10 @@ def _check_indentation(

def visit_collection(self, node: AnyCollection) -> None:
"""Checks how collection items indentation."""
elements = node.keys if isinstance(node, ast.Dict) else node.elts
if isinstance(node, ast.Dict):
elements = normalize_dict_elements(node)
else:
elements = node.elts
self._check_indentation(node, elements, extra_lines=1)
self.generic_visit(node)

Expand Down

0 comments on commit 4a77d81

Please sign in to comment.