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

Move consider-using-augmented-assign to CodeStyle extension #7628

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/data/messages/c/consider-using-augmented-assign/pylintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[MAIN]
load-plugins=pylint.extensions.code_style
enable=consider-using-augmented-assign
6 changes: 6 additions & 0 deletions doc/whatsnew/fragments/3391.extension
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Added ``consider-using-augmented-assign`` check for ``CodeStyle`` extension
which flags ``x = x + 1`` to simplify to ``x += 1``.
This check is disabled by default. To use it, load the code style extension
with ``load-plugins=pylint.extensions.code_style`` and add ``consider-using-augmented-assign`` in the ``enable`` option.

Closes #3391
4 changes: 0 additions & 4 deletions doc/whatsnew/fragments/3391.new_check

This file was deleted.

20 changes: 0 additions & 20 deletions pylint/checkers/refactoring/recommendation_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

from pylint import checkers
from pylint.checkers import utils
from pylint.interfaces import INFERENCE


class RecommendationChecker(checkers.BaseChecker):
Expand Down Expand Up @@ -61,12 +60,6 @@ class RecommendationChecker(checkers.BaseChecker):
"which could potentially be a f-string. The use of f-strings is preferred. "
"Requires Python 3.6 and ``py-version >= 3.6``.",
),
"C0210": (
"Use '%s' to do an augmented assign directly",
"consider-using-augmented-assign",
"Emitted when an assignment is referring to the object that it is assigning "
"to. This can be changed to be an augmented assign.",
),
}

def open(self) -> None:
Expand Down Expand Up @@ -424,16 +417,3 @@ def _detect_replacable_format_call(self, node: nodes.Const) -> None:
line=node.lineno,
col_offset=node.col_offset,
)

@utils.only_required_for_messages("consider-using-augmented-assign")
def visit_assign(self, node: nodes.Assign) -> None:
is_aug, op = utils.is_augmented_assign(node)
if is_aug:
self.add_message(
"consider-using-augmented-assign",
args=f"{op}=",
node=node,
line=node.lineno,
col_offset=node.col_offset,
confidence=INFERENCE,
)
23 changes: 23 additions & 0 deletions pylint/extensions/code_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from pylint.checkers import BaseChecker, utils
from pylint.checkers.utils import only_required_for_messages, safe_infer
from pylint.interfaces import INFERENCE

if TYPE_CHECKING:
from pylint.lint import PyLinter
Expand Down Expand Up @@ -58,6 +59,13 @@ class CodeStyleChecker(BaseChecker):
"both can be combined by using an assignment expression ``:=``. "
"Requires Python 3.8 and ``py-version >= 3.8``.",
),
"R6104": (
"Use '%s' to do an augmented assign directly",
"consider-using-augmented-assign",
"Emitted when an assignment is referring to the object that it is assigning "
"to. This can be changed to be an augmented assign.\n"
"Disabled by default!",
),
}
options = (
(
Expand Down Expand Up @@ -301,6 +309,21 @@ def _check_ignore_assignment_expr_suggestion(
return True
return False

@only_required_for_messages("consider-using-augmented-assign")
def visit_assign(self, node: nodes.Assign) -> None:
is_aug, op = utils.is_augmented_assign(node)
if is_aug:
self.add_message(
"consider-using-augmented-assign",
args=f"{op}=",
node=node,
line=node.lineno,
col_offset=node.col_offset,
confidence=INFERENCE,
)


def register(linter: PyLinter) -> None:
linter.register_checker(CodeStyleChecker(linter))
# Disable some checks by default
linter.disable("consider-using-augmented-assign")
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[MAIN]
load-plugins=pylint.extensions.code_style
enable=consider-using-augmented-assign
6 changes: 6 additions & 0 deletions tests/functional/ext/code_style/cs_default.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""Test default configuration for code-style checker."""
# pylint: disable=invalid-name

# consider-using-augmented-assign is disabled by default
x = 1
x = x + 1
2 changes: 2 additions & 0 deletions tests/functional/ext/code_style/cs_default.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[MAIN]
load-plugins=pylint.extensions.code_style
2 changes: 1 addition & 1 deletion tests/functional/n/no/no_member_augassign.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Tests for no-member in relation to AugAssign operations."""
# pylint: disable=missing-module-docstring, too-few-public-methods, missing-class-docstring, invalid-name, consider-using-augmented-assign
# pylint: disable=missing-module-docstring, too-few-public-methods, missing-class-docstring, invalid-name

# Test for: https://github.com/PyCQA/pylint/issues/4562
class A:
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/u/undefined/undefined_variable_py38.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Tests for undefined variable with assignment expressions"""
# pylint: disable=using-constant-test, expression-not-assigned, consider-using-augmented-assign
# pylint: disable=using-constant-test, expression-not-assigned

# Tests for annotation of variables and potentially undefinition

Expand Down
2 changes: 1 addition & 1 deletion tests/functional/u/use/use_maxsplit_arg.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Emit a message for accessing first/last element of string.split"""
# pylint: disable=line-too-long,missing-docstring,unsubscriptable-object,too-few-public-methods,invalid-name,redefined-builtin,consider-using-augmented-assign
# pylint: disable=line-too-long,missing-docstring,unsubscriptable-object,too-few-public-methods,invalid-name,redefined-builtin

# Test subscripting .split()
get_first = '1,2,3'.split(',')[0] # [use-maxsplit-arg]
Expand Down
2 changes: 0 additions & 2 deletions tests/functional/u/used/used_before_assignment_conditional.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""used-before-assignment cases involving IF conditions"""

# pylint: disable=consider-using-augmented-assign

if 1 + 1 == 2:
x = x + 1 # [used-before-assignment]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
used-before-assignment:6:8:6:9::Using variable 'x' before assignment:HIGH
used-before-assignment:8:3:8:4::Using variable 'y' before assignment:HIGH
used-before-assignment:4:8:4:9::Using variable 'x' before assignment:HIGH
used-before-assignment:6:3:6:4::Using variable 'y' before assignment:HIGH
2 changes: 1 addition & 1 deletion tests/functional/u/used/used_before_assignment_nonlocal.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Check for nonlocal and used-before-assignment"""
# pylint: disable=missing-docstring, unused-variable, too-few-public-methods, consider-using-augmented-assign
# pylint: disable=missing-docstring, unused-variable, too-few-public-methods


def test_ok():
Expand Down