-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Extend support for Path.Open in FURB103 #21080
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
base: main
Are you sure you want to change the base?
Conversation
|
| 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 <[email protected]>
…ons with qualified name resolution Signed-off-by: 11happy <[email protected]>
Signed-off-by: 11happy <[email protected]>
Signed-off-by: 11happy <[email protected]>
b5a8638 to
c3a703c
Compare
Signed-off-by: 11happy <[email protected]>
Signed-off-by: 11happy <[email protected]>
Signed-off-by: 11happy <[email protected]>
|
done rebased it & resolved merge conflicts : ) |
ntBre
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 <[email protected]>
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? |
Summary
This PR fixes #18409
Test Plan
I have added tests in FURB103.