diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_use_pathlib/PTH202.py b/crates/ruff_linter/resources/test/fixtures/flake8_use_pathlib/PTH202.py index ca0bd47a6b931..1379cdf20300e 100644 --- a/crates/ruff_linter/resources/test/fixtures/flake8_use_pathlib/PTH202.py +++ b/crates/ruff_linter/resources/test/fixtures/flake8_use_pathlib/PTH202.py @@ -2,13 +2,81 @@ from pathlib import Path from os.path import getsize +filename = "filename" +filename1 = __file__ +filename2 = Path("filename") + os.path.getsize("filename") os.path.getsize(b"filename") os.path.getsize(Path("filename")) os.path.getsize(__file__) +os.path.getsize(filename) +os.path.getsize(filename1) +os.path.getsize(filename2) + +os.path.getsize(filename="filename") +os.path.getsize(filename=b"filename") +os.path.getsize(filename=Path("filename")) +os.path.getsize(filename=__file__) + getsize("filename") getsize(b"filename") getsize(Path("filename")) getsize(__file__) + +getsize(filename="filename") +getsize(filename=b"filename") +getsize(filename=Path("filename")) +getsize(filename=__file__) + +getsize(filename) +getsize(filename1) +getsize(filename2) + + +os.path.getsize( + "filename", # comment +) + +os.path.getsize( + # comment + "filename" + , + # comment +) + +os.path.getsize( + # comment + b"filename" + # comment +) + +os.path.getsize( # comment + Path(__file__) + # comment +) # comment + +getsize( # comment + "filename") + +getsize( # comment + b"filename", + #comment +) + +os.path.getsize("file" + "name") + +getsize \ +\ +\ + ( # comment + "filename", + ) + +getsize(Path("filename").resolve()) + +import pathlib + +os.path.getsize(pathlib.Path("filename")) \ No newline at end of file diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_use_pathlib/PTH202_2.py b/crates/ruff_linter/resources/test/fixtures/flake8_use_pathlib/PTH202_2.py new file mode 100644 index 0000000000000..1544c791aa42c --- /dev/null +++ b/crates/ruff_linter/resources/test/fixtures/flake8_use_pathlib/PTH202_2.py @@ -0,0 +1,5 @@ +import os + +os.path.getsize(filename="filename") +os.path.getsize(filename=b"filename") +os.path.getsize(filename=__file__) diff --git a/crates/ruff_linter/src/checkers/ast/analyze/expression.rs b/crates/ruff_linter/src/checkers/ast/analyze/expression.rs index 51c88aea87a98..7076f5784c713 100644 --- a/crates/ruff_linter/src/checkers/ast/analyze/expression.rs +++ b/crates/ruff_linter/src/checkers/ast/analyze/expression.rs @@ -1056,7 +1056,6 @@ pub(crate) fn expression(expr: &Expr, checker: &Checker) { Rule::OsPathSplitext, Rule::BuiltinOpen, Rule::PyPath, - Rule::OsPathGetsize, Rule::OsPathGetatime, Rule::OsPathGetmtime, Rule::OsPathGetctime, @@ -1066,6 +1065,9 @@ pub(crate) fn expression(expr: &Expr, checker: &Checker) { ]) { flake8_use_pathlib::rules::replaceable_by_pathlib(checker, call); } + if checker.is_rule_enabled(Rule::OsPathGetsize) { + flake8_use_pathlib::rules::os_path_getsize(checker, call); + } if checker.is_rule_enabled(Rule::PathConstructorCurrentDirectory) { flake8_use_pathlib::rules::path_constructor_current_directory(checker, call); } diff --git a/crates/ruff_linter/src/preview.rs b/crates/ruff_linter/src/preview.rs index 8640eac1a1812..89efe7db19211 100644 --- a/crates/ruff_linter/src/preview.rs +++ b/crates/ruff_linter/src/preview.rs @@ -50,6 +50,11 @@ pub(crate) const fn is_fix_manual_list_comprehension_enabled(settings: &LinterSe settings.preview.is_enabled() } +// https://github.com/astral-sh/ruff/pull/18763 +pub(crate) const fn is_fix_os_path_getsize_enabled(settings: &LinterSettings) -> bool { + settings.preview.is_enabled() +} + // https://github.com/astral-sh/ruff/pull/11436 // https://github.com/astral-sh/ruff/pull/11168 pub(crate) const fn is_dunder_init_fix_unused_import_enabled(settings: &LinterSettings) -> bool { diff --git a/crates/ruff_linter/src/rules/flake8_use_pathlib/mod.rs b/crates/ruff_linter/src/rules/flake8_use_pathlib/mod.rs index 6a3c003f54333..7fbc92e410ecf 100644 --- a/crates/ruff_linter/src/rules/flake8_use_pathlib/mod.rs +++ b/crates/ruff_linter/src/rules/flake8_use_pathlib/mod.rs @@ -12,6 +12,7 @@ mod tests { use crate::assert_diagnostics; use crate::registry::Rule; use crate::settings; + use crate::settings::types::PreviewMode; use crate::test::test_path; #[test_case(Path::new("full_name.py"))] @@ -58,6 +59,7 @@ mod tests { #[test_case(Rule::PyPath, Path::new("py_path_2.py"))] #[test_case(Rule::PathConstructorCurrentDirectory, Path::new("PTH201.py"))] #[test_case(Rule::OsPathGetsize, Path::new("PTH202.py"))] + #[test_case(Rule::OsPathGetsize, Path::new("PTH202_2.py"))] #[test_case(Rule::OsPathGetatime, Path::new("PTH203.py"))] #[test_case(Rule::OsPathGetmtime, Path::new("PTH204.py"))] #[test_case(Rule::OsPathGetctime, Path::new("PTH205.py"))] @@ -76,4 +78,23 @@ mod tests { assert_diagnostics!(snapshot, diagnostics); Ok(()) } + + #[test_case(Rule::OsPathGetsize, Path::new("PTH202.py"))] + #[test_case(Rule::OsPathGetsize, Path::new("PTH202_2.py"))] + fn preview_flake8_use_pathlib(rule_code: Rule, path: &Path) -> Result<()> { + let snapshot = format!( + "preview__{}_{}", + rule_code.noqa_code(), + path.to_string_lossy() + ); + let diagnostics = test_path( + Path::new("flake8_use_pathlib").join(path).as_path(), + &settings::LinterSettings { + preview: PreviewMode::Enabled, + ..settings::LinterSettings::for_rule(rule_code) + }, + )?; + assert_diagnostics!(snapshot, diagnostics); + Ok(()) + } } diff --git a/crates/ruff_linter/src/rules/flake8_use_pathlib/rules/os_path_getsize.rs b/crates/ruff_linter/src/rules/flake8_use_pathlib/rules/os_path_getsize.rs index 5dd42507a0f81..e801e13a7c13e 100644 --- a/crates/ruff_linter/src/rules/flake8_use_pathlib/rules/os_path_getsize.rs +++ b/crates/ruff_linter/src/rules/flake8_use_pathlib/rules/os_path_getsize.rs @@ -1,6 +1,11 @@ +use crate::checkers::ast::Checker; +use crate::importer::ImportRequest; +use crate::preview::is_fix_os_path_getsize_enabled; +use crate::{Applicability, Edit, Fix, FixAvailability, Violation}; use ruff_macros::{ViolationMetadata, derive_message_formats}; - -use crate::Violation; +use ruff_python_ast::name::QualifiedName; +use ruff_python_ast::{Expr, ExprCall}; +use ruff_text_size::Ranged; /// ## What it does /// Checks for uses of `os.path.getsize`. @@ -32,6 +37,9 @@ use crate::Violation; /// 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.stat`](https://docs.python.org/3/library/pathlib.html#pathlib.Path.stat) /// - [Python documentation: `os.path.getsize`](https://docs.python.org/3/library/os.path.html#os.path.getsize) @@ -43,8 +51,77 @@ use crate::Violation; pub(crate) struct OsPathGetsize; impl Violation for OsPathGetsize { + const FIX_AVAILABILITY: FixAvailability = FixAvailability::Sometimes; + #[derive_message_formats] fn message(&self) -> String { "`os.path.getsize` should be replaced by `Path.stat().st_size`".to_string() } + + fn fix_title(&self) -> Option { + Some("Replace with `Path(...).stat().st_size`".to_string()) + } +} + +/// PTH202 +pub(crate) fn os_path_getsize(checker: &Checker, call: &ExprCall) { + if !matches!( + checker + .semantic() + .resolve_qualified_name(&call.func) + .as_ref() + .map(QualifiedName::segments), + Some(["os", "path", "getsize"]) + ) { + return; + } + + if call.arguments.len() != 1 { + return; + } + + let Some(arg) = call.arguments.find_argument_value("filename", 0) else { + return; + }; + + let arg_code = checker.locator().slice(arg.range()); + let range = call.range(); + + let applicability = if checker.comment_ranges().intersects(range) { + Applicability::Unsafe + } else { + Applicability::Safe + }; + + let mut diagnostic = checker.report_diagnostic(OsPathGetsize, range); + + if is_fix_os_path_getsize_enabled(checker.settings()) { + diagnostic.try_set_fix(|| { + let (import_edit, binding) = checker.importer().get_or_import_symbol( + &ImportRequest::import("pathlib", "Path"), + call.start(), + checker.semantic(), + )?; + + let replacement = if is_path_call(checker, arg) { + format!("{arg_code}.stat().st_size") + } else { + format!("{binding}({arg_code}).stat().st_size") + }; + + Ok( + Fix::safe_edits(Edit::range_replacement(replacement, range), [import_edit]) + .with_applicability(applicability), + ) + }); + } +} + +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"])) + }) } diff --git a/crates/ruff_linter/src/rules/flake8_use_pathlib/rules/replaceable_by_pathlib.rs b/crates/ruff_linter/src/rules/flake8_use_pathlib/rules/replaceable_by_pathlib.rs index ec64457faeb50..6fdf05b902273 100644 --- a/crates/ruff_linter/src/rules/flake8_use_pathlib/rules/replaceable_by_pathlib.rs +++ b/crates/ruff_linter/src/rules/flake8_use_pathlib/rules/replaceable_by_pathlib.rs @@ -5,7 +5,7 @@ use ruff_text_size::Ranged; use crate::checkers::ast::Checker; use crate::rules::flake8_use_pathlib::rules::{ - Glob, OsPathGetatime, OsPathGetctime, OsPathGetmtime, OsPathGetsize, + Glob, OsPathGetatime, OsPathGetctime, OsPathGetmtime, }; use crate::rules::flake8_use_pathlib::violations::{ BuiltinOpen, Joiner, OsChmod, OsGetcwd, OsListdir, OsMakedirs, OsMkdir, OsPathAbspath, @@ -194,8 +194,6 @@ pub(crate) fn replaceable_by_pathlib(checker: &Checker, call: &ExprCall) { ["os", "path", "samefile"] => checker.report_diagnostic_if_enabled(OsPathSamefile, range), // PTH122 ["os", "path", "splitext"] => checker.report_diagnostic_if_enabled(OsPathSplitext, range), - // PTH202 - ["os", "path", "getsize"] => checker.report_diagnostic_if_enabled(OsPathGetsize, range), // PTH203 ["os", "path", "getatime"] => checker.report_diagnostic_if_enabled(OsPathGetatime, range), // PTH204 diff --git a/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__PTH202_PTH202.py.snap b/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__PTH202_PTH202.py.snap index 4f2fd0ce76387..d4d6fa2c9db76 100644 --- a/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__PTH202_PTH202.py.snap +++ b/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__PTH202_PTH202.py.snap @@ -1,74 +1,357 @@ --- source: crates/ruff_linter/src/rules/flake8_use_pathlib/mod.rs --- -PTH202.py:6:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` - | -6 | os.path.getsize("filename") - | ^^^^^^^^^^^^^^^ PTH202 -7 | os.path.getsize(b"filename") -8 | os.path.getsize(Path("filename")) - | - -PTH202.py:7:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` - | -6 | os.path.getsize("filename") -7 | os.path.getsize(b"filename") - | ^^^^^^^^^^^^^^^ PTH202 -8 | os.path.getsize(Path("filename")) -9 | os.path.getsize(__file__) - | - -PTH202.py:8:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` - | -6 | os.path.getsize("filename") -7 | os.path.getsize(b"filename") -8 | os.path.getsize(Path("filename")) - | ^^^^^^^^^^^^^^^ PTH202 -9 | os.path.getsize(__file__) - | - -PTH202.py:9:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` - | - 7 | os.path.getsize(b"filename") - 8 | os.path.getsize(Path("filename")) - 9 | os.path.getsize(__file__) - | ^^^^^^^^^^^^^^^ PTH202 -10 | -11 | getsize("filename") +PTH202.py:10:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` | +10 | os.path.getsize("filename") + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 +11 | os.path.getsize(b"filename") +12 | os.path.getsize(Path("filename")) + | + = help: Replace with `Path(...).stat().st_size` PTH202.py:11:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` | - 9 | os.path.getsize(__file__) -10 | -11 | getsize("filename") - | ^^^^^^^ PTH202 -12 | getsize(b"filename") -13 | getsize(Path("filename")) +10 | os.path.getsize("filename") +11 | os.path.getsize(b"filename") + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 +12 | os.path.getsize(Path("filename")) +13 | os.path.getsize(__file__) | + = help: Replace with `Path(...).stat().st_size` PTH202.py:12:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` | -11 | getsize("filename") -12 | getsize(b"filename") - | ^^^^^^^ PTH202 -13 | getsize(Path("filename")) -14 | getsize(__file__) +10 | os.path.getsize("filename") +11 | os.path.getsize(b"filename") +12 | os.path.getsize(Path("filename")) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 +13 | os.path.getsize(__file__) | + = help: Replace with `Path(...).stat().st_size` PTH202.py:13:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` | -11 | getsize("filename") -12 | getsize(b"filename") -13 | getsize(Path("filename")) - | ^^^^^^^ PTH202 -14 | getsize(__file__) +11 | os.path.getsize(b"filename") +12 | os.path.getsize(Path("filename")) +13 | os.path.getsize(__file__) + | ^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 +14 | +15 | os.path.getsize(filename) + | + = help: Replace with `Path(...).stat().st_size` + +PTH202.py:15:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` + | +13 | os.path.getsize(__file__) +14 | +15 | os.path.getsize(filename) + | ^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 +16 | os.path.getsize(filename1) +17 | os.path.getsize(filename2) + | + = help: Replace with `Path(...).stat().st_size` + +PTH202.py:16:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` + | +15 | os.path.getsize(filename) +16 | os.path.getsize(filename1) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 +17 | os.path.getsize(filename2) + | + = help: Replace with `Path(...).stat().st_size` + +PTH202.py:17:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` + | +15 | os.path.getsize(filename) +16 | os.path.getsize(filename1) +17 | os.path.getsize(filename2) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 +18 | +19 | os.path.getsize(filename="filename") + | + = help: Replace with `Path(...).stat().st_size` + +PTH202.py:19:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` + | +17 | os.path.getsize(filename2) +18 | +19 | os.path.getsize(filename="filename") + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 +20 | os.path.getsize(filename=b"filename") +21 | os.path.getsize(filename=Path("filename")) + | + = help: Replace with `Path(...).stat().st_size` + +PTH202.py:20:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` + | +19 | os.path.getsize(filename="filename") +20 | os.path.getsize(filename=b"filename") + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 +21 | os.path.getsize(filename=Path("filename")) +22 | os.path.getsize(filename=__file__) + | + = help: Replace with `Path(...).stat().st_size` + +PTH202.py:21:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` + | +19 | os.path.getsize(filename="filename") +20 | os.path.getsize(filename=b"filename") +21 | os.path.getsize(filename=Path("filename")) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 +22 | os.path.getsize(filename=__file__) + | + = help: Replace with `Path(...).stat().st_size` + +PTH202.py:22:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` + | +20 | os.path.getsize(filename=b"filename") +21 | os.path.getsize(filename=Path("filename")) +22 | os.path.getsize(filename=__file__) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 +23 | +24 | getsize("filename") + | + = help: Replace with `Path(...).stat().st_size` + +PTH202.py:24:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` + | +22 | os.path.getsize(filename=__file__) +23 | +24 | getsize("filename") + | ^^^^^^^^^^^^^^^^^^^ PTH202 +25 | getsize(b"filename") +26 | getsize(Path("filename")) + | + = help: Replace with `Path(...).stat().st_size` + +PTH202.py:25:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` + | +24 | getsize("filename") +25 | getsize(b"filename") + | ^^^^^^^^^^^^^^^^^^^^ PTH202 +26 | getsize(Path("filename")) +27 | getsize(__file__) + | + = help: Replace with `Path(...).stat().st_size` + +PTH202.py:26:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` + | +24 | getsize("filename") +25 | getsize(b"filename") +26 | getsize(Path("filename")) + | ^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 +27 | getsize(__file__) + | + = help: Replace with `Path(...).stat().st_size` + +PTH202.py:27:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` + | +25 | getsize(b"filename") +26 | getsize(Path("filename")) +27 | getsize(__file__) + | ^^^^^^^^^^^^^^^^^ PTH202 +28 | +29 | getsize(filename="filename") + | + = help: Replace with `Path(...).stat().st_size` + +PTH202.py:29:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` + | +27 | getsize(__file__) +28 | +29 | getsize(filename="filename") + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 +30 | getsize(filename=b"filename") +31 | getsize(filename=Path("filename")) + | + = help: Replace with `Path(...).stat().st_size` + +PTH202.py:30:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` + | +29 | getsize(filename="filename") +30 | getsize(filename=b"filename") + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 +31 | getsize(filename=Path("filename")) +32 | getsize(filename=__file__) + | + = help: Replace with `Path(...).stat().st_size` + +PTH202.py:31:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` + | +29 | getsize(filename="filename") +30 | getsize(filename=b"filename") +31 | getsize(filename=Path("filename")) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 +32 | getsize(filename=__file__) + | + = help: Replace with `Path(...).stat().st_size` + +PTH202.py:32:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` + | +30 | getsize(filename=b"filename") +31 | getsize(filename=Path("filename")) +32 | getsize(filename=__file__) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 +33 | +34 | getsize(filename) + | + = help: Replace with `Path(...).stat().st_size` + +PTH202.py:34:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` + | +32 | getsize(filename=__file__) +33 | +34 | getsize(filename) + | ^^^^^^^^^^^^^^^^^ PTH202 +35 | getsize(filename1) +36 | getsize(filename2) + | + = help: Replace with `Path(...).stat().st_size` + +PTH202.py:35:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` + | +34 | getsize(filename) +35 | getsize(filename1) + | ^^^^^^^^^^^^^^^^^^ PTH202 +36 | getsize(filename2) + | + = help: Replace with `Path(...).stat().st_size` + +PTH202.py:36:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` + | +34 | getsize(filename) +35 | getsize(filename1) +36 | getsize(filename2) + | ^^^^^^^^^^^^^^^^^^ PTH202 + | + = help: Replace with `Path(...).stat().st_size` + +PTH202.py:39:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` + | +39 | / os.path.getsize( +40 | | "filename", # comment +41 | | ) + | |_^ PTH202 +42 | +43 | os.path.getsize( + | + = help: Replace with `Path(...).stat().st_size` + +PTH202.py:43:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` + | +41 | ) +42 | +43 | / os.path.getsize( +44 | | # comment +45 | | "filename" +46 | | , +47 | | # comment +48 | | ) + | |_^ PTH202 +49 | +50 | os.path.getsize( + | + = help: Replace with `Path(...).stat().st_size` + +PTH202.py:50:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` + | +48 | ) +49 | +50 | / os.path.getsize( +51 | | # comment +52 | | b"filename" +53 | | # comment +54 | | ) + | |_^ PTH202 +55 | +56 | os.path.getsize( # comment + | + = help: Replace with `Path(...).stat().st_size` + +PTH202.py:56:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` + | +54 | ) +55 | +56 | / os.path.getsize( # comment +57 | | Path(__file__) +58 | | # comment +59 | | ) # comment + | |_^ PTH202 +60 | +61 | getsize( # comment + | + = help: Replace with `Path(...).stat().st_size` + +PTH202.py:61:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` + | +59 | ) # comment +60 | +61 | / getsize( # comment +62 | | "filename") + | |_______________^ PTH202 +63 | +64 | getsize( # comment + | + = help: Replace with `Path(...).stat().st_size` + +PTH202.py:64:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` + | +62 | "filename") +63 | +64 | / getsize( # comment +65 | | b"filename", +66 | | #comment +67 | | ) + | |_^ PTH202 +68 | +69 | os.path.getsize("file" + "name") + | + = help: Replace with `Path(...).stat().st_size` + +PTH202.py:69:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` + | +67 | ) +68 | +69 | os.path.getsize("file" + "name") + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 +70 | +71 | getsize \ + | + = help: Replace with `Path(...).stat().st_size` + +PTH202.py:71:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` + | +69 | os.path.getsize("file" + "name") +70 | +71 | / getsize \ +72 | | \ +73 | | \ +74 | | ( # comment +75 | | "filename", +76 | | ) + | |_____^ PTH202 +77 | +78 | getsize(Path("filename").resolve()) + | + = help: Replace with `Path(...).stat().st_size` + +PTH202.py:78:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` + | +76 | ) +77 | +78 | getsize(Path("filename").resolve()) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 +79 | +80 | import pathlib | + = help: Replace with `Path(...).stat().st_size` -PTH202.py:14:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` +PTH202.py:82:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` | -12 | getsize(b"filename") -13 | getsize(Path("filename")) -14 | getsize(__file__) - | ^^^^^^^ PTH202 +80 | import pathlib +81 | +82 | os.path.getsize(pathlib.Path("filename")) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 | + = help: Replace with `Path(...).stat().st_size` diff --git a/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__PTH202_PTH202_2.py.snap b/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__PTH202_PTH202_2.py.snap new file mode 100644 index 0000000000000..f74ef8e4cc3d2 --- /dev/null +++ b/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__PTH202_PTH202_2.py.snap @@ -0,0 +1,31 @@ +--- +source: crates/ruff_linter/src/rules/flake8_use_pathlib/mod.rs +--- +PTH202_2.py:3:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` + | +1 | import os +2 | +3 | os.path.getsize(filename="filename") + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 +4 | os.path.getsize(filename=b"filename") +5 | os.path.getsize(filename=__file__) + | + = help: Replace with `Path(...).stat().st_size` + +PTH202_2.py:4:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` + | +3 | os.path.getsize(filename="filename") +4 | os.path.getsize(filename=b"filename") + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 +5 | os.path.getsize(filename=__file__) + | + = help: Replace with `Path(...).stat().st_size` + +PTH202_2.py:5:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` + | +3 | os.path.getsize(filename="filename") +4 | os.path.getsize(filename=b"filename") +5 | os.path.getsize(filename=__file__) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | + = help: Replace with `Path(...).stat().st_size` diff --git a/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__preview__PTH202_PTH202.py.snap b/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__preview__PTH202_PTH202.py.snap new file mode 100644 index 0000000000000..155b2d83079f0 --- /dev/null +++ b/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__preview__PTH202_PTH202.py.snap @@ -0,0 +1,697 @@ +--- +source: crates/ruff_linter/src/rules/flake8_use_pathlib/mod.rs +--- +PTH202.py:10:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size` + | +10 | os.path.getsize("filename") + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 +11 | os.path.getsize(b"filename") +12 | os.path.getsize(Path("filename")) + | + = help: Replace with `Path(...).stat().st_size` + +ℹ Safe fix +7 7 | filename2 = Path("filename") +8 8 | +9 9 | +10 |-os.path.getsize("filename") + 10 |+Path("filename").stat().st_size +11 11 | os.path.getsize(b"filename") +12 12 | os.path.getsize(Path("filename")) +13 13 | os.path.getsize(__file__) + +PTH202.py:11:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size` + | +10 | os.path.getsize("filename") +11 | os.path.getsize(b"filename") + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 +12 | os.path.getsize(Path("filename")) +13 | os.path.getsize(__file__) + | + = help: Replace with `Path(...).stat().st_size` + +ℹ Safe fix +8 8 | +9 9 | +10 10 | os.path.getsize("filename") +11 |-os.path.getsize(b"filename") + 11 |+Path(b"filename").stat().st_size +12 12 | os.path.getsize(Path("filename")) +13 13 | os.path.getsize(__file__) +14 14 | + +PTH202.py:12:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size` + | +10 | os.path.getsize("filename") +11 | os.path.getsize(b"filename") +12 | os.path.getsize(Path("filename")) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 +13 | os.path.getsize(__file__) + | + = help: Replace with `Path(...).stat().st_size` + +ℹ Safe fix +9 9 | +10 10 | os.path.getsize("filename") +11 11 | os.path.getsize(b"filename") +12 |-os.path.getsize(Path("filename")) + 12 |+Path("filename").stat().st_size +13 13 | os.path.getsize(__file__) +14 14 | +15 15 | os.path.getsize(filename) + +PTH202.py:13:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size` + | +11 | os.path.getsize(b"filename") +12 | os.path.getsize(Path("filename")) +13 | os.path.getsize(__file__) + | ^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 +14 | +15 | os.path.getsize(filename) + | + = help: Replace with `Path(...).stat().st_size` + +ℹ Safe fix +10 10 | os.path.getsize("filename") +11 11 | os.path.getsize(b"filename") +12 12 | os.path.getsize(Path("filename")) +13 |-os.path.getsize(__file__) + 13 |+Path(__file__).stat().st_size +14 14 | +15 15 | os.path.getsize(filename) +16 16 | os.path.getsize(filename1) + +PTH202.py:15:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size` + | +13 | os.path.getsize(__file__) +14 | +15 | os.path.getsize(filename) + | ^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 +16 | os.path.getsize(filename1) +17 | os.path.getsize(filename2) + | + = help: Replace with `Path(...).stat().st_size` + +ℹ Safe fix +12 12 | os.path.getsize(Path("filename")) +13 13 | os.path.getsize(__file__) +14 14 | +15 |-os.path.getsize(filename) + 15 |+Path(filename).stat().st_size +16 16 | os.path.getsize(filename1) +17 17 | os.path.getsize(filename2) +18 18 | + +PTH202.py:16:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size` + | +15 | os.path.getsize(filename) +16 | os.path.getsize(filename1) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 +17 | os.path.getsize(filename2) + | + = help: Replace with `Path(...).stat().st_size` + +ℹ Safe fix +13 13 | os.path.getsize(__file__) +14 14 | +15 15 | os.path.getsize(filename) +16 |-os.path.getsize(filename1) + 16 |+Path(filename1).stat().st_size +17 17 | os.path.getsize(filename2) +18 18 | +19 19 | os.path.getsize(filename="filename") + +PTH202.py:17:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size` + | +15 | os.path.getsize(filename) +16 | os.path.getsize(filename1) +17 | os.path.getsize(filename2) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 +18 | +19 | os.path.getsize(filename="filename") + | + = help: Replace with `Path(...).stat().st_size` + +ℹ Safe fix +14 14 | +15 15 | os.path.getsize(filename) +16 16 | os.path.getsize(filename1) +17 |-os.path.getsize(filename2) + 17 |+Path(filename2).stat().st_size +18 18 | +19 19 | os.path.getsize(filename="filename") +20 20 | os.path.getsize(filename=b"filename") + +PTH202.py:19:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size` + | +17 | os.path.getsize(filename2) +18 | +19 | os.path.getsize(filename="filename") + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 +20 | os.path.getsize(filename=b"filename") +21 | os.path.getsize(filename=Path("filename")) + | + = help: Replace with `Path(...).stat().st_size` + +ℹ Safe fix +16 16 | os.path.getsize(filename1) +17 17 | os.path.getsize(filename2) +18 18 | +19 |-os.path.getsize(filename="filename") + 19 |+Path("filename").stat().st_size +20 20 | os.path.getsize(filename=b"filename") +21 21 | os.path.getsize(filename=Path("filename")) +22 22 | os.path.getsize(filename=__file__) + +PTH202.py:20:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size` + | +19 | os.path.getsize(filename="filename") +20 | os.path.getsize(filename=b"filename") + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 +21 | os.path.getsize(filename=Path("filename")) +22 | os.path.getsize(filename=__file__) + | + = help: Replace with `Path(...).stat().st_size` + +ℹ Safe fix +17 17 | os.path.getsize(filename2) +18 18 | +19 19 | os.path.getsize(filename="filename") +20 |-os.path.getsize(filename=b"filename") + 20 |+Path(b"filename").stat().st_size +21 21 | os.path.getsize(filename=Path("filename")) +22 22 | os.path.getsize(filename=__file__) +23 23 | + +PTH202.py:21:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size` + | +19 | os.path.getsize(filename="filename") +20 | os.path.getsize(filename=b"filename") +21 | os.path.getsize(filename=Path("filename")) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 +22 | os.path.getsize(filename=__file__) + | + = help: Replace with `Path(...).stat().st_size` + +ℹ Safe fix +18 18 | +19 19 | os.path.getsize(filename="filename") +20 20 | os.path.getsize(filename=b"filename") +21 |-os.path.getsize(filename=Path("filename")) + 21 |+Path("filename").stat().st_size +22 22 | os.path.getsize(filename=__file__) +23 23 | +24 24 | getsize("filename") + +PTH202.py:22:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size` + | +20 | os.path.getsize(filename=b"filename") +21 | os.path.getsize(filename=Path("filename")) +22 | os.path.getsize(filename=__file__) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 +23 | +24 | getsize("filename") + | + = help: Replace with `Path(...).stat().st_size` + +ℹ Safe fix +19 19 | os.path.getsize(filename="filename") +20 20 | os.path.getsize(filename=b"filename") +21 21 | os.path.getsize(filename=Path("filename")) +22 |-os.path.getsize(filename=__file__) + 22 |+Path(__file__).stat().st_size +23 23 | +24 24 | getsize("filename") +25 25 | getsize(b"filename") + +PTH202.py:24:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size` + | +22 | os.path.getsize(filename=__file__) +23 | +24 | getsize("filename") + | ^^^^^^^^^^^^^^^^^^^ PTH202 +25 | getsize(b"filename") +26 | getsize(Path("filename")) + | + = help: Replace with `Path(...).stat().st_size` + +ℹ Safe fix +21 21 | os.path.getsize(filename=Path("filename")) +22 22 | os.path.getsize(filename=__file__) +23 23 | +24 |-getsize("filename") + 24 |+Path("filename").stat().st_size +25 25 | getsize(b"filename") +26 26 | getsize(Path("filename")) +27 27 | getsize(__file__) + +PTH202.py:25:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size` + | +24 | getsize("filename") +25 | getsize(b"filename") + | ^^^^^^^^^^^^^^^^^^^^ PTH202 +26 | getsize(Path("filename")) +27 | getsize(__file__) + | + = help: Replace with `Path(...).stat().st_size` + +ℹ Safe fix +22 22 | os.path.getsize(filename=__file__) +23 23 | +24 24 | getsize("filename") +25 |-getsize(b"filename") + 25 |+Path(b"filename").stat().st_size +26 26 | getsize(Path("filename")) +27 27 | getsize(__file__) +28 28 | + +PTH202.py:26:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size` + | +24 | getsize("filename") +25 | getsize(b"filename") +26 | getsize(Path("filename")) + | ^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 +27 | getsize(__file__) + | + = help: Replace with `Path(...).stat().st_size` + +ℹ Safe fix +23 23 | +24 24 | getsize("filename") +25 25 | getsize(b"filename") +26 |-getsize(Path("filename")) + 26 |+Path("filename").stat().st_size +27 27 | getsize(__file__) +28 28 | +29 29 | getsize(filename="filename") + +PTH202.py:27:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size` + | +25 | getsize(b"filename") +26 | getsize(Path("filename")) +27 | getsize(__file__) + | ^^^^^^^^^^^^^^^^^ PTH202 +28 | +29 | getsize(filename="filename") + | + = help: Replace with `Path(...).stat().st_size` + +ℹ Safe fix +24 24 | getsize("filename") +25 25 | getsize(b"filename") +26 26 | getsize(Path("filename")) +27 |-getsize(__file__) + 27 |+Path(__file__).stat().st_size +28 28 | +29 29 | getsize(filename="filename") +30 30 | getsize(filename=b"filename") + +PTH202.py:29:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size` + | +27 | getsize(__file__) +28 | +29 | getsize(filename="filename") + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 +30 | getsize(filename=b"filename") +31 | getsize(filename=Path("filename")) + | + = help: Replace with `Path(...).stat().st_size` + +ℹ Safe fix +26 26 | getsize(Path("filename")) +27 27 | getsize(__file__) +28 28 | +29 |-getsize(filename="filename") + 29 |+Path("filename").stat().st_size +30 30 | getsize(filename=b"filename") +31 31 | getsize(filename=Path("filename")) +32 32 | getsize(filename=__file__) + +PTH202.py:30:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size` + | +29 | getsize(filename="filename") +30 | getsize(filename=b"filename") + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 +31 | getsize(filename=Path("filename")) +32 | getsize(filename=__file__) + | + = help: Replace with `Path(...).stat().st_size` + +ℹ Safe fix +27 27 | getsize(__file__) +28 28 | +29 29 | getsize(filename="filename") +30 |-getsize(filename=b"filename") + 30 |+Path(b"filename").stat().st_size +31 31 | getsize(filename=Path("filename")) +32 32 | getsize(filename=__file__) +33 33 | + +PTH202.py:31:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size` + | +29 | getsize(filename="filename") +30 | getsize(filename=b"filename") +31 | getsize(filename=Path("filename")) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 +32 | getsize(filename=__file__) + | + = help: Replace with `Path(...).stat().st_size` + +ℹ Safe fix +28 28 | +29 29 | getsize(filename="filename") +30 30 | getsize(filename=b"filename") +31 |-getsize(filename=Path("filename")) + 31 |+Path("filename").stat().st_size +32 32 | getsize(filename=__file__) +33 33 | +34 34 | getsize(filename) + +PTH202.py:32:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size` + | +30 | getsize(filename=b"filename") +31 | getsize(filename=Path("filename")) +32 | getsize(filename=__file__) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 +33 | +34 | getsize(filename) + | + = help: Replace with `Path(...).stat().st_size` + +ℹ Safe fix +29 29 | getsize(filename="filename") +30 30 | getsize(filename=b"filename") +31 31 | getsize(filename=Path("filename")) +32 |-getsize(filename=__file__) + 32 |+Path(__file__).stat().st_size +33 33 | +34 34 | getsize(filename) +35 35 | getsize(filename1) + +PTH202.py:34:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size` + | +32 | getsize(filename=__file__) +33 | +34 | getsize(filename) + | ^^^^^^^^^^^^^^^^^ PTH202 +35 | getsize(filename1) +36 | getsize(filename2) + | + = help: Replace with `Path(...).stat().st_size` + +ℹ Safe fix +31 31 | getsize(filename=Path("filename")) +32 32 | getsize(filename=__file__) +33 33 | +34 |-getsize(filename) + 34 |+Path(filename).stat().st_size +35 35 | getsize(filename1) +36 36 | getsize(filename2) +37 37 | + +PTH202.py:35:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size` + | +34 | getsize(filename) +35 | getsize(filename1) + | ^^^^^^^^^^^^^^^^^^ PTH202 +36 | getsize(filename2) + | + = help: Replace with `Path(...).stat().st_size` + +ℹ Safe fix +32 32 | getsize(filename=__file__) +33 33 | +34 34 | getsize(filename) +35 |-getsize(filename1) + 35 |+Path(filename1).stat().st_size +36 36 | getsize(filename2) +37 37 | +38 38 | + +PTH202.py:36:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size` + | +34 | getsize(filename) +35 | getsize(filename1) +36 | getsize(filename2) + | ^^^^^^^^^^^^^^^^^^ PTH202 + | + = help: Replace with `Path(...).stat().st_size` + +ℹ Safe fix +33 33 | +34 34 | getsize(filename) +35 35 | getsize(filename1) +36 |-getsize(filename2) + 36 |+Path(filename2).stat().st_size +37 37 | +38 38 | +39 39 | os.path.getsize( + +PTH202.py:39:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size` + | +39 | / os.path.getsize( +40 | | "filename", # comment +41 | | ) + | |_^ PTH202 +42 | +43 | os.path.getsize( + | + = help: Replace with `Path(...).stat().st_size` + +ℹ Unsafe fix +36 36 | getsize(filename2) +37 37 | +38 38 | +39 |-os.path.getsize( +40 |- "filename", # comment +41 |-) + 39 |+Path("filename").stat().st_size +42 40 | +43 41 | os.path.getsize( +44 42 | # comment + +PTH202.py:43:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size` + | +41 | ) +42 | +43 | / os.path.getsize( +44 | | # comment +45 | | "filename" +46 | | , +47 | | # comment +48 | | ) + | |_^ PTH202 +49 | +50 | os.path.getsize( + | + = help: Replace with `Path(...).stat().st_size` + +ℹ Unsafe fix +40 40 | "filename", # comment +41 41 | ) +42 42 | +43 |-os.path.getsize( +44 |- # comment +45 |- "filename" +46 |- , +47 |- # comment +48 |-) + 43 |+Path("filename").stat().st_size +49 44 | +50 45 | os.path.getsize( +51 46 | # comment + +PTH202.py:50:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size` + | +48 | ) +49 | +50 | / os.path.getsize( +51 | | # comment +52 | | b"filename" +53 | | # comment +54 | | ) + | |_^ PTH202 +55 | +56 | os.path.getsize( # comment + | + = help: Replace with `Path(...).stat().st_size` + +ℹ Unsafe fix +47 47 | # comment +48 48 | ) +49 49 | +50 |-os.path.getsize( +51 |- # comment +52 |- b"filename" +53 |- # comment +54 |-) + 50 |+Path(b"filename").stat().st_size +55 51 | +56 52 | os.path.getsize( # comment +57 53 | Path(__file__) + +PTH202.py:56:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size` + | +54 | ) +55 | +56 | / os.path.getsize( # comment +57 | | Path(__file__) +58 | | # comment +59 | | ) # comment + | |_^ PTH202 +60 | +61 | getsize( # comment + | + = help: Replace with `Path(...).stat().st_size` + +ℹ Unsafe fix +53 53 | # comment +54 54 | ) +55 55 | +56 |-os.path.getsize( # comment +57 |- Path(__file__) +58 |- # comment +59 |-) # comment + 56 |+Path(__file__).stat().st_size # comment +60 57 | +61 58 | getsize( # comment +62 59 | "filename") + +PTH202.py:61:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size` + | +59 | ) # comment +60 | +61 | / getsize( # comment +62 | | "filename") + | |_______________^ PTH202 +63 | +64 | getsize( # comment + | + = help: Replace with `Path(...).stat().st_size` + +ℹ Unsafe fix +58 58 | # comment +59 59 | ) # comment +60 60 | +61 |-getsize( # comment +62 |- "filename") + 61 |+Path("filename").stat().st_size +63 62 | +64 63 | getsize( # comment +65 64 | b"filename", + +PTH202.py:64:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size` + | +62 | "filename") +63 | +64 | / getsize( # comment +65 | | b"filename", +66 | | #comment +67 | | ) + | |_^ PTH202 +68 | +69 | os.path.getsize("file" + "name") + | + = help: Replace with `Path(...).stat().st_size` + +ℹ Unsafe fix +61 61 | getsize( # comment +62 62 | "filename") +63 63 | +64 |-getsize( # comment +65 |- b"filename", +66 |- #comment +67 |-) + 64 |+Path(b"filename").stat().st_size +68 65 | +69 66 | os.path.getsize("file" + "name") +70 67 | + +PTH202.py:69:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size` + | +67 | ) +68 | +69 | os.path.getsize("file" + "name") + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 +70 | +71 | getsize \ + | + = help: Replace with `Path(...).stat().st_size` + +ℹ Safe fix +66 66 | #comment +67 67 | ) +68 68 | +69 |-os.path.getsize("file" + "name") + 69 |+Path("file" + "name").stat().st_size +70 70 | +71 71 | getsize \ +72 72 | \ + +PTH202.py:71:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size` + | +69 | os.path.getsize("file" + "name") +70 | +71 | / getsize \ +72 | | \ +73 | | \ +74 | | ( # comment +75 | | "filename", +76 | | ) + | |_____^ PTH202 +77 | +78 | getsize(Path("filename").resolve()) + | + = help: Replace with `Path(...).stat().st_size` + +ℹ Unsafe fix +68 68 | +69 69 | os.path.getsize("file" + "name") +70 70 | +71 |-getsize \ +72 |-\ +73 |-\ +74 |- ( # comment +75 |- "filename", +76 |- ) + 71 |+Path("filename").stat().st_size +77 72 | +78 73 | getsize(Path("filename").resolve()) +79 74 | + +PTH202.py:78:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size` + | +76 | ) +77 | +78 | getsize(Path("filename").resolve()) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 +79 | +80 | import pathlib + | + = help: Replace with `Path(...).stat().st_size` + +ℹ Safe fix +75 75 | "filename", +76 76 | ) +77 77 | +78 |-getsize(Path("filename").resolve()) + 78 |+Path(Path("filename").resolve()).stat().st_size +79 79 | +80 80 | import pathlib +81 81 | + +PTH202.py:82:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size` + | +80 | import pathlib +81 | +82 | os.path.getsize(pathlib.Path("filename")) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | + = help: Replace with `Path(...).stat().st_size` + +ℹ Safe fix +79 79 | +80 80 | import pathlib +81 81 | +82 |-os.path.getsize(pathlib.Path("filename")) + 82 |+pathlib.Path("filename").stat().st_size diff --git a/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__preview__PTH202_PTH202_2.py.snap b/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__preview__PTH202_PTH202_2.py.snap new file mode 100644 index 0000000000000..23ae9b5bff22a --- /dev/null +++ b/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__preview__PTH202_PTH202_2.py.snap @@ -0,0 +1,58 @@ +--- +source: crates/ruff_linter/src/rules/flake8_use_pathlib/mod.rs +--- +PTH202_2.py:3:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size` + | +1 | import os +2 | +3 | os.path.getsize(filename="filename") + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 +4 | os.path.getsize(filename=b"filename") +5 | os.path.getsize(filename=__file__) + | + = help: Replace with `Path(...).stat().st_size` + +ℹ Safe fix +1 1 | import os + 2 |+import pathlib +2 3 | +3 |-os.path.getsize(filename="filename") + 4 |+pathlib.Path("filename").stat().st_size +4 5 | os.path.getsize(filename=b"filename") +5 6 | os.path.getsize(filename=__file__) + +PTH202_2.py:4:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size` + | +3 | os.path.getsize(filename="filename") +4 | os.path.getsize(filename=b"filename") + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 +5 | os.path.getsize(filename=__file__) + | + = help: Replace with `Path(...).stat().st_size` + +ℹ Safe fix +1 1 | import os + 2 |+import pathlib +2 3 | +3 4 | os.path.getsize(filename="filename") +4 |-os.path.getsize(filename=b"filename") + 5 |+pathlib.Path(b"filename").stat().st_size +5 6 | os.path.getsize(filename=__file__) + +PTH202_2.py:5:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size` + | +3 | os.path.getsize(filename="filename") +4 | os.path.getsize(filename=b"filename") +5 | os.path.getsize(filename=__file__) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | + = help: Replace with `Path(...).stat().st_size` + +ℹ Safe fix +1 1 | import os + 2 |+import pathlib +2 3 | +3 4 | os.path.getsize(filename="filename") +4 5 | os.path.getsize(filename=b"filename") +5 |-os.path.getsize(filename=__file__) + 6 |+pathlib.Path(__file__).stat().st_size