Skip to content

Commit

Permalink
Changes from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
yushao2 committed May 19, 2021
1 parent 90b9b8d commit 91e443b
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 13 deletions.
2 changes: 1 addition & 1 deletion ChangeLog
Expand Up @@ -51,7 +51,7 @@ modules are added.

Closes #4412

* New checker ``consider-using-str-partition``. Emitted either when only accessing the first or last
* New checker ``consider-using-str-partition``. Emitted either when accessing only the first or last
element of ``str.split()``.

Closes #4440
Expand Down
2 changes: 1 addition & 1 deletion doc/whatsnew/2.9.rst
Expand Up @@ -16,7 +16,7 @@ New checkers
indexing the same dictionary with the key within loop body.


* ``consider-using-str-partition``: Emitted either when only accessing the first or last
* ``consider-using-str-partition``: Emitted either when accessing only the first or last
element of ``str.split()``.

* An ``ignore_signatures`` option has been added to the similarity checker. It will permits to reduce false positives when multiple functions have the same parameters.
Expand Down
13 changes: 2 additions & 11 deletions pylint/checkers/refactoring/recommendation_checker.py
Expand Up @@ -82,14 +82,9 @@ def _check_consider_using_str_partition(self, node: astroid.Call) -> None:
# Check if call is split() or rsplit()
if (
isinstance(node.func, astroid.Attribute)
and node.func.attrname
in (
"split",
"rsplit",
)
and node.func.attrname in ("split", "rsplit")
and isinstance(utils.safe_infer(node.func), astroid.BoundMethod)
):

try:
seperator = utils.get_argument_from_call(node, 0, "sep").value
except utils.NoSuchArgumentError:
Expand Down Expand Up @@ -117,12 +112,8 @@ def _check_consider_using_str_partition(self, node: astroid.Call) -> None:

elif maxsplit == 0 and subscript_value in (-1, 0):
fn_name = node.func.attrname
node_name = node.as_string()
new_fn = "rpartition" if subscript_value == -1 else "partition"
new_fn = new_fn[::-1]
node_name = node_name[::-1]
fn_name = fn_name[::-1]
new_name = f"{node_name.replace(fn_name, new_fn, 1)[::-1]}[{subscript_value}]"
new_name = f"{new_fn.join(node.as_string().rsplit(fn_name, maxsplit=1))}[{subscript_value}]"

else:
return
Expand Down

0 comments on commit 91e443b

Please sign in to comment.