[refurb] Extend support for Path.open (FURB101, FURB103)#21080
[refurb] Extend support for Path.open (FURB101, FURB103)#21080ntBre merged 14 commits intoastral-sh:mainfrom
refurb] Extend support for Path.open (FURB101, FURB103)#21080Conversation
|
| code | total | + violation | - violation | + fix | - fix |
|---|---|---|---|---|---|
| FURB103 | 8 | 8 | 0 | 0 | 0 |
| FURB101 | 7 | 7 | 0 | 0 | 0 |
|
@MichaReiser would appreciate your feedback on this whenever you get some time. Thank you : ) |
ntBre
left a comment
There was a problem hiding this comment.
Thanks for working on this!
I think we need to be a bit more careful with how we identify existing Path instances and hopefully we can deduplicate some code too.
...rc/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__preview_FURB103_FURB103.py.snap
Show resolved
Hide resolved
...rc/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__preview_FURB103_FURB103.py.snap
Outdated
Show resolved
Hide resolved
|
It might be helpful to reuse |
|
Alternately PathlibPathChecker which can also match annotations: |
072bb07 to
7ab4212
Compare
|
the common helper has too many arguments causing the clippy error, should I break it down ? |
I think it's okay just to add |
| } | ||
| let attr = func.as_attribute_expr()?; | ||
| let path_call = attr.value.as_call_expr()?; | ||
| let filename = path_call.arguments.args.first()?; |
There was a problem hiding this comment.
Are we missing some of the argument validation code from find_file_open, especially these lines:
if args.iter().any(Expr::is_starred_expr)
|| keywords.iter().any(|keyword| keyword.arg.is_none())
{
return None;
}I also think we're doing too much inspection on the attr.value. For example, I think is_open_call_from_pathlib should be able to identify a case like this:
p = Path("file.txt")
with p.open("r") as f:
f.write("test")but the current implementation expects a literal Path() call, not a name that resolves to a Path.
We might need to make the filename field optional on the FileOpen struct.
There was a problem hiding this comment.
done , also the message is formatted accordingly but i feel its getting nested & dosent feel good , not sure although
There was a problem hiding this comment.
also I have not added any comments yet , should i add similar comments like find_file_open
|
Would you mind rebasing your PR? Doing so will allow us to merge your PR without delayed once reviewed. |
Signed-off-by: 11happy <soni5happy@gmail.com>
…ons with qualified name resolution Signed-off-by: 11happy <soni5happy@gmail.com>
Signed-off-by: 11happy <soni5happy@gmail.com>
Signed-off-by: 11happy <soni5happy@gmail.com>
b5a8638 to
c3a703c
Compare
Signed-off-by: 11happy <soni5happy@gmail.com>
Signed-off-by: 11happy <soni5happy@gmail.com>
Signed-off-by: 11happy <soni5happy@gmail.com>
|
done rebased it & resolved merge conflicts : ) |
ntBre
left a comment
There was a problem hiding this comment.
Thanks for rebasing and for your work on this! I pushed a commit fixing a few nits but then noticed a few more minor issues. The implementation feels a bit complicated to me now, I want to keep thinking about it.
...nter/src/rules/refurb/snapshots/ruff_linter__rules__refurb__tests__FURB103_FURB103_1.py.snap
Show resolved
Hide resolved
Signed-off-by: 11happy <soni5happy@gmail.com>
I agree with you implementation is complex ,felt the same, would appreciate any pointers to decomplicate/refactor it ? |
|
I just pushed a commit trying out an I also fixed some new merge conflicts while I was here. I also noticed that we haven't updated the code for FURB102, even though it's quite similar to FURB103. Do you want to do that in a follow-up PR? |
Thank you for these updates, will go through the commit in detail & will share my thoughts, I am also interested to work on the follow up PR for FURB102 , I feel a little occupied this week but will update this PR & my other open PRs soon. |
Signed-off-by: 11happy <bhuminjaysoni@gmail.com>
Yes, with the new OpenArgument enum code is a lot cleaner, what do you think of using |
ntBre
left a comment
There was a problem hiding this comment.
Thank you! The code for FURB103 looks good to me now, your last commit was a nice improvement :)
Based on the ecosystem report, this is already affecting FURB101 too (I had the wrong rule code when I mentioned FURB102 earlier). Would you mind going ahead and updating the FURB101 code and adding a test for it? I don't think it makes as much sense to save that for a follow-up if we already changed the rule's behavior here.
I think it's mostly just the message, fix_title, and filename_code that need to be updated, most of the logic is already in the shared helpers.
Signed-off-by: 11happy <bhuminjaysoni@gmail.com>
Signed-off-by: 11happy <bhuminjaysoni@gmail.com>
done, to keep it consistent with FURB103 separated the tests for FURB101, also should I add more tests ? |
ntBre
left a comment
There was a problem hiding this comment.
Thank you! I think this is enough testing for FURB101 since most of the tricky code is shared with FURB103.
refurb] Extend support for Path.open (FURB101, FURB103)
Summary
This PR fixes #18409
Test Plan
I have added tests in FURB103.