Skip to content

Commit

Permalink
Closes #1921
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolevn committed Jun 21, 2021
1 parent 2c750f9 commit 0f16b83
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Semantic versioning in our case means:
- Fixes `ReassigningVariableToItselfViolation` to not report on `x = (x,)` #1807
- Fixes `ReassigningVariableToItselfViolation` to extract variables
from unary operators #1874
- Fixes that `f'{some:,}'` was considered too complex #1921

### Misc

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
f_double_call_arg = "f'{foo()(arg)}'"
f_single_chained_functions = "f'{f1().f2()}'"

# regression 1921
f_string_comma_format = 'f"Count={count:,}"'


@pytest.mark.parametrize('code', [
regular_string,
Expand Down Expand Up @@ -132,6 +135,7 @@ def test_complex_f_string(assert_errors, parse_ast_tree, code, default_options):
f_true_index,
f_none_index,
f_byte_index,
f_string_comma_format,
])
def test_simple_f_string(assert_errors, parse_ast_tree, code, default_options):
"""Testing that non complex ``f`` strings are allowed."""
Expand Down
13 changes: 7 additions & 6 deletions wemake_python_styleguide/visitors/ast/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,12 @@ def visit_JoinedStr(self, node: ast.JoinedStr) -> None:
TooComplexFormattedStringViolation
"""
self._check_complex_formatted_string(node)

# We don't allow `f` strings by default:
self.add_violation(consistency.FormattedStringViolation(node))
if not isinstance(nodes.get_parent(node), ast.FormattedValue):
# We don't allow `f` strings by default,
# But, we need this condition to make sure that this
# is not a part of complex string format like `f"Count={count:,}"`:
self._check_complex_formatted_string(node)
self.add_violation(consistency.FormattedStringViolation(node))
self.generic_visit(node)

def _check_complex_formatted_string(self, node: ast.JoinedStr) -> None:
Expand Down Expand Up @@ -261,8 +263,7 @@ def _is_valid_chain_structure(self, chained_parts: List[ast.AST]) -> bool:
isinstance(part, self._single_use_types)
for part in chained_parts
) == 1
# All chaining with fewer elements is fine!
return True
return True # All chaining with fewer elements is fine!


@final
Expand Down

0 comments on commit 0f16b83

Please sign in to comment.