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

Fix crash in use-maxsplit-arg checker where sep given by keyword #5772

Merged
merged 1 commit into from
Feb 6, 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
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,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 @@ -275,6 +275,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