[refurb] Restrict delete-full-slice to lists (FURB131) - #27711
Conversation
ntBre
left a comment
There was a problem hiding this comment.
Thanks, this looks good to me overall. I just had a few minor suggestions.
| /// ## References | ||
| /// - [Python documentation: Mutable Sequence Types](https://docs.python.org/3/library/stdtypes.html?highlight=list#mutable-sequence-types) | ||
| /// - [Python documentation: `dict.clear()`](https://docs.python.org/3/library/stdtypes.html?highlight=list#dict.clear) | ||
| /// - [Python documentation: `list.clear()`](https://docs.python.org/3/library/stdtypes.html?highlight=list#mutable-sequence-types) |
There was a problem hiding this comment.
Did you intentionally link to this? I think this is the closer one:
| /// - [Python documentation: `list.clear()`](https://docs.python.org/3/library/stdtypes.html?highlight=list#mutable-sequence-types) | |
| /// - [Python documentation: `list.clear()`](https://docs.python.org/3/library/stdtypes.html?highlight=list#sequence.clear) |
I tried list.clear first, but that redirects to this sequence.clear node.
There was a problem hiding this comment.
My bad, I just used something available upstairs, and opened it and read something that made sense to me. I don't remember what anymore :/
| // It should only apply to variables that are known to be lists. `del d[:]` on a dict | ||
| // raises `KeyError` at runtime, so the rule must not apply to dicts. |
There was a problem hiding this comment.
I don't think this comment will actually be helpful to future readers since from their perspective the rule doesn't apply to dicts anymore.
| // It should only apply to variables that are known to be lists. `del d[:]` on a dict | |
| // raises `KeyError` at runtime, so the rule must not apply to dicts. | |
| // It should only apply to variables that are known to be lists. |
| # `del d[:]` on a dict raises `KeyError` at runtime, so FURB131 must not apply to | ||
| # dicts. Regression coverage: these dict full-slice deletions must NOT be flagged. | ||
| del names[:] | ||
| del y, names[:], x | ||
|
|
||
|
|
||
| def no_dict_one(x: dict[int, str]): | ||
| # not FURB131 |
There was a problem hiding this comment.
I don't think we need to preserve these tests. As I mentioned in my other comment, I think it will just confuse future readers who aren't going to expect the rule to apply to dicts. If we do want to preserve dict tests, I would actually avoid modifying this file at all, which will make the removed diagnostics much clearer in the snapshot diff.
So I would either revert changes to this file or completely drop dict tests, not move the dict tests down here.
There was a problem hiding this comment.
No, you're right. I often sometimes overdocument previous behaviors, but isn't needed. Accepted, thank you!
refurb] Restrict delete-full-slice to lists (FURB131)
|
`del d[:]` on a dict raises `KeyError` at runtime, so suggesting `dict.clear()` as an equivalent replacement is incorrect. Restrict the rule to lists only, matching the upstream Refurb behavior. Update the rule documentation and the fixture accordingly. The dict cases are kept in the fixture as regression coverage to confirm they are no longer flagged, while the list cases remain flagged.
80fea25 to
c9fbfdc
Compare
…sh#27711) Fixes astral-sh#27216 Co-authored-by: Brent Westbrook <[email protected]>
Fixes #27216