Skip to content

Commit

Permalink
Fix crash in use-maxsplit-arg checker where sep given by keyword (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtylerwalls committed Feb 6, 2022
1 parent 9d3a9c3 commit 3d167ab
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 2 deletions.
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,11 @@ Release date: TBA

Closes #5461

* Fixed crash in ``use-maxsplit-arg`` checker when providing the ``sep`` argument
to ``str.split()`` by keyword.

Closes #5737

* Fix false positive for ``unused-variable`` for a comprehension variable matching
an outer scope type annotation.

Expand Down
5 changes: 5 additions & 0 deletions doc/whatsnew/2.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,11 @@ Other Changes

Closes #5461

* Fixed crash in ``use-maxsplit-arg`` checker when providing the ``sep`` argument
to ``str.split()`` by keyword.

Closes #5737

* Fix false positive for ``unused-variable`` for a comprehension variable matching
an outer scope type annotation.

Expand Down
4 changes: 2 additions & 2 deletions pylint/checkers/refactoring/recommendation_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def _check_use_maxsplit_arg(self, node: nodes.Call) -> None:
return

try:
utils.get_argument_from_call(node, 0, "sep")
sep = utils.get_argument_from_call(node, 0, "sep")
except utils.NoSuchArgumentError:
return

Expand Down Expand Up @@ -154,7 +154,7 @@ def _check_use_maxsplit_arg(self, node: nodes.Call) -> None:
new_name = (
node.func.as_string().rsplit(fn_name, maxsplit=1)[0]
+ new_fn
+ f"({node.args[0].as_string()}, maxsplit=1)[{subscript_value}]"
+ f"({sep.as_string()}, maxsplit=1)[{subscript_value}]"
)
self.add_message("use-maxsplit-arg", node=node, args=(new_name,))

Expand Down
4 changes: 4 additions & 0 deletions tests/functional/u/use/use_maxsplit_arg.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,7 @@ class Bar():
for j in range(5):
print(source.split('.')[i])
i = i + 1

# Test for crash when sep is given by keyword
# https://github.com/PyCQA/pylint/issues/5737
get_last = SEQ.split(sep=None)[-1] # [use-maxsplit-arg]
1 change: 1 addition & 0 deletions tests/functional/u/use/use_maxsplit_arg.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ use-maxsplit-arg:79:6:79:27::Use Bar.split.rsplit(',', maxsplit=1)[-1] instead:U
use-maxsplit-arg:82:4:82:23::Use '1,2,3'.split('\n', maxsplit=1)[0] instead:UNDEFINED
use-maxsplit-arg:83:4:83:26::Use '1,2,3'.rsplit('split', maxsplit=1)[-1] instead:UNDEFINED
use-maxsplit-arg:84:4:84:28::Use '1,2,3'.split('rsplit', maxsplit=1)[0] instead:UNDEFINED
use-maxsplit-arg:96:11:96:30::Use SEQ.rsplit(None, maxsplit=1)[-1] instead:UNDEFINED

0 comments on commit 3d167ab

Please sign in to comment.