-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[flake8-use-pathlib
] Add autofixes for PTH100
, PTH106
, PTH107
, PTH108
, PTH110
, PTH111
, PTH112
, PTH113
, PTH114
, PTH115
, PTH117
, PTH119
, PTH120
#19213
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
cb053b5
refactor: rename `is_path_call` to `check_os_pathlib_single_arg_calls…
chirizxc 2741efc
refactor: make `is_keyword_only_argument_non_default` pub
chirizxc 572c71b
feat: add more autofixes
chirizxc c2705b1
chore: add PR number
chirizxc aa5db5e
fix: benchmark regression
chirizxc 5e946f3
fix: `os.path.basename()` parameter name / clippy
chirizxc f9a300b
fix: tests
chirizxc d794ba9
fix: parameter names
chirizxc c33a2b2
chore: update snapshots
chirizxc b0ea088
fix: benchmark regression
chirizxc 327cafb
chore: add tests, snapshots for preview
chirizxc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
26 changes: 26 additions & 0 deletions
26
crates/ruff_linter/src/rules/flake8_use_pathlib/rules/mod.rs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,45 @@ | ||
pub(crate) use glob_rule::*; | ||
pub(crate) use invalid_pathlib_with_suffix::*; | ||
pub(crate) use os_path_abspath::*; | ||
pub(crate) use os_path_basename::*; | ||
pub(crate) use os_path_dirname::*; | ||
pub(crate) use os_path_exists::*; | ||
pub(crate) use os_path_expanduser::*; | ||
pub(crate) use os_path_getatime::*; | ||
pub(crate) use os_path_getctime::*; | ||
pub(crate) use os_path_getmtime::*; | ||
pub(crate) use os_path_getsize::*; | ||
pub(crate) use os_path_isabs::*; | ||
pub(crate) use os_path_isdir::*; | ||
pub(crate) use os_path_isfile::*; | ||
pub(crate) use os_path_islink::*; | ||
pub(crate) use os_readlink::*; | ||
pub(crate) use os_remove::*; | ||
pub(crate) use os_rmdir::*; | ||
pub(crate) use os_sep_split::*; | ||
pub(crate) use os_unlink::*; | ||
pub(crate) use path_constructor_current_directory::*; | ||
pub(crate) use replaceable_by_pathlib::*; | ||
|
||
mod glob_rule; | ||
mod invalid_pathlib_with_suffix; | ||
mod os_path_abspath; | ||
mod os_path_basename; | ||
mod os_path_dirname; | ||
mod os_path_exists; | ||
mod os_path_expanduser; | ||
mod os_path_getatime; | ||
mod os_path_getctime; | ||
mod os_path_getmtime; | ||
mod os_path_getsize; | ||
mod os_path_isabs; | ||
mod os_path_isdir; | ||
mod os_path_isfile; | ||
mod os_path_islink; | ||
mod os_readlink; | ||
mod os_remove; | ||
mod os_rmdir; | ||
mod os_sep_split; | ||
mod os_unlink; | ||
mod path_constructor_current_directory; | ||
mod replaceable_by_pathlib; |
78 changes: 78 additions & 0 deletions
78
crates/ruff_linter/src/rules/flake8_use_pathlib/rules/os_path_abspath.rs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use crate::checkers::ast::Checker; | ||
use crate::preview::is_fix_os_path_abspath_enabled; | ||
use crate::rules::flake8_use_pathlib::helpers::check_os_pathlib_single_arg_calls; | ||
use crate::{FixAvailability, Violation}; | ||
use ruff_macros::{ViolationMetadata, derive_message_formats}; | ||
use ruff_python_ast::ExprCall; | ||
|
||
/// ## What it does | ||
/// Checks for uses of `os.path.abspath`. | ||
/// | ||
/// ## Why is this bad? | ||
/// `pathlib` offers a high-level API for path manipulation, as compared to | ||
/// the lower-level API offered by `os.path`. When possible, using `Path` object | ||
/// methods such as `Path.resolve()` can improve readability over the `os.path` | ||
/// module's counterparts (e.g., `os.path.abspath()`). | ||
/// | ||
/// ## Examples | ||
/// ```python | ||
/// import os | ||
/// | ||
/// file_path = os.path.abspath("../path/to/file") | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// from pathlib import Path | ||
/// | ||
/// file_path = Path("../path/to/file").resolve() | ||
/// ``` | ||
/// | ||
/// ## Known issues | ||
/// While using `pathlib` can improve the readability and type safety of your code, | ||
/// it can be less performant than the lower-level alternatives that work directly with strings, | ||
/// especially on older versions of Python. | ||
/// | ||
/// ## Fix Safety | ||
/// This rule's fix is marked as unsafe if the replacement would remove comments attached to the original expression. | ||
/// | ||
/// ## References | ||
/// - [Python documentation: `Path.resolve`](https://docs.python.org/3/library/pathlib.html#pathlib.Path.resolve) | ||
/// - [Python documentation: `os.path.abspath`](https://docs.python.org/3/library/os.path.html#os.path.abspath) | ||
/// - [PEP 428 – The pathlib module – object-oriented filesystem paths](https://peps.python.org/pep-0428/) | ||
/// - [Correspondence between `os` and `pathlib`](https://docs.python.org/3/library/pathlib.html#correspondence-to-tools-in-the-os-module) | ||
/// - [Why you should be using pathlib](https://treyhunner.com/2018/12/why-you-should-be-using-pathlib/) | ||
/// - [No really, pathlib is great](https://treyhunner.com/2019/01/no-really-pathlib-is-great/) | ||
#[derive(ViolationMetadata)] | ||
pub(crate) struct OsPathAbspath; | ||
|
||
impl Violation for OsPathAbspath { | ||
const FIX_AVAILABILITY: FixAvailability = FixAvailability::Sometimes; | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
"`os.path.abspath()` should be replaced by `Path.resolve()`".to_string() | ||
} | ||
|
||
fn fix_title(&self) -> Option<String> { | ||
Some("Replace with `Path(...).resolve()`".to_string()) | ||
} | ||
} | ||
|
||
/// PTH100 | ||
pub(crate) fn os_path_abspath(checker: &Checker, call: &ExprCall) { | ||
if checker | ||
.semantic() | ||
.resolve_qualified_name(&call.func) | ||
.is_none_or(|qualified_name| qualified_name.segments() != ["os", "path", "abspath"]) | ||
{ | ||
return; | ||
} | ||
check_os_pathlib_single_arg_calls( | ||
checker, | ||
call, | ||
"resolve()", | ||
"path", | ||
is_fix_os_path_abspath_enabled(checker.settings()), | ||
OsPathAbspath, | ||
); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.