-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[flake8-use-pathlib
] Add autofixes for PTH203
, PTH204
, PTH205
#18922
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 5 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
2cd414f
[`flake8-use-pathlib`] Add autofixes for `PTH203`, `PTH204`, `PTH205`,
chirizxc 15b09c6
add autofixes for `PTH203`, `PTH204`, `PTH205` (x2)
chirizxc 376ae32
chore: add snapshots
chirizxc a6c6c42
fix: add new snapshots, fix typo
chirizxc 92c72a6
fix: clippy
chirizxc 691927d
refactor: simplify and `applicable_edits`
chirizxc 45c8c70
fix: clippy
chirizxc 9c63eac
chore: simplify tests
chirizxc 792e747
chore: simplify tests
chirizxc 7c7dc27
chore: move `applicability` to `if fix_enabled` branch, update snapshots
chirizxc 679d630
fix: fmt
chirizxc c4d3c02
fix snapshot ranges, simplify `check_os_path_get_calls`
chirizxc 965bca1
fix: clippy
chirizxc 30b4a89
chore: update snapshots
chirizxc e027e2b
chore: revert PTH204.py and PTH205.py to initial state
chirizxc f3ba4dc
Merge remote-tracking branch 'origin/main' into feat/autofixes
chirizxc de70bda
fix: CI
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
69 changes: 69 additions & 0 deletions
69
crates/ruff_linter/resources/test/fixtures/flake8_use_pathlib/PTH203.py
ntBre marked this conversation as resolved.
Show resolved
Hide resolved
|
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,12 +1,81 @@ | ||
import pathlib | ||
import os.path | ||
from pathlib import Path | ||
from os.path import getatime | ||
|
||
filename = "filename" | ||
filename1 = __file__ | ||
filename2 = Path("filename") | ||
|
||
|
||
os.path.getatime("filename") | ||
os.path.getatime(b"filename") | ||
os.path.getatime(Path("filename")) | ||
os.path.getatime(__file__) | ||
|
||
os.path.getatime(filename) | ||
os.path.getatime(filename1) | ||
os.path.getatime(filename2) | ||
|
||
os.path.getatime(filename="filename") | ||
os.path.getatime(filename=b"filename") | ||
os.path.getatime(filename=Path("filename")) | ||
os.path.getatime(filename=__file__) | ||
|
||
getatime("filename") | ||
getatime(b"filename") | ||
getatime(Path("filename")) | ||
getatime(__file__) | ||
|
||
getatime(filename="filename") | ||
getatime(filename=b"filename") | ||
getatime(filename=Path("filename")) | ||
getatime(filename=__file__) | ||
|
||
getatime(filename) | ||
getatime(filename1) | ||
getatime(filename2) | ||
|
||
|
||
os.path.getatime( | ||
"filename", # comment | ||
) | ||
|
||
os.path.getatime( | ||
# comment | ||
"filename" | ||
, | ||
# comment | ||
) | ||
|
||
os.path.getatime( | ||
# comment | ||
b"filename" | ||
# comment | ||
) | ||
|
||
os.path.getatime( # comment | ||
Path(__file__) | ||
# comment | ||
) # comment | ||
|
||
getatime( # comment | ||
"filename") | ||
|
||
getatime( # comment | ||
b"filename", | ||
#comment | ||
) | ||
|
||
os.path.getatime("file" + "name") | ||
|
||
getatime \ | ||
\ | ||
\ | ||
( # comment | ||
"filename", | ||
) | ||
|
||
getatime(Path("filename").resolve()) | ||
|
||
os.path.getatime(pathlib.Path("filename")) |
68 changes: 68 additions & 0 deletions
68
crates/ruff_linter/resources/test/fixtures/flake8_use_pathlib/PTH204.py
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,13 +1,81 @@ | ||
import pathlib | ||
import os.path | ||
from pathlib import Path | ||
from os.path import getmtime | ||
|
||
filename = "filename" | ||
filename1 = __file__ | ||
filename2 = Path("filename") | ||
|
||
|
||
os.path.getmtime("filename") | ||
os.path.getmtime(b"filename") | ||
os.path.getmtime(Path("filename")) | ||
os.path.getmtime(__file__) | ||
|
||
os.path.getmtime(filename) | ||
os.path.getmtime(filename1) | ||
os.path.getmtime(filename2) | ||
|
||
os.path.getmtime(filename="filename") | ||
os.path.getmtime(filename=b"filename") | ||
os.path.getmtime(filename=Path("filename")) | ||
os.path.getmtime(filename=__file__) | ||
|
||
getmtime("filename") | ||
getmtime(b"filename") | ||
getmtime(Path("filename")) | ||
getmtime(__file__) | ||
|
||
getmtime(filename="filename") | ||
getmtime(filename=b"filename") | ||
getmtime(filename=Path("filename")) | ||
getmtime(filename=__file__) | ||
|
||
getmtime(filename) | ||
getmtime(filename1) | ||
getmtime(filename2) | ||
|
||
|
||
os.path.getmtime( | ||
"filename", # comment | ||
) | ||
|
||
os.path.getmtime( | ||
# comment | ||
"filename" | ||
, | ||
# comment | ||
) | ||
|
||
os.path.getmtime( | ||
# comment | ||
b"filename" | ||
# comment | ||
) | ||
|
||
os.path.getmtime( # comment | ||
Path(__file__) | ||
# comment | ||
) # comment | ||
|
||
getmtime( # comment | ||
"filename") | ||
|
||
getmtime( # comment | ||
b"filename", | ||
#comment | ||
) | ||
|
||
os.path.getmtime("file" + "name") | ||
|
||
getmtime \ | ||
\ | ||
\ | ||
( # comment | ||
"filename", | ||
) | ||
|
||
getmtime(Path("filename").resolve()) | ||
|
||
os.path.getmtime(pathlib.Path("filename")) |
69 changes: 69 additions & 0 deletions
69
crates/ruff_linter/resources/test/fixtures/flake8_use_pathlib/PTH205.py
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,12 +1,81 @@ | ||
import pathlib | ||
import os.path | ||
from pathlib import Path | ||
from os.path import getctime | ||
|
||
filename = "filename" | ||
filename1 = __file__ | ||
filename2 = Path("filename") | ||
|
||
|
||
os.path.getctime("filename") | ||
os.path.getctime(b"filename") | ||
os.path.getctime(Path("filename")) | ||
os.path.getctime(__file__) | ||
|
||
os.path.getctime(filename) | ||
os.path.getctime(filename1) | ||
os.path.getctime(filename2) | ||
|
||
os.path.getctime(filename="filename") | ||
os.path.getctime(filename=b"filename") | ||
os.path.getctime(filename=Path("filename")) | ||
os.path.getctime(filename=__file__) | ||
|
||
getctime("filename") | ||
getctime(b"filename") | ||
getctime(Path("filename")) | ||
getctime(__file__) | ||
|
||
getctime(filename="filename") | ||
getctime(filename=b"filename") | ||
getctime(filename=Path("filename")) | ||
getctime(filename=__file__) | ||
|
||
getctime(filename) | ||
getctime(filename1) | ||
getctime(filename2) | ||
|
||
|
||
os.path.getctime( | ||
"filename", # comment | ||
) | ||
|
||
os.path.getctime( | ||
# comment | ||
"filename" | ||
, | ||
# comment | ||
) | ||
|
||
os.path.getctime( | ||
# comment | ||
b"filename" | ||
# comment | ||
) | ||
|
||
os.path.getctime( # comment | ||
Path(__file__) | ||
# comment | ||
) # comment | ||
|
||
getctime( # comment | ||
"filename") | ||
|
||
getctime( # comment | ||
b"filename", | ||
#comment | ||
) | ||
|
||
os.path.getctime("file" + "name") | ||
|
||
getctime \ | ||
\ | ||
\ | ||
( # comment | ||
"filename", | ||
) | ||
|
||
getctime(Path("filename").resolve()) | ||
|
||
os.path.getctime(pathlib.Path("filename")) |
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
11 changes: 11 additions & 0 deletions
11
crates/ruff_linter/src/rules/flake8_use_pathlib/helpers.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,11 @@ | ||
use crate::checkers::ast::Checker; | ||
use ruff_python_ast::Expr; | ||
|
||
pub(crate) fn is_path_call(checker: &Checker, expr: &Expr) -> bool { | ||
expr.as_call_expr().is_some_and(|expr_call| { | ||
checker | ||
.semantic() | ||
.resolve_qualified_name(&expr_call.func) | ||
.is_some_and(|name| matches!(name.segments(), ["pathlib", "Path"])) | ||
}) | ||
} |
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
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.