diff options
author | Jacob Walls <jacobtylerwalls@gmail.com> | 2022-02-06 13:50:54 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-06 19:50:54 +0100 |
commit | 3d167ab01b07a12fa29fac1c076763508e88534b (patch) | |
tree | d8aaf0c4f02ca17b4e061db8ce394a5726e2cf3b | |
parent | 9d3a9c390fcb3ad173a6f204fbfa5221852adca4 (diff) | |
download | pylint-git-3d167ab01b07a12fa29fac1c076763508e88534b.tar.gz |
Fix crash in `use-maxsplit-arg` checker where `sep` given by keyword (#5772)
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | doc/whatsnew/2.13.rst | 5 | ||||
-rw-r--r-- | pylint/checkers/refactoring/recommendation_checker.py | 4 | ||||
-rw-r--r-- | tests/functional/u/use/use_maxsplit_arg.py | 4 | ||||
-rw-r--r-- | tests/functional/u/use/use_maxsplit_arg.txt | 1 |
5 files changed, 17 insertions, 2 deletions
@@ -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. diff --git a/doc/whatsnew/2.13.rst b/doc/whatsnew/2.13.rst index 9d5330c43..7daac870d 100644 --- a/doc/whatsnew/2.13.rst +++ b/doc/whatsnew/2.13.rst @@ -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. diff --git a/pylint/checkers/refactoring/recommendation_checker.py b/pylint/checkers/refactoring/recommendation_checker.py index 785e37fdd..d517cfd1b 100644 --- a/pylint/checkers/refactoring/recommendation_checker.py +++ b/pylint/checkers/refactoring/recommendation_checker.py @@ -113,7 +113,7 @@ class RecommendationChecker(checkers.BaseChecker): return try: - utils.get_argument_from_call(node, 0, "sep") + sep = utils.get_argument_from_call(node, 0, "sep") except utils.NoSuchArgumentError: return @@ -154,7 +154,7 @@ class RecommendationChecker(checkers.BaseChecker): 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,)) diff --git a/tests/functional/u/use/use_maxsplit_arg.py b/tests/functional/u/use/use_maxsplit_arg.py index 396da3adc..d0d43c2b9 100644 --- a/tests/functional/u/use/use_maxsplit_arg.py +++ b/tests/functional/u/use/use_maxsplit_arg.py @@ -90,3 +90,7 @@ i = 0 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]
diff --git a/tests/functional/u/use/use_maxsplit_arg.txt b/tests/functional/u/use/use_maxsplit_arg.txt index d9c108446..a583dfcca 100644 --- a/tests/functional/u/use/use_maxsplit_arg.txt +++ b/tests/functional/u/use/use_maxsplit_arg.txt @@ -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 |