fix(PTH208): add display-only hint for os.listdir - #28027
Conversation
OpenAI Codex assisted with implementation and verification. The contributor reviewed the change before submission.
9882e6d to
39b40ac
Compare
|
We support using AI (i.e., LLMs) while coding. However, AI should not be used to communicate with maintainers. Please read and follow our AI policy when submitting issues and pull requests. |
|
I confirm that I have read the AI policy. However, as a non-native English speaker, I may not have fully understood all the details. Please note that the comments were originally written by me, with AI used solely for language polishing. |
|
Your test plan specifically read as very LLM-generated to me, but I'll take you at your word and reopen this. Thank you for reading the policy. |
ntBre
left a comment
There was a problem hiding this comment.
Thanks. This looks reasonable at a high level, but we still need to check that the diagnostic is enabled and also use the importer infrastructure to ensure that Path is in scope for the fix. I also had a couple of smaller suggestions.
| let mut diagnostic = checker.report_diagnostic(OsListdir, range); | ||
| if let Some(path) = call.arguments.find_argument_value("path", 0) { |
There was a problem hiding this comment.
I think we still need to use report_diagnostic_if_enabled here, or we could report the diagnostic when the rule is disabled. Something like this should work:
| let mut diagnostic = checker.report_diagnostic(OsListdir, range); | |
| if let Some(path) = call.arguments.find_argument_value("path", 0) { | |
| if let Some(mut diagnostic) = checker.report_diagnostic_if_enabled(OsListdir, range) { | |
| if let Some(path) = call.arguments.find_argument_value("path", 0) { |
It also looks like we don't need to return the diagnostic here, I think the match arms are just missing semicolons so that we can drop the Some(diagnostic).
There was a problem hiding this comment.
Got it. I switched to report_diagnostic_if_enabled, and this branch no longer returns Some(diagnostic).
| let mut diagnostic = checker.report_diagnostic(OsListdir, range); | ||
| if let Some(path) = call.arguments.find_argument_value("path", 0) { |
There was a problem hiding this comment.
Can we reuse the find_argument_value call from above? I'm picturing something like:
let path = call.arguments.find_argument_value("path", 0);
if path.is_some_and(|expr| is_file_descriptor(expr, checker.semantic())) {
return;
}then your code from above.
There was a problem hiding this comment.
Sure,I now look up path only once and use it for both the file-descriptor check and the replacement.
| let mut diagnostic = checker.report_diagnostic(OsListdir, range); | ||
| if let Some(path) = call.arguments.find_argument_value("path", 0) { | ||
| diagnostic.set_fix(Fix::display_only_edit(Edit::range_replacement( | ||
| format!("Path({}).iterdir()", checker.locator().slice(path)), |
There was a problem hiding this comment.
We need to make sure Path is actually in scope. See some other rules for examples:
ruff/crates/ruff_linter/src/rules/flake8_use_pathlib/rules/os_mkdir.rs
Lines 105 to 110 in 26f4f73
There was a problem hiding this comment.
Alright – we can also rely on the importer to reuse an existing Path binding, or add the import when needed.
| pub(crate) struct OsListdir; | ||
|
|
||
| impl Violation for OsListdir { | ||
| const FIX_AVAILABILITY: FixAvailability = FixAvailability::Sometimes; |
There was a problem hiding this comment.
We should also add a fix_title method now to customize the hint shown for the diagnostic.
There was a problem hiding this comment.
I've added a custom fix_title method per your suggestion.
|
As one other note, I think we've mosty added pathlib fixes in preview, but that's only required for safe fixes by our versioning policy. I think this display-only fix is fine to add in stable. |
4f4b004 to
39b40ac
Compare
Use the diagnostic-enabled reporting path and the importer when suggesting a pathlib replacement for os.listdir. Add a dedicated fix title and update the snapshot. OpenAI Codex assisted with implementation and verification. I reviewed the changes before submission.
Summary
Adds a display-only suggestion for
PTH208diagnostics with a path argument:The suggestion is intentionally not an automatic fix because
os.listdirreturns strings or bytes, whilePath.iterdir()returnsPathobjects and can change downstream behavior. The existing diagnostic remains unchanged, and file-descriptor calls do not receive the suggestion.Part of #2331.
Verification
INSTA_FORCE_PASS=1 INSTA_UPDATE=always cargo test -p ruff_linter flake8_use_pathlib::tests --lib(34 passed)cargo fmt --all -- --checkgit diff --check