-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[flake8-use-pathlib
] Add autofix for PTH202
#18763
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
Conversation
this not the final version yet, and i still have some questions |
should we remove [ import os
x = os.path.getsize(filename="filename")
y = os.path.getsize(filename=b"filename")
z = os.path.getsize(filename=__file__) => import os # unused
from pathlib import Path # we also have to import it if it wasn't there before
x = Path("filename").stat().st_size
y = Path(b"filename").stat().st_size
z = Path(__file__).stat().st_size |
I don't think you need to remove the imports, you can defer to unused-import (F401) for that, but you do need to add the Let me know if you have more questions or when it's ready for review! |
as well as, for example os.path.getsize(Path("filename")) => Path(Path("filename")).stat().st_size |
The call to Path with parentheses here seems redundant, but I don't think we can consider it unnecessary since it might have additional methods like |
|
code | total | + violation | - violation | + fix | - fix |
---|---|---|---|---|---|
PTH202 | 20 | 0 | 0 | 20 | 0 |
is it worth splitting pr if the changes are the same as this pr? the question is more to this one |
Let's start with one fix. Then the second PR can generalize it and apply it to the other rules. |
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! I have some questions and suggestions here.
crates/ruff_linter/src/rules/flake8_use_pathlib/rules/os_path_getsize.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/flake8_use_pathlib/rules/os_path_getsize.rs
Outdated
Show resolved
Hide resolved
crates/ruff_linter/src/rules/flake8_use_pathlib/rules/os_path_getsize.rs
Outdated
Show resolved
Hide resolved
# Conflicts: # crates/ruff_linter/src/checkers/ast/analyze/expression.rs
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, this looks good. I often forget this, but based on our versioning policy, adding a safe fix actually needs to be done in preview. Could you add a preview method like this and gate the fix behind that?
ruff/crates/ruff_linter/src/preview.rs
Lines 43 to 46 in 833be2e
// https://github.com/astral-sh/ruff/pull/16719 | |
pub(crate) const fn is_fix_manual_dict_comprehension_enabled(settings: &LinterSettings) -> bool { | |
settings.preview.is_enabled() | |
} |
10 10 | os.path.getsize("filename") | ||
11 11 | os.path.getsize(b"filename") | ||
12 |-os.path.getsize(Path("filename")) | ||
12 |+Path(Path("filename")).stat().st_size |
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.
Have you looked into how hard it would be to detect this? I think it would just be a match
on the argument to see if it's a call to Path
. I don't think this is a deal-breaker for this PR, but it is a bit unfortunate, as you pointed out.
I think if you look just for an Expr::Call
, it should avoid your concern about additional attributes like Path(...).resolve()
because those would be Expr::Attribute
s.
should the changes disappear after that in the snapshots? |
|
Yes, you'll need to add a version of the PTH202 test that enables preview mode to see the preview snapshot changes. |
done |
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!
crates/ruff_linter/src/rules/flake8_use_pathlib/rules/os_path_getsize.rs
Outdated
Show resolved
Hide resolved
flake8-use-pathlib
] add autofix for PTH202
flake8-use-pathlib
] Add autofix for PTH202
* main: [ty] Fix false positives when subscripting an object inferred as having an `Intersection` type (#18920) [`flake8-use-pathlib`] Add autofix for `PTH202` (#18763) [ty] Add relative import completion tests [ty] Clarify what "cursor" means [ty] Add a cursor test builder [ty] Enforce sort order of completions (#18917) [formatter] Fix missing blank lines before decorated classes in .pyi files (#18888) Apply fix availability and applicability when adding to `DiagnosticGuard` and remove `NoqaCode::rule` (#18834) py-fuzzer: allow relative executable paths (#18915) [ty] Change `environment.root` to accept multiple paths (#18913) [ty] Rename `src.root` setting to `environment.root` (#18760) Use file path for detecting package root (#18914) Consider virtual path for various server actions (#18910) [ty] Introduce `UnionType::try_from_elements` and `UnionType::try_map` (#18911) [ty] Support narrowing on `isinstance()`/`issubclass()` if the second argument is a dynamic, intersection, union or typevar type (#18900) [ty] Add decorator check for implicit attribute assignments (#18587) [`ruff`] Trigger `RUF037` for empty string and byte strings (#18862) [ty] Avoid duplicate diagnostic in unpacking (#18897) [`pyupgrade`] Extend version detection to include `sys.version_info.major` (`UP036`) (#18633) [`ruff`] Frozen Dataclass default should be valid (`RUF009`) (#18735)
…#18922) <!-- Thank you for contributing to Ruff/ty! To help us out with reviewing, please consider the following: - Does this pull request include a summary of the change? (See below.) - Does this pull request include a descriptive title? (Please prefix with `[ty]` for ty pull requests.) - Does this pull request include references to any relevant issues? --> ## Summary Part of #2331 | [#18763](#18763 (comment)) <!-- What's the purpose of the change? What does it do, and why? --> ## Test Plan update snapshots <!-- How was it tested? -->
Summary
/closes #2331
Test Plan
update snapshots