Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow dot and comma to be overused string literals #2210

Merged
merged 13 commits into from
Dec 13, 2021
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Semantic versioning in our case means:
- Now `InconsistentYieldViolation` and `InconsistentReturnViolation` are raised
when `yield` or `return` is used with `None`
where plain version should be used
- Dot `'.'` and comma `','` do not count against string literal overuse limit anymore

### Bugfixes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,20 +200,22 @@ def test_string_type_annotations(
r'"\t"',
r'"\n"',
'""',
'","',
'"."',
])
@pytest.mark.parametrize('prefix', [
'b',
'u',
'',
])
def test_string_overuse_exceptions(
def test_common_strings_allowed(
assert_errors,
parse_ast_tree,
default_options,
prefix,
string_value,
):
"""Ensures that over-used strings raise violations."""
"""Ensures that common strings do not count against the overuse limit."""
snippet = string_actions.format(prefix + string_value)
tree = parse_ast_tree(snippet)

Expand Down
10 changes: 7 additions & 3 deletions wemake_python_styleguide/violations/complexity.py
Original file line number Diff line number Diff line change
Expand Up @@ -851,12 +851,16 @@ class TooManyExceptCasesViolation(ASTViolation):

@final
class OverusedStringViolation(MaybeASTViolation):
"""
Forbid the overuse of string constants.
r"""
Forbid the overuse of string literals.

We allow to use strings without any restrictions as annotations for
variables, arguments, return values, and class attributes.

Some common string literals like dot `'.'`, comma `','`, empty string `''`,
single space `' '`, new line `'\n'`, `'\r\n'` and tabulator `'\t'`
do not count against string literal overuse limit.

Reasoning:
When some string is used more than several time in your code,
it probably means that this string is a meaningful constant
Expand All @@ -875,7 +879,7 @@ class OverusedStringViolation(MaybeASTViolation):

"""

error_template = 'Found string constant over-use: {0}'
error_template = "Found string literal over-use: '{0}'"
code = 226


Expand Down
7 changes: 6 additions & 1 deletion wemake_python_styleguide/visitors/ast/complexity/overuses.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,21 @@ class StringOveruseVisitor(base.BaseNodeVisitor):
Restricts repeated usage of the same string constant.

NB: Some short strings are ignored, as their use is very common and
forcing assignment would not make much sense (i.e. newlines or "").
forcing assignment would not make much sense (i.e. newlines, "",
comma, dot).
"""

_ignored_string_constants: ClassVar[_StringConstants] = frozenset((
' ',
'.',
',',
'',
'\n',
'\r\n',
'\t',
b' ',
b'.',
b',',
b'',
b'\n',
b'\r\n',
Expand Down