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

Do not count -1 as an overused expression #2500

Merged
merged 1 commit into from Sep 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 7 additions & 4 deletions CHANGELOG.md
Expand Up @@ -21,9 +21,16 @@ Semantic versioning in our case means:

### Features

- **Breaking**: drops `python3.6` support
- Adds `__init_subclass__` in the beginning of accepted methods
order as per WPS338 #2411

### Bugfixes

- Fixes `WPS226` false positives on `|` use in `SomeType | AnotherType`
type hints syntax
- Now `-1` is not reported to be an overused expression

### Misc

- Adds full violation codes to docs and `BaseViolation.full_code` #2409
Expand All @@ -32,10 +39,6 @@ Semantic versioning in our case means:
- Domain name was changed from `wemake-python-stylegui.de`
to `wemake-python-styleguide.rtfd.io`

### Bugfixes

- Fixes `WPS226` false positives on `|` use in `SomeType | AnotherType` type hints syntax


## 0.16.1

Expand Down
Expand Up @@ -134,8 +134,8 @@ def method2(self, arg2: "List[int]") -> 'type.Any':
'{a for a in other}',

# Unary nodes:
'-1', # unary minus operator is raising when used with literal
'+some', # unary plus always raises
'~other',

# Special cases for self:
'self.method([star])', # `[star]` is raising
Expand Down Expand Up @@ -169,6 +169,8 @@ def method2(self, arg2: "List[int]") -> 'type.Any':
'some.prop',
# unary operator
'-value',
'-value.attr',
'-1',
)


Expand Down
12 changes: 6 additions & 6 deletions wemake_python_styleguide/logic/complexity/overuses.py
Expand Up @@ -102,12 +102,12 @@ def is_unary_minus(node: ast.AST) -> bool:
We use this predicate to allow values
like ``-some_value`` to be overused.

Although negative constants like ``-1``
Although negative constants like ``-5``
should raise violation to force naming them.
"""
if isinstance(node, ast.UnaryOp):
return (
isinstance(node.op, ast.USub) and
not isinstance(node.operand, (Constant, ast.Num))
)
if isinstance(node, ast.UnaryOp) and isinstance(node.op, ast.USub):
# We allow variables, attributes, subscripts, and `-1`
if isinstance(node.operand, (Constant, ast.Num)):
return node.operand.n == 1
return True
return False