[ruff] Handle extra arguments to deque (RUF037)#18614
Merged
Conversation
Contributor
|
MichaReiser
reviewed
Jun 11, 2025
| 65 65 | def f(): | ||
| 66 66 | deque([], *[10]) # RUF037 but no fix | ||
| 67 |- deque([], **{"maxlen": 10}) # RUF037 | ||
| 67 |+ deque(**{"maxlen": 10}) # RUF037 |
Member
There was a problem hiding this comment.
Very nit picky, but I think this is unsafe if the dict contains an iterable keyword. The same if there are any other keywords around:
>>> from collections import deque
>>> a = deque(iterable=[1, 2])
>>> list(a)
[1, 2]
>>> list(a)
KeyboardInterrupt
>>> a = deque([1], iterable=[1, 2])
Traceback (most recent call last):
File "<python-input-6>", line 1, in <module>
a = deque([1], iterable=[1, 2])
TypeError: argument for deque() given by name ('iterable') and position (1)
We should mark the fix as unsafe if there are any other arguments.
Contributor
Author
There was a problem hiding this comment.
Wow nice catch! I added this case:
def f():
deque([], iterable=[])and realized that I was mishandling the range passed to remove_argument because this was returning an Err! I was only passing the ArgOrKeyword::value not the whole ArgOrKeyword.
I fixed that, and then also marked the fix unsafe if we find an unrecognized number of arguments.
MichaReiser
approved these changes
Jun 11, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #18612 by:
*args, which I don't think we can fix reliablyEdit::deletionfromremove_argumentinstead of anEdit::range_replacementin the presence of unrecognized keyword argumentsI thought we could always switch to the
Edit::deletionapproach initially, but it caused problems whenmaxlenwas passed positionally, which we didn't have any existing tests for.The replacement fix can easily delete comments, so I also marked the fix unsafe in these cases and updated the docs accordingly.
Test Plan
New test cases derived from the issue.
Stabilization
These are pretty significant changes, much like those to PYI059 in #18611 (and based a bit on the implementation there!), so I think it probably makes sense to un-stabilize this for the 0.12 release, but I'm open to other thoughts there.