diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_use_pathlib/PTH203.py b/crates/ruff_linter/resources/test/fixtures/flake8_use_pathlib/PTH203.py index 19cd4f3583..7c499bfbc3 100644 --- a/crates/ruff_linter/resources/test/fixtures/flake8_use_pathlib/PTH203.py +++ b/crates/ruff_linter/resources/test/fixtures/flake8_use_pathlib/PTH203.py @@ -1,4 +1,4 @@ -import os.path +import os.path, pathlib from pathlib import Path from os.path import getatime @@ -10,3 +10,26 @@ os.path.getatime(Path("filename")) getatime("filename") getatime(b"filename") getatime(Path("filename")) + + +file = __file__ + +os.path.getatime(file) +os.path.getatime(filename="filename") +os.path.getatime(filename=Path("filename")) + +os.path.getatime( # comment 1 + # comment 2 + "filename" # comment 3 + # comment 4 + , # comment 5 + # comment 6 +) # comment 7 + +os.path.getatime("file" + "name") + +getatime(Path("filename").resolve()) + +os.path.getatime(pathlib.Path("filename")) + +getatime(Path("dir") / "file.txt") diff --git a/crates/ruff_linter/src/checkers/ast/analyze/expression.rs b/crates/ruff_linter/src/checkers/ast/analyze/expression.rs index a1f854e4d5..dfddaab7a3 100644 --- a/crates/ruff_linter/src/checkers/ast/analyze/expression.rs +++ b/crates/ruff_linter/src/checkers/ast/analyze/expression.rs @@ -1062,9 +1062,6 @@ pub(crate) fn expression(expr: &Expr, checker: &Checker) { Rule::OsPathSplitext, Rule::BuiltinOpen, Rule::PyPath, - Rule::OsPathGetatime, - Rule::OsPathGetmtime, - Rule::OsPathGetctime, Rule::Glob, Rule::OsListdir, Rule::OsSymlink, @@ -1074,6 +1071,15 @@ pub(crate) fn expression(expr: &Expr, checker: &Checker) { if checker.is_rule_enabled(Rule::OsPathGetsize) { flake8_use_pathlib::rules::os_path_getsize(checker, call); } + if checker.is_rule_enabled(Rule::OsPathGetatime) { + flake8_use_pathlib::rules::os_path_getatime(checker, call); + } + if checker.is_rule_enabled(Rule::OsPathGetctime) { + flake8_use_pathlib::rules::os_path_getctime(checker, call); + } + if checker.is_rule_enabled(Rule::OsPathGetmtime) { + flake8_use_pathlib::rules::os_path_getmtime(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 68d6e45b3f..26aaea5b53 100644 --- a/crates/ruff_linter/src/preview.rs +++ b/crates/ruff_linter/src/preview.rs @@ -54,6 +54,20 @@ pub(crate) const fn is_fix_manual_list_comprehension_enabled(settings: &LinterSe pub(crate) const fn is_fix_os_path_getsize_enabled(settings: &LinterSettings) -> bool { settings.preview.is_enabled() } +// https://github.com/astral-sh/ruff/pull/18922 +pub(crate) const fn is_fix_os_path_getmtime_enabled(settings: &LinterSettings) -> bool { + settings.preview.is_enabled() +} + +// https://github.com/astral-sh/ruff/pull/18922 +pub(crate) const fn is_fix_os_path_getatime_enabled(settings: &LinterSettings) -> bool { + settings.preview.is_enabled() +} + +// https://github.com/astral-sh/ruff/pull/18922 +pub(crate) const fn is_fix_os_path_getctime_enabled(settings: &LinterSettings) -> bool { + settings.preview.is_enabled() +} // https://github.com/astral-sh/ruff/pull/11436 // https://github.com/astral-sh/ruff/pull/11168 diff --git a/crates/ruff_linter/src/rules/flake8_use_pathlib/helpers.rs b/crates/ruff_linter/src/rules/flake8_use_pathlib/helpers.rs new file mode 100644 index 0000000000..ef71f948e3 --- /dev/null +++ b/crates/ruff_linter/src/rules/flake8_use_pathlib/helpers.rs @@ -0,0 +1,72 @@ +use crate::checkers::ast::Checker; +use crate::importer::ImportRequest; +use crate::{Applicability, Edit, Fix, Violation}; +use ruff_python_ast::{Expr, ExprCall}; +use ruff_text_size::Ranged; + +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"])) + }) +} + +pub(crate) fn check_os_path_get_calls( + checker: &Checker, + call: &ExprCall, + fn_name: &str, + attr: &str, + fix_enabled: bool, + violation: impl Violation, +) { + if checker + .semantic() + .resolve_qualified_name(&call.func) + .is_none_or(|qualified_name| qualified_name.segments() != ["os", "path", fn_name]) + { + 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 mut diagnostic = checker.report_diagnostic(violation, call.func.range()); + + if fix_enabled { + diagnostic.try_set_fix(|| { + let (import_edit, binding) = checker.importer().get_or_import_symbol( + &ImportRequest::import("pathlib", "Path"), + call.start(), + checker.semantic(), + )?; + + let applicability = if checker.comment_ranges().intersects(range) { + Applicability::Unsafe + } else { + Applicability::Safe + }; + + let replacement = if is_path_call(checker, arg) { + format!("{arg_code}.stat().{attr}") + } else { + format!("{binding}({arg_code}).stat().{attr}") + }; + + Ok(Fix::applicable_edits( + Edit::range_replacement(replacement, range), + [import_edit], + applicability, + )) + }); + } +} 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 7fbc92e410..b22fa584b7 100644 --- a/crates/ruff_linter/src/rules/flake8_use_pathlib/mod.rs +++ b/crates/ruff_linter/src/rules/flake8_use_pathlib/mod.rs @@ -1,4 +1,5 @@ //! Rules from [flake8-use-pathlib](https://pypi.org/project/flake8-use-pathlib/). +mod helpers; pub(crate) mod rules; pub(crate) mod violations; @@ -81,6 +82,9 @@ mod tests { #[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"))] fn preview_flake8_use_pathlib(rule_code: Rule, path: &Path) -> Result<()> { let snapshot = format!( "preview__{}_{}", diff --git a/crates/ruff_linter/src/rules/flake8_use_pathlib/rules/os_path_getatime.rs b/crates/ruff_linter/src/rules/flake8_use_pathlib/rules/os_path_getatime.rs index 78c5bfe06e..9ec2297844 100644 --- a/crates/ruff_linter/src/rules/flake8_use_pathlib/rules/os_path_getatime.rs +++ b/crates/ruff_linter/src/rules/flake8_use_pathlib/rules/os_path_getatime.rs @@ -1,6 +1,9 @@ +use crate::checkers::ast::Checker; +use crate::preview::is_fix_os_path_getatime_enabled; +use crate::rules::flake8_use_pathlib::helpers::check_os_path_get_calls; +use crate::{FixAvailability, Violation}; use ruff_macros::{ViolationMetadata, derive_message_formats}; - -use crate::Violation; +use ruff_python_ast::ExprCall; /// ## What it does /// Checks for uses of `os.path.getatime`. @@ -32,6 +35,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.getatime`](https://docs.python.org/3/library/os.path.html#os.path.getatime) @@ -43,8 +49,25 @@ use crate::Violation; pub(crate) struct OsPathGetatime; impl Violation for OsPathGetatime { + const FIX_AVAILABILITY: FixAvailability = FixAvailability::Sometimes; #[derive_message_formats] fn message(&self) -> String { "`os.path.getatime` should be replaced by `Path.stat().st_atime`".to_string() } + + fn fix_title(&self) -> Option { + Some("Replace with `Path.stat(...).st_atime`".to_string()) + } +} + +/// PTH203 +pub(crate) fn os_path_getatime(checker: &Checker, call: &ExprCall) { + check_os_path_get_calls( + checker, + call, + "getatime", + "st_atime", + is_fix_os_path_getatime_enabled(checker.settings()), + OsPathGetatime, + ); } diff --git a/crates/ruff_linter/src/rules/flake8_use_pathlib/rules/os_path_getctime.rs b/crates/ruff_linter/src/rules/flake8_use_pathlib/rules/os_path_getctime.rs index 866c37e306..023d17daeb 100644 --- a/crates/ruff_linter/src/rules/flake8_use_pathlib/rules/os_path_getctime.rs +++ b/crates/ruff_linter/src/rules/flake8_use_pathlib/rules/os_path_getctime.rs @@ -1,6 +1,9 @@ +use crate::checkers::ast::Checker; +use crate::preview::is_fix_os_path_getctime_enabled; +use crate::rules::flake8_use_pathlib::helpers::check_os_path_get_calls; +use crate::{FixAvailability, Violation}; use ruff_macros::{ViolationMetadata, derive_message_formats}; - -use crate::Violation; +use ruff_python_ast::ExprCall; /// ## What it does /// Checks for uses of `os.path.getctime`. @@ -32,6 +35,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.getctime`](https://docs.python.org/3/library/os.path.html#os.path.getctime) @@ -43,8 +49,26 @@ use crate::Violation; pub(crate) struct OsPathGetctime; impl Violation for OsPathGetctime { + const FIX_AVAILABILITY: FixAvailability = FixAvailability::Sometimes; + #[derive_message_formats] fn message(&self) -> String { "`os.path.getctime` should be replaced by `Path.stat().st_ctime`".to_string() } + + fn fix_title(&self) -> Option { + Some("Replace with `Path.stat(...).st_ctime`".to_string()) + } +} + +/// PTH205 +pub(crate) fn os_path_getctime(checker: &Checker, call: &ExprCall) { + check_os_path_get_calls( + checker, + call, + "getctime", + "st_ctime", + is_fix_os_path_getctime_enabled(checker.settings()), + OsPathGetctime, + ); } diff --git a/crates/ruff_linter/src/rules/flake8_use_pathlib/rules/os_path_getmtime.rs b/crates/ruff_linter/src/rules/flake8_use_pathlib/rules/os_path_getmtime.rs index 81ec1b53c2..40c7d96e8a 100644 --- a/crates/ruff_linter/src/rules/flake8_use_pathlib/rules/os_path_getmtime.rs +++ b/crates/ruff_linter/src/rules/flake8_use_pathlib/rules/os_path_getmtime.rs @@ -1,6 +1,9 @@ +use crate::checkers::ast::Checker; +use crate::preview::is_fix_os_path_getmtime_enabled; +use crate::rules::flake8_use_pathlib::helpers::check_os_path_get_calls; +use crate::{FixAvailability, Violation}; use ruff_macros::{ViolationMetadata, derive_message_formats}; - -use crate::Violation; +use ruff_python_ast::ExprCall; /// ## What it does /// Checks for uses of `os.path.getmtime`. @@ -32,6 +35,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.getmtime`](https://docs.python.org/3/library/os.path.html#os.path.getmtime) @@ -43,8 +49,26 @@ use crate::Violation; pub(crate) struct OsPathGetmtime; impl Violation for OsPathGetmtime { + const FIX_AVAILABILITY: FixAvailability = FixAvailability::Sometimes; + #[derive_message_formats] fn message(&self) -> String { "`os.path.getmtime` should be replaced by `Path.stat().st_mtime`".to_string() } + + fn fix_title(&self) -> Option { + Some("Replace with `Path.stat(...).st_mtime`".to_string()) + } +} + +/// PTH204 +pub(crate) fn os_path_getmtime(checker: &Checker, call: &ExprCall) { + check_os_path_get_calls( + checker, + call, + "getmtime", + "st_mtime", + is_fix_os_path_getmtime_enabled(checker.settings()), + OsPathGetmtime, + ); } 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 e801e13a7c..6e3da85fac 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,11 +1,9 @@ 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 crate::rules::flake8_use_pathlib::helpers::check_os_path_get_calls; +use crate::{FixAvailability, Violation}; use ruff_macros::{ViolationMetadata, derive_message_formats}; -use ruff_python_ast::name::QualifiedName; -use ruff_python_ast::{Expr, ExprCall}; -use ruff_text_size::Ranged; +use ruff_python_ast::ExprCall; /// ## What it does /// Checks for uses of `os.path.getsize`. @@ -65,63 +63,12 @@ impl Violation for OsPathGetsize { /// 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"])) - }) + check_os_path_get_calls( + checker, + call, + "getsize", + "st_size", + is_fix_os_path_getsize_enabled(checker.settings()), + OsPathGetsize, + ); } 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 6fdf05b902..ab3c7e43ca 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 @@ -4,9 +4,7 @@ use ruff_python_semantic::analyze::typing; use ruff_text_size::Ranged; use crate::checkers::ast::Checker; -use crate::rules::flake8_use_pathlib::rules::{ - Glob, OsPathGetatime, OsPathGetctime, OsPathGetmtime, -}; +use crate::rules::flake8_use_pathlib::rules::Glob; use crate::rules::flake8_use_pathlib::violations::{ BuiltinOpen, Joiner, OsChmod, OsGetcwd, OsListdir, OsMakedirs, OsMkdir, OsPathAbspath, OsPathBasename, OsPathDirname, OsPathExists, OsPathExpanduser, OsPathIsabs, OsPathIsdir, @@ -194,12 +192,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), - // PTH203 - ["os", "path", "getatime"] => checker.report_diagnostic_if_enabled(OsPathGetatime, range), - // PTH204 - ["os", "path", "getmtime"] => checker.report_diagnostic_if_enabled(OsPathGetmtime, range), - // PTH205 - ["os", "path", "getctime"] => checker.report_diagnostic_if_enabled(OsPathGetctime, range), // PTH211 ["os", "symlink"] => { // `dir_fd` is not supported by pathlib, so check if there are non-default values. 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 d4d6fa2c9d..4ae823b9e1 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 @@ -4,7 +4,7 @@ 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 + | ^^^^^^^^^^^^^^^ PTH202 11 | os.path.getsize(b"filename") 12 | os.path.getsize(Path("filename")) | @@ -14,7 +14,7 @@ PTH202.py:11:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_s | 10 | os.path.getsize("filename") 11 | os.path.getsize(b"filename") - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^^^^^^^^^ PTH202 12 | os.path.getsize(Path("filename")) 13 | os.path.getsize(__file__) | @@ -25,7 +25,7 @@ PTH202.py:12:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_s 10 | os.path.getsize("filename") 11 | os.path.getsize(b"filename") 12 | os.path.getsize(Path("filename")) - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^^^^^^^^^ PTH202 13 | os.path.getsize(__file__) | = help: Replace with `Path(...).stat().st_size` @@ -35,7 +35,7 @@ PTH202.py:13:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_s 11 | os.path.getsize(b"filename") 12 | os.path.getsize(Path("filename")) 13 | os.path.getsize(__file__) - | ^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^^^^^^^^^ PTH202 14 | 15 | os.path.getsize(filename) | @@ -46,7 +46,7 @@ PTH202.py:15:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_s 13 | os.path.getsize(__file__) 14 | 15 | os.path.getsize(filename) - | ^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^^^^^^^^^ PTH202 16 | os.path.getsize(filename1) 17 | os.path.getsize(filename2) | @@ -56,7 +56,7 @@ PTH202.py:16:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_s | 15 | os.path.getsize(filename) 16 | os.path.getsize(filename1) - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^^^^^^^^^ PTH202 17 | os.path.getsize(filename2) | = help: Replace with `Path(...).stat().st_size` @@ -66,7 +66,7 @@ PTH202.py:17:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_s 15 | os.path.getsize(filename) 16 | os.path.getsize(filename1) 17 | os.path.getsize(filename2) - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^^^^^^^^^ PTH202 18 | 19 | os.path.getsize(filename="filename") | @@ -77,7 +77,7 @@ PTH202.py:19:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_s 17 | os.path.getsize(filename2) 18 | 19 | os.path.getsize(filename="filename") - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^^^^^^^^^ PTH202 20 | os.path.getsize(filename=b"filename") 21 | os.path.getsize(filename=Path("filename")) | @@ -87,7 +87,7 @@ PTH202.py:20:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_s | 19 | os.path.getsize(filename="filename") 20 | os.path.getsize(filename=b"filename") - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^^^^^^^^^ PTH202 21 | os.path.getsize(filename=Path("filename")) 22 | os.path.getsize(filename=__file__) | @@ -98,7 +98,7 @@ PTH202.py:21:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_s 19 | os.path.getsize(filename="filename") 20 | os.path.getsize(filename=b"filename") 21 | os.path.getsize(filename=Path("filename")) - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^^^^^^^^^ PTH202 22 | os.path.getsize(filename=__file__) | = help: Replace with `Path(...).stat().st_size` @@ -108,7 +108,7 @@ PTH202.py:22:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_s 20 | os.path.getsize(filename=b"filename") 21 | os.path.getsize(filename=Path("filename")) 22 | os.path.getsize(filename=__file__) - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^^^^^^^^^ PTH202 23 | 24 | getsize("filename") | @@ -119,7 +119,7 @@ PTH202.py:24:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_s 22 | os.path.getsize(filename=__file__) 23 | 24 | getsize("filename") - | ^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^ PTH202 25 | getsize(b"filename") 26 | getsize(Path("filename")) | @@ -129,7 +129,7 @@ PTH202.py:25:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_s | 24 | getsize("filename") 25 | getsize(b"filename") - | ^^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^ PTH202 26 | getsize(Path("filename")) 27 | getsize(__file__) | @@ -140,7 +140,7 @@ PTH202.py:26:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_s 24 | getsize("filename") 25 | getsize(b"filename") 26 | getsize(Path("filename")) - | ^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^ PTH202 27 | getsize(__file__) | = help: Replace with `Path(...).stat().st_size` @@ -150,7 +150,7 @@ PTH202.py:27:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_s 25 | getsize(b"filename") 26 | getsize(Path("filename")) 27 | getsize(__file__) - | ^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^ PTH202 28 | 29 | getsize(filename="filename") | @@ -161,7 +161,7 @@ PTH202.py:29:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_s 27 | getsize(__file__) 28 | 29 | getsize(filename="filename") - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^ PTH202 30 | getsize(filename=b"filename") 31 | getsize(filename=Path("filename")) | @@ -171,7 +171,7 @@ PTH202.py:30:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_s | 29 | getsize(filename="filename") 30 | getsize(filename=b"filename") - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^ PTH202 31 | getsize(filename=Path("filename")) 32 | getsize(filename=__file__) | @@ -182,7 +182,7 @@ PTH202.py:31:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_s 29 | getsize(filename="filename") 30 | getsize(filename=b"filename") 31 | getsize(filename=Path("filename")) - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^ PTH202 32 | getsize(filename=__file__) | = help: Replace with `Path(...).stat().st_size` @@ -192,7 +192,7 @@ PTH202.py:32:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_s 30 | getsize(filename=b"filename") 31 | getsize(filename=Path("filename")) 32 | getsize(filename=__file__) - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^ PTH202 33 | 34 | getsize(filename) | @@ -203,7 +203,7 @@ PTH202.py:34:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_s 32 | getsize(filename=__file__) 33 | 34 | getsize(filename) - | ^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^ PTH202 35 | getsize(filename1) 36 | getsize(filename2) | @@ -213,7 +213,7 @@ PTH202.py:35:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_s | 34 | getsize(filename) 35 | getsize(filename1) - | ^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^ PTH202 36 | getsize(filename2) | = help: Replace with `Path(...).stat().st_size` @@ -223,89 +223,70 @@ PTH202.py:36:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_s 34 | getsize(filename) 35 | getsize(filename1) 36 | getsize(filename2) - | ^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^ 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( +39 | os.path.getsize( + | ^^^^^^^^^^^^^^^ PTH202 +40 | "filename", # comment +41 | ) | = help: Replace with `Path(...).stat().st_size` PTH202.py:43:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` | -41 | ) +41 | ) 42 | -43 | / os.path.getsize( -44 | | # comment -45 | | "filename" -46 | | , -47 | | # comment -48 | | ) - | |_^ PTH202 -49 | -50 | os.path.getsize( +43 | os.path.getsize( + | ^^^^^^^^^^^^^^^ PTH202 +44 | # comment +45 | "filename" | = help: Replace with `Path(...).stat().st_size` PTH202.py:50:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` | -48 | ) +48 | ) 49 | -50 | / os.path.getsize( -51 | | # comment -52 | | b"filename" -53 | | # comment -54 | | ) - | |_^ PTH202 -55 | -56 | os.path.getsize( # comment +50 | os.path.getsize( + | ^^^^^^^^^^^^^^^ PTH202 +51 | # comment +52 | b"filename" | = help: Replace with `Path(...).stat().st_size` PTH202.py:56:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` | -54 | ) +54 | ) 55 | -56 | / os.path.getsize( # comment -57 | | Path(__file__) -58 | | # comment -59 | | ) # comment - | |_^ PTH202 -60 | -61 | getsize( # comment +56 | os.path.getsize( # comment + | ^^^^^^^^^^^^^^^ PTH202 +57 | Path(__file__) +58 | # 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 +59 | ) # comment 60 | -61 | / getsize( # comment -62 | | "filename") - | |_______________^ PTH202 -63 | -64 | getsize( # comment +61 | getsize( # comment + | ^^^^^^^ PTH202 +62 | "filename") | = 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") +62 | "filename") 63 | -64 | / getsize( # comment -65 | | b"filename", -66 | | #comment -67 | | ) - | |_^ PTH202 -68 | -69 | os.path.getsize("file" + "name") +64 | getsize( # comment + | ^^^^^^^ PTH202 +65 | b"filename", +66 | #comment | = help: Replace with `Path(...).stat().st_size` @@ -314,7 +295,7 @@ PTH202.py:69:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_s 67 | ) 68 | 69 | os.path.getsize("file" + "name") - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^^^^^^^^^ PTH202 70 | 71 | getsize \ | @@ -322,17 +303,12 @@ PTH202.py:69:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_s PTH202.py:71:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_size` | -69 | os.path.getsize("file" + "name") +69 | os.path.getsize("file" + "name") 70 | -71 | / getsize \ -72 | | \ -73 | | \ -74 | | ( # comment -75 | | "filename", -76 | | ) - | |_____^ PTH202 -77 | -78 | getsize(Path("filename").resolve()) +71 | getsize \ + | ^^^^^^^ PTH202 +72 | \ +73 | \ | = help: Replace with `Path(...).stat().st_size` @@ -341,7 +317,7 @@ PTH202.py:78:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_s 76 | ) 77 | 78 | getsize(Path("filename").resolve()) - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^ PTH202 79 | 80 | import pathlib | @@ -352,6 +328,6 @@ PTH202.py:82:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_s 80 | import pathlib 81 | 82 | os.path.getsize(pathlib.Path("filename")) - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^^^^^^^^^ 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 index f74ef8e4cc..a5c3c8b7c2 100644 --- 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 @@ -6,7 +6,7 @@ PTH202_2.py:3:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_ 1 | import os 2 | 3 | os.path.getsize(filename="filename") - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^^^^^^^^^ PTH202 4 | os.path.getsize(filename=b"filename") 5 | os.path.getsize(filename=__file__) | @@ -16,7 +16,7 @@ PTH202_2.py:4:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_ | 3 | os.path.getsize(filename="filename") 4 | os.path.getsize(filename=b"filename") - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^^^^^^^^^ PTH202 5 | os.path.getsize(filename=__file__) | = help: Replace with `Path(...).stat().st_size` @@ -26,6 +26,6 @@ PTH202_2.py:5:1: PTH202 `os.path.getsize` should be replaced by `Path.stat().st_ 3 | os.path.getsize(filename="filename") 4 | os.path.getsize(filename=b"filename") 5 | os.path.getsize(filename=__file__) - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^^^^^^^^^ 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__PTH203_PTH203.py.snap b/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__PTH203_PTH203.py.snap index 8851d9d32a..112cb7933a 100644 --- a/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__PTH203_PTH203.py.snap +++ b/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__PTH203_PTH203.py.snap @@ -10,6 +10,7 @@ PTH203.py:5:1: PTH203 `os.path.getatime` should be replaced by `Path.stat().st_a 6 | os.path.getatime(b"filename") 7 | os.path.getatime(Path("filename")) | + = help: Replace with `Path.stat(...).st_atime` PTH203.py:6:1: PTH203 `os.path.getatime` should be replaced by `Path.stat().st_atime` | @@ -18,6 +19,7 @@ PTH203.py:6:1: PTH203 `os.path.getatime` should be replaced by `Path.stat().st_a | ^^^^^^^^^^^^^^^^ PTH203 7 | os.path.getatime(Path("filename")) | + = help: Replace with `Path.stat(...).st_atime` PTH203.py:7:1: PTH203 `os.path.getatime` should be replaced by `Path.stat().st_atime` | @@ -26,6 +28,7 @@ PTH203.py:7:1: PTH203 `os.path.getatime` should be replaced by `Path.stat().st_a 7 | os.path.getatime(Path("filename")) | ^^^^^^^^^^^^^^^^ PTH203 | + = help: Replace with `Path.stat(...).st_atime` PTH203.py:10:1: PTH203 `os.path.getatime` should be replaced by `Path.stat().st_atime` | @@ -34,6 +37,7 @@ PTH203.py:10:1: PTH203 `os.path.getatime` should be replaced by `Path.stat().st_ 11 | getatime(b"filename") 12 | getatime(Path("filename")) | + = help: Replace with `Path.stat(...).st_atime` PTH203.py:11:1: PTH203 `os.path.getatime` should be replaced by `Path.stat().st_atime` | @@ -42,6 +46,7 @@ PTH203.py:11:1: PTH203 `os.path.getatime` should be replaced by `Path.stat().st_ | ^^^^^^^^ PTH203 12 | getatime(Path("filename")) | + = help: Replace with `Path.stat(...).st_atime` PTH203.py:12:1: PTH203 `os.path.getatime` should be replaced by `Path.stat().st_atime` | @@ -50,3 +55,88 @@ PTH203.py:12:1: PTH203 `os.path.getatime` should be replaced by `Path.stat().st_ 12 | getatime(Path("filename")) | ^^^^^^^^ PTH203 | + = help: Replace with `Path.stat(...).st_atime` + +PTH203.py:17:1: PTH203 `os.path.getatime` should be replaced by `Path.stat().st_atime` + | +15 | file = __file__ +16 | +17 | os.path.getatime(file) + | ^^^^^^^^^^^^^^^^ PTH203 +18 | os.path.getatime(filename="filename") +19 | os.path.getatime(filename=Path("filename")) + | + = help: Replace with `Path.stat(...).st_atime` + +PTH203.py:18:1: PTH203 `os.path.getatime` should be replaced by `Path.stat().st_atime` + | +17 | os.path.getatime(file) +18 | os.path.getatime(filename="filename") + | ^^^^^^^^^^^^^^^^ PTH203 +19 | os.path.getatime(filename=Path("filename")) + | + = help: Replace with `Path.stat(...).st_atime` + +PTH203.py:19:1: PTH203 `os.path.getatime` should be replaced by `Path.stat().st_atime` + | +17 | os.path.getatime(file) +18 | os.path.getatime(filename="filename") +19 | os.path.getatime(filename=Path("filename")) + | ^^^^^^^^^^^^^^^^ PTH203 +20 | +21 | os.path.getatime( # comment 1 + | + = help: Replace with `Path.stat(...).st_atime` + +PTH203.py:21:1: PTH203 `os.path.getatime` should be replaced by `Path.stat().st_atime` + | +19 | os.path.getatime(filename=Path("filename")) +20 | +21 | os.path.getatime( # comment 1 + | ^^^^^^^^^^^^^^^^ PTH203 +22 | # comment 2 +23 | "filename" # comment 3 + | + = help: Replace with `Path.stat(...).st_atime` + +PTH203.py:29:1: PTH203 `os.path.getatime` should be replaced by `Path.stat().st_atime` + | +27 | ) # comment 7 +28 | +29 | os.path.getatime("file" + "name") + | ^^^^^^^^^^^^^^^^ PTH203 +30 | +31 | getatime(Path("filename").resolve()) + | + = help: Replace with `Path.stat(...).st_atime` + +PTH203.py:31:1: PTH203 `os.path.getatime` should be replaced by `Path.stat().st_atime` + | +29 | os.path.getatime("file" + "name") +30 | +31 | getatime(Path("filename").resolve()) + | ^^^^^^^^ PTH203 +32 | +33 | os.path.getatime(pathlib.Path("filename")) + | + = help: Replace with `Path.stat(...).st_atime` + +PTH203.py:33:1: PTH203 `os.path.getatime` should be replaced by `Path.stat().st_atime` + | +31 | getatime(Path("filename").resolve()) +32 | +33 | os.path.getatime(pathlib.Path("filename")) + | ^^^^^^^^^^^^^^^^ PTH203 +34 | +35 | getatime(Path("dir") / "file.txt") + | + = help: Replace with `Path.stat(...).st_atime` + +PTH203.py:35:1: PTH203 `os.path.getatime` should be replaced by `Path.stat().st_atime` + | +33 | os.path.getatime(pathlib.Path("filename")) +34 | +35 | getatime(Path("dir") / "file.txt") + | ^^^^^^^^ PTH203 + | + = help: Replace with `Path.stat(...).st_atime` diff --git a/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__PTH204_PTH204.py.snap b/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__PTH204_PTH204.py.snap index b137cbd772..7e3b242486 100644 --- a/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__PTH204_PTH204.py.snap +++ b/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__PTH204_PTH204.py.snap @@ -1,6 +1,5 @@ --- source: crates/ruff_linter/src/rules/flake8_use_pathlib/mod.rs -snapshot_kind: text --- PTH204.py:6:1: PTH204 `os.path.getmtime` should be replaced by `Path.stat().st_mtime` | @@ -9,6 +8,7 @@ PTH204.py:6:1: PTH204 `os.path.getmtime` should be replaced by `Path.stat().st_m 7 | os.path.getmtime(b"filename") 8 | os.path.getmtime(Path("filename")) | + = help: Replace with `Path.stat(...).st_mtime` PTH204.py:7:1: PTH204 `os.path.getmtime` should be replaced by `Path.stat().st_mtime` | @@ -17,6 +17,7 @@ PTH204.py:7:1: PTH204 `os.path.getmtime` should be replaced by `Path.stat().st_m | ^^^^^^^^^^^^^^^^ PTH204 8 | os.path.getmtime(Path("filename")) | + = help: Replace with `Path.stat(...).st_mtime` PTH204.py:8:1: PTH204 `os.path.getmtime` should be replaced by `Path.stat().st_mtime` | @@ -25,6 +26,7 @@ PTH204.py:8:1: PTH204 `os.path.getmtime` should be replaced by `Path.stat().st_m 8 | os.path.getmtime(Path("filename")) | ^^^^^^^^^^^^^^^^ PTH204 | + = help: Replace with `Path.stat(...).st_mtime` PTH204.py:11:1: PTH204 `os.path.getmtime` should be replaced by `Path.stat().st_mtime` | @@ -33,6 +35,7 @@ PTH204.py:11:1: PTH204 `os.path.getmtime` should be replaced by `Path.stat().st_ 12 | getmtime(b"filename") 13 | getmtime(Path("filename")) | + = help: Replace with `Path.stat(...).st_mtime` PTH204.py:12:1: PTH204 `os.path.getmtime` should be replaced by `Path.stat().st_mtime` | @@ -41,6 +44,7 @@ PTH204.py:12:1: PTH204 `os.path.getmtime` should be replaced by `Path.stat().st_ | ^^^^^^^^ PTH204 13 | getmtime(Path("filename")) | + = help: Replace with `Path.stat(...).st_mtime` PTH204.py:13:1: PTH204 `os.path.getmtime` should be replaced by `Path.stat().st_mtime` | @@ -49,3 +53,4 @@ PTH204.py:13:1: PTH204 `os.path.getmtime` should be replaced by `Path.stat().st_ 13 | getmtime(Path("filename")) | ^^^^^^^^ PTH204 | + = help: Replace with `Path.stat(...).st_mtime` diff --git a/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__PTH205_PTH205.py.snap b/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__PTH205_PTH205.py.snap index 3edeac694e..a8b523d0ce 100644 --- a/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__PTH205_PTH205.py.snap +++ b/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__PTH205_PTH205.py.snap @@ -8,6 +8,7 @@ PTH205.py:6:1: PTH205 `os.path.getctime` should be replaced by `Path.stat().st_c 7 | os.path.getctime(b"filename") 8 | os.path.getctime(Path("filename")) | + = help: Replace with `Path.stat(...).st_ctime` PTH205.py:7:1: PTH205 `os.path.getctime` should be replaced by `Path.stat().st_ctime` | @@ -16,6 +17,7 @@ PTH205.py:7:1: PTH205 `os.path.getctime` should be replaced by `Path.stat().st_c | ^^^^^^^^^^^^^^^^ PTH205 8 | os.path.getctime(Path("filename")) | + = help: Replace with `Path.stat(...).st_ctime` PTH205.py:8:1: PTH205 `os.path.getctime` should be replaced by `Path.stat().st_ctime` | @@ -26,6 +28,7 @@ PTH205.py:8:1: PTH205 `os.path.getctime` should be replaced by `Path.stat().st_c 9 | 10 | getctime("filename") | + = help: Replace with `Path.stat(...).st_ctime` PTH205.py:10:1: PTH205 `os.path.getctime` should be replaced by `Path.stat().st_ctime` | @@ -36,6 +39,7 @@ PTH205.py:10:1: PTH205 `os.path.getctime` should be replaced by `Path.stat().st_ 11 | getctime(b"filename") 12 | getctime(Path("filename")) | + = help: Replace with `Path.stat(...).st_ctime` PTH205.py:11:1: PTH205 `os.path.getctime` should be replaced by `Path.stat().st_ctime` | @@ -44,6 +48,7 @@ PTH205.py:11:1: PTH205 `os.path.getctime` should be replaced by `Path.stat().st_ | ^^^^^^^^ PTH205 12 | getctime(Path("filename")) | + = help: Replace with `Path.stat(...).st_ctime` PTH205.py:12:1: PTH205 `os.path.getctime` should be replaced by `Path.stat().st_ctime` | @@ -52,3 +57,4 @@ PTH205.py:12:1: PTH205 `os.path.getctime` should be replaced by `Path.stat().st_ 12 | getctime(Path("filename")) | ^^^^^^^^ PTH205 | + = help: Replace with `Path.stat(...).st_ctime` 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 index 155b2d8307..35a650e9db 100644 --- 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 @@ -4,7 +4,7 @@ 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 + | ^^^^^^^^^^^^^^^ PTH202 11 | os.path.getsize(b"filename") 12 | os.path.getsize(Path("filename")) | @@ -24,7 +24,7 @@ PTH202.py:11:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat(). | 10 | os.path.getsize("filename") 11 | os.path.getsize(b"filename") - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^^^^^^^^^ PTH202 12 | os.path.getsize(Path("filename")) 13 | os.path.getsize(__file__) | @@ -45,7 +45,7 @@ PTH202.py:12:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat(). 10 | os.path.getsize("filename") 11 | os.path.getsize(b"filename") 12 | os.path.getsize(Path("filename")) - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^^^^^^^^^ PTH202 13 | os.path.getsize(__file__) | = help: Replace with `Path(...).stat().st_size` @@ -65,7 +65,7 @@ PTH202.py:13:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat(). 11 | os.path.getsize(b"filename") 12 | os.path.getsize(Path("filename")) 13 | os.path.getsize(__file__) - | ^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^^^^^^^^^ PTH202 14 | 15 | os.path.getsize(filename) | @@ -86,7 +86,7 @@ PTH202.py:15:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat(). 13 | os.path.getsize(__file__) 14 | 15 | os.path.getsize(filename) - | ^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^^^^^^^^^ PTH202 16 | os.path.getsize(filename1) 17 | os.path.getsize(filename2) | @@ -106,7 +106,7 @@ PTH202.py:16:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat(). | 15 | os.path.getsize(filename) 16 | os.path.getsize(filename1) - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^^^^^^^^^ PTH202 17 | os.path.getsize(filename2) | = help: Replace with `Path(...).stat().st_size` @@ -126,7 +126,7 @@ PTH202.py:17:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat(). 15 | os.path.getsize(filename) 16 | os.path.getsize(filename1) 17 | os.path.getsize(filename2) - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^^^^^^^^^ PTH202 18 | 19 | os.path.getsize(filename="filename") | @@ -147,7 +147,7 @@ PTH202.py:19:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat(). 17 | os.path.getsize(filename2) 18 | 19 | os.path.getsize(filename="filename") - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^^^^^^^^^ PTH202 20 | os.path.getsize(filename=b"filename") 21 | os.path.getsize(filename=Path("filename")) | @@ -167,7 +167,7 @@ PTH202.py:20:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat(). | 19 | os.path.getsize(filename="filename") 20 | os.path.getsize(filename=b"filename") - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^^^^^^^^^ PTH202 21 | os.path.getsize(filename=Path("filename")) 22 | os.path.getsize(filename=__file__) | @@ -188,7 +188,7 @@ PTH202.py:21:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat(). 19 | os.path.getsize(filename="filename") 20 | os.path.getsize(filename=b"filename") 21 | os.path.getsize(filename=Path("filename")) - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^^^^^^^^^ PTH202 22 | os.path.getsize(filename=__file__) | = help: Replace with `Path(...).stat().st_size` @@ -208,7 +208,7 @@ PTH202.py:22:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat(). 20 | os.path.getsize(filename=b"filename") 21 | os.path.getsize(filename=Path("filename")) 22 | os.path.getsize(filename=__file__) - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^^^^^^^^^ PTH202 23 | 24 | getsize("filename") | @@ -229,7 +229,7 @@ PTH202.py:24:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat(). 22 | os.path.getsize(filename=__file__) 23 | 24 | getsize("filename") - | ^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^ PTH202 25 | getsize(b"filename") 26 | getsize(Path("filename")) | @@ -249,7 +249,7 @@ PTH202.py:25:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat(). | 24 | getsize("filename") 25 | getsize(b"filename") - | ^^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^ PTH202 26 | getsize(Path("filename")) 27 | getsize(__file__) | @@ -270,7 +270,7 @@ PTH202.py:26:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat(). 24 | getsize("filename") 25 | getsize(b"filename") 26 | getsize(Path("filename")) - | ^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^ PTH202 27 | getsize(__file__) | = help: Replace with `Path(...).stat().st_size` @@ -290,7 +290,7 @@ PTH202.py:27:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat(). 25 | getsize(b"filename") 26 | getsize(Path("filename")) 27 | getsize(__file__) - | ^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^ PTH202 28 | 29 | getsize(filename="filename") | @@ -311,7 +311,7 @@ PTH202.py:29:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat(). 27 | getsize(__file__) 28 | 29 | getsize(filename="filename") - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^ PTH202 30 | getsize(filename=b"filename") 31 | getsize(filename=Path("filename")) | @@ -331,7 +331,7 @@ PTH202.py:30:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat(). | 29 | getsize(filename="filename") 30 | getsize(filename=b"filename") - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^ PTH202 31 | getsize(filename=Path("filename")) 32 | getsize(filename=__file__) | @@ -352,7 +352,7 @@ PTH202.py:31:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat(). 29 | getsize(filename="filename") 30 | getsize(filename=b"filename") 31 | getsize(filename=Path("filename")) - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^ PTH202 32 | getsize(filename=__file__) | = help: Replace with `Path(...).stat().st_size` @@ -372,7 +372,7 @@ PTH202.py:32:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat(). 30 | getsize(filename=b"filename") 31 | getsize(filename=Path("filename")) 32 | getsize(filename=__file__) - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^ PTH202 33 | 34 | getsize(filename) | @@ -393,7 +393,7 @@ PTH202.py:34:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat(). 32 | getsize(filename=__file__) 33 | 34 | getsize(filename) - | ^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^ PTH202 35 | getsize(filename1) 36 | getsize(filename2) | @@ -413,7 +413,7 @@ PTH202.py:35:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat(). | 34 | getsize(filename) 35 | getsize(filename1) - | ^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^ PTH202 36 | getsize(filename2) | = help: Replace with `Path(...).stat().st_size` @@ -433,7 +433,7 @@ PTH202.py:36:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat(). 34 | getsize(filename) 35 | getsize(filename1) 36 | getsize(filename2) - | ^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^ PTH202 | = help: Replace with `Path(...).stat().st_size` @@ -449,12 +449,10 @@ PTH202.py:36:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat(). 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( +39 | os.path.getsize( + | ^^^^^^^^^^^^^^^ PTH202 +40 | "filename", # comment +41 | ) | = help: Replace with `Path(...).stat().st_size` @@ -472,17 +470,12 @@ PTH202.py:39:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat(). PTH202.py:43:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size` | -41 | ) +41 | ) 42 | -43 | / os.path.getsize( -44 | | # comment -45 | | "filename" -46 | | , -47 | | # comment -48 | | ) - | |_^ PTH202 -49 | -50 | os.path.getsize( +43 | os.path.getsize( + | ^^^^^^^^^^^^^^^ PTH202 +44 | # comment +45 | "filename" | = help: Replace with `Path(...).stat().st_size` @@ -503,16 +496,12 @@ PTH202.py:43:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat(). PTH202.py:50:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size` | -48 | ) +48 | ) 49 | -50 | / os.path.getsize( -51 | | # comment -52 | | b"filename" -53 | | # comment -54 | | ) - | |_^ PTH202 -55 | -56 | os.path.getsize( # comment +50 | os.path.getsize( + | ^^^^^^^^^^^^^^^ PTH202 +51 | # comment +52 | b"filename" | = help: Replace with `Path(...).stat().st_size` @@ -532,15 +521,12 @@ PTH202.py:50:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat(). PTH202.py:56:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size` | -54 | ) +54 | ) 55 | -56 | / os.path.getsize( # comment -57 | | Path(__file__) -58 | | # comment -59 | | ) # comment - | |_^ PTH202 -60 | -61 | getsize( # comment +56 | os.path.getsize( # comment + | ^^^^^^^^^^^^^^^ PTH202 +57 | Path(__file__) +58 | # comment | = help: Replace with `Path(...).stat().st_size` @@ -559,13 +545,11 @@ PTH202.py:56:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat(). PTH202.py:61:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size` | -59 | ) # comment +59 | ) # comment 60 | -61 | / getsize( # comment -62 | | "filename") - | |_______________^ PTH202 -63 | -64 | getsize( # comment +61 | getsize( # comment + | ^^^^^^^ PTH202 +62 | "filename") | = help: Replace with `Path(...).stat().st_size` @@ -582,15 +566,12 @@ PTH202.py:61:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat(). PTH202.py:64:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size` | -62 | "filename") +62 | "filename") 63 | -64 | / getsize( # comment -65 | | b"filename", -66 | | #comment -67 | | ) - | |_^ PTH202 -68 | -69 | os.path.getsize("file" + "name") +64 | getsize( # comment + | ^^^^^^^ PTH202 +65 | b"filename", +66 | #comment | = help: Replace with `Path(...).stat().st_size` @@ -612,7 +593,7 @@ PTH202.py:69:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat(). 67 | ) 68 | 69 | os.path.getsize("file" + "name") - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^^^^^^^^^ PTH202 70 | 71 | getsize \ | @@ -630,17 +611,12 @@ PTH202.py:69:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat(). PTH202.py:71:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat().st_size` | -69 | os.path.getsize("file" + "name") +69 | os.path.getsize("file" + "name") 70 | -71 | / getsize \ -72 | | \ -73 | | \ -74 | | ( # comment -75 | | "filename", -76 | | ) - | |_____^ PTH202 -77 | -78 | getsize(Path("filename").resolve()) +71 | getsize \ + | ^^^^^^^ PTH202 +72 | \ +73 | \ | = help: Replace with `Path(...).stat().st_size` @@ -664,7 +640,7 @@ PTH202.py:78:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat(). 76 | ) 77 | 78 | getsize(Path("filename").resolve()) - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^ PTH202 79 | 80 | import pathlib | @@ -685,7 +661,7 @@ PTH202.py:82:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat(). 80 | import pathlib 81 | 82 | os.path.getsize(pathlib.Path("filename")) - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^^^^^^^^^ 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_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 index 23ae9b5bff..a61e87ae2d 100644 --- 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 @@ -6,7 +6,7 @@ PTH202_2.py:3:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat() 1 | import os 2 | 3 | os.path.getsize(filename="filename") - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^^^^^^^^^ PTH202 4 | os.path.getsize(filename=b"filename") 5 | os.path.getsize(filename=__file__) | @@ -25,7 +25,7 @@ PTH202_2.py:4:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat() | 3 | os.path.getsize(filename="filename") 4 | os.path.getsize(filename=b"filename") - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^^^^^^^^^ PTH202 5 | os.path.getsize(filename=__file__) | = help: Replace with `Path(...).stat().st_size` @@ -44,7 +44,7 @@ PTH202_2.py:5:1: PTH202 [*] `os.path.getsize` should be replaced by `Path.stat() 3 | os.path.getsize(filename="filename") 4 | os.path.getsize(filename=b"filename") 5 | os.path.getsize(filename=__file__) - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH202 + | ^^^^^^^^^^^^^^^ 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__PTH203_PTH203.py.snap b/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__preview__PTH203_PTH203.py.snap new file mode 100644 index 0000000000..8a02cfe4ff --- /dev/null +++ b/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__preview__PTH203_PTH203.py.snap @@ -0,0 +1,284 @@ +--- +source: crates/ruff_linter/src/rules/flake8_use_pathlib/mod.rs +--- +PTH203.py:5:1: PTH203 [*] `os.path.getatime` should be replaced by `Path.stat().st_atime` + | +3 | from os.path import getatime +4 | +5 | os.path.getatime("filename") + | ^^^^^^^^^^^^^^^^ PTH203 +6 | os.path.getatime(b"filename") +7 | os.path.getatime(Path("filename")) + | + = help: Replace with `Path.stat(...).st_atime` + +ℹ Safe fix +2 2 | from pathlib import Path +3 3 | from os.path import getatime +4 4 | +5 |-os.path.getatime("filename") + 5 |+Path("filename").stat().st_atime +6 6 | os.path.getatime(b"filename") +7 7 | os.path.getatime(Path("filename")) +8 8 | + +PTH203.py:6:1: PTH203 [*] `os.path.getatime` should be replaced by `Path.stat().st_atime` + | +5 | os.path.getatime("filename") +6 | os.path.getatime(b"filename") + | ^^^^^^^^^^^^^^^^ PTH203 +7 | os.path.getatime(Path("filename")) + | + = help: Replace with `Path.stat(...).st_atime` + +ℹ Safe fix +3 3 | from os.path import getatime +4 4 | +5 5 | os.path.getatime("filename") +6 |-os.path.getatime(b"filename") + 6 |+Path(b"filename").stat().st_atime +7 7 | os.path.getatime(Path("filename")) +8 8 | +9 9 | + +PTH203.py:7:1: PTH203 [*] `os.path.getatime` should be replaced by `Path.stat().st_atime` + | +5 | os.path.getatime("filename") +6 | os.path.getatime(b"filename") +7 | os.path.getatime(Path("filename")) + | ^^^^^^^^^^^^^^^^ PTH203 + | + = help: Replace with `Path.stat(...).st_atime` + +ℹ Safe fix +4 4 | +5 5 | os.path.getatime("filename") +6 6 | os.path.getatime(b"filename") +7 |-os.path.getatime(Path("filename")) + 7 |+Path("filename").stat().st_atime +8 8 | +9 9 | +10 10 | getatime("filename") + +PTH203.py:10:1: PTH203 [*] `os.path.getatime` should be replaced by `Path.stat().st_atime` + | +10 | getatime("filename") + | ^^^^^^^^ PTH203 +11 | getatime(b"filename") +12 | getatime(Path("filename")) + | + = help: Replace with `Path.stat(...).st_atime` + +ℹ Safe fix +7 7 | os.path.getatime(Path("filename")) +8 8 | +9 9 | +10 |-getatime("filename") + 10 |+Path("filename").stat().st_atime +11 11 | getatime(b"filename") +12 12 | getatime(Path("filename")) +13 13 | + +PTH203.py:11:1: PTH203 [*] `os.path.getatime` should be replaced by `Path.stat().st_atime` + | +10 | getatime("filename") +11 | getatime(b"filename") + | ^^^^^^^^ PTH203 +12 | getatime(Path("filename")) + | + = help: Replace with `Path.stat(...).st_atime` + +ℹ Safe fix +8 8 | +9 9 | +10 10 | getatime("filename") +11 |-getatime(b"filename") + 11 |+Path(b"filename").stat().st_atime +12 12 | getatime(Path("filename")) +13 13 | +14 14 | + +PTH203.py:12:1: PTH203 [*] `os.path.getatime` should be replaced by `Path.stat().st_atime` + | +10 | getatime("filename") +11 | getatime(b"filename") +12 | getatime(Path("filename")) + | ^^^^^^^^ PTH203 + | + = help: Replace with `Path.stat(...).st_atime` + +ℹ Safe fix +9 9 | +10 10 | getatime("filename") +11 11 | getatime(b"filename") +12 |-getatime(Path("filename")) + 12 |+Path("filename").stat().st_atime +13 13 | +14 14 | +15 15 | file = __file__ + +PTH203.py:17:1: PTH203 [*] `os.path.getatime` should be replaced by `Path.stat().st_atime` + | +15 | file = __file__ +16 | +17 | os.path.getatime(file) + | ^^^^^^^^^^^^^^^^ PTH203 +18 | os.path.getatime(filename="filename") +19 | os.path.getatime(filename=Path("filename")) + | + = help: Replace with `Path.stat(...).st_atime` + +ℹ Safe fix +14 14 | +15 15 | file = __file__ +16 16 | +17 |-os.path.getatime(file) + 17 |+Path(file).stat().st_atime +18 18 | os.path.getatime(filename="filename") +19 19 | os.path.getatime(filename=Path("filename")) +20 20 | + +PTH203.py:18:1: PTH203 [*] `os.path.getatime` should be replaced by `Path.stat().st_atime` + | +17 | os.path.getatime(file) +18 | os.path.getatime(filename="filename") + | ^^^^^^^^^^^^^^^^ PTH203 +19 | os.path.getatime(filename=Path("filename")) + | + = help: Replace with `Path.stat(...).st_atime` + +ℹ Safe fix +15 15 | file = __file__ +16 16 | +17 17 | os.path.getatime(file) +18 |-os.path.getatime(filename="filename") + 18 |+Path("filename").stat().st_atime +19 19 | os.path.getatime(filename=Path("filename")) +20 20 | +21 21 | os.path.getatime( # comment 1 + +PTH203.py:19:1: PTH203 [*] `os.path.getatime` should be replaced by `Path.stat().st_atime` + | +17 | os.path.getatime(file) +18 | os.path.getatime(filename="filename") +19 | os.path.getatime(filename=Path("filename")) + | ^^^^^^^^^^^^^^^^ PTH203 +20 | +21 | os.path.getatime( # comment 1 + | + = help: Replace with `Path.stat(...).st_atime` + +ℹ Safe fix +16 16 | +17 17 | os.path.getatime(file) +18 18 | os.path.getatime(filename="filename") +19 |-os.path.getatime(filename=Path("filename")) + 19 |+Path("filename").stat().st_atime +20 20 | +21 21 | os.path.getatime( # comment 1 +22 22 | # comment 2 + +PTH203.py:21:1: PTH203 [*] `os.path.getatime` should be replaced by `Path.stat().st_atime` + | +19 | os.path.getatime(filename=Path("filename")) +20 | +21 | os.path.getatime( # comment 1 + | ^^^^^^^^^^^^^^^^ PTH203 +22 | # comment 2 +23 | "filename" # comment 3 + | + = help: Replace with `Path.stat(...).st_atime` + +ℹ Unsafe fix +18 18 | os.path.getatime(filename="filename") +19 19 | os.path.getatime(filename=Path("filename")) +20 20 | +21 |-os.path.getatime( # comment 1 +22 |- # comment 2 +23 |- "filename" # comment 3 +24 |- # comment 4 +25 |- , # comment 5 +26 |- # comment 6 +27 |-) # comment 7 + 21 |+Path("filename").stat().st_atime # comment 7 +28 22 | +29 23 | os.path.getatime("file" + "name") +30 24 | + +PTH203.py:29:1: PTH203 [*] `os.path.getatime` should be replaced by `Path.stat().st_atime` + | +27 | ) # comment 7 +28 | +29 | os.path.getatime("file" + "name") + | ^^^^^^^^^^^^^^^^ PTH203 +30 | +31 | getatime(Path("filename").resolve()) + | + = help: Replace with `Path.stat(...).st_atime` + +ℹ Safe fix +26 26 | # comment 6 +27 27 | ) # comment 7 +28 28 | +29 |-os.path.getatime("file" + "name") + 29 |+Path("file" + "name").stat().st_atime +30 30 | +31 31 | getatime(Path("filename").resolve()) +32 32 | + +PTH203.py:31:1: PTH203 [*] `os.path.getatime` should be replaced by `Path.stat().st_atime` + | +29 | os.path.getatime("file" + "name") +30 | +31 | getatime(Path("filename").resolve()) + | ^^^^^^^^ PTH203 +32 | +33 | os.path.getatime(pathlib.Path("filename")) + | + = help: Replace with `Path.stat(...).st_atime` + +ℹ Safe fix +28 28 | +29 29 | os.path.getatime("file" + "name") +30 30 | +31 |-getatime(Path("filename").resolve()) + 31 |+Path(Path("filename").resolve()).stat().st_atime +32 32 | +33 33 | os.path.getatime(pathlib.Path("filename")) +34 34 | + +PTH203.py:33:1: PTH203 [*] `os.path.getatime` should be replaced by `Path.stat().st_atime` + | +31 | getatime(Path("filename").resolve()) +32 | +33 | os.path.getatime(pathlib.Path("filename")) + | ^^^^^^^^^^^^^^^^ PTH203 +34 | +35 | getatime(Path("dir") / "file.txt") + | + = help: Replace with `Path.stat(...).st_atime` + +ℹ Safe fix +30 30 | +31 31 | getatime(Path("filename").resolve()) +32 32 | +33 |-os.path.getatime(pathlib.Path("filename")) + 33 |+pathlib.Path("filename").stat().st_atime +34 34 | +35 35 | getatime(Path("dir") / "file.txt") + +PTH203.py:35:1: PTH203 [*] `os.path.getatime` should be replaced by `Path.stat().st_atime` + | +33 | os.path.getatime(pathlib.Path("filename")) +34 | +35 | getatime(Path("dir") / "file.txt") + | ^^^^^^^^ PTH203 + | + = help: Replace with `Path.stat(...).st_atime` + +ℹ Safe fix +32 32 | +33 33 | os.path.getatime(pathlib.Path("filename")) +34 34 | +35 |-getatime(Path("dir") / "file.txt") + 35 |+Path(Path("dir") / "file.txt").stat().st_atime diff --git a/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__preview__PTH204_PTH204.py.snap b/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__preview__PTH204_PTH204.py.snap new file mode 100644 index 0000000000..b63229a19b --- /dev/null +++ b/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__preview__PTH204_PTH204.py.snap @@ -0,0 +1,110 @@ +--- +source: crates/ruff_linter/src/rules/flake8_use_pathlib/mod.rs +--- +PTH204.py:6:1: PTH204 [*] `os.path.getmtime` should be replaced by `Path.stat().st_mtime` + | +6 | os.path.getmtime("filename") + | ^^^^^^^^^^^^^^^^ PTH204 +7 | os.path.getmtime(b"filename") +8 | os.path.getmtime(Path("filename")) + | + = help: Replace with `Path.stat(...).st_mtime` + +ℹ Safe fix +3 3 | from os.path import getmtime +4 4 | +5 5 | +6 |-os.path.getmtime("filename") + 6 |+Path("filename").stat().st_mtime +7 7 | os.path.getmtime(b"filename") +8 8 | os.path.getmtime(Path("filename")) +9 9 | + +PTH204.py:7:1: PTH204 [*] `os.path.getmtime` should be replaced by `Path.stat().st_mtime` + | +6 | os.path.getmtime("filename") +7 | os.path.getmtime(b"filename") + | ^^^^^^^^^^^^^^^^ PTH204 +8 | os.path.getmtime(Path("filename")) + | + = help: Replace with `Path.stat(...).st_mtime` + +ℹ Safe fix +4 4 | +5 5 | +6 6 | os.path.getmtime("filename") +7 |-os.path.getmtime(b"filename") + 7 |+Path(b"filename").stat().st_mtime +8 8 | os.path.getmtime(Path("filename")) +9 9 | +10 10 | + +PTH204.py:8:1: PTH204 [*] `os.path.getmtime` should be replaced by `Path.stat().st_mtime` + | +6 | os.path.getmtime("filename") +7 | os.path.getmtime(b"filename") +8 | os.path.getmtime(Path("filename")) + | ^^^^^^^^^^^^^^^^ PTH204 + | + = help: Replace with `Path.stat(...).st_mtime` + +ℹ Safe fix +5 5 | +6 6 | os.path.getmtime("filename") +7 7 | os.path.getmtime(b"filename") +8 |-os.path.getmtime(Path("filename")) + 8 |+Path("filename").stat().st_mtime +9 9 | +10 10 | +11 11 | getmtime("filename") + +PTH204.py:11:1: PTH204 [*] `os.path.getmtime` should be replaced by `Path.stat().st_mtime` + | +11 | getmtime("filename") + | ^^^^^^^^ PTH204 +12 | getmtime(b"filename") +13 | getmtime(Path("filename")) + | + = help: Replace with `Path.stat(...).st_mtime` + +ℹ Safe fix +8 8 | os.path.getmtime(Path("filename")) +9 9 | +10 10 | +11 |-getmtime("filename") + 11 |+Path("filename").stat().st_mtime +12 12 | getmtime(b"filename") +13 13 | getmtime(Path("filename")) + +PTH204.py:12:1: PTH204 [*] `os.path.getmtime` should be replaced by `Path.stat().st_mtime` + | +11 | getmtime("filename") +12 | getmtime(b"filename") + | ^^^^^^^^ PTH204 +13 | getmtime(Path("filename")) + | + = help: Replace with `Path.stat(...).st_mtime` + +ℹ Safe fix +9 9 | +10 10 | +11 11 | getmtime("filename") +12 |-getmtime(b"filename") + 12 |+Path(b"filename").stat().st_mtime +13 13 | getmtime(Path("filename")) + +PTH204.py:13:1: PTH204 [*] `os.path.getmtime` should be replaced by `Path.stat().st_mtime` + | +11 | getmtime("filename") +12 | getmtime(b"filename") +13 | getmtime(Path("filename")) + | ^^^^^^^^ PTH204 + | + = help: Replace with `Path.stat(...).st_mtime` + +ℹ Safe fix +10 10 | +11 11 | getmtime("filename") +12 12 | getmtime(b"filename") +13 |-getmtime(Path("filename")) + 13 |+Path("filename").stat().st_mtime diff --git a/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__preview__PTH205_PTH205.py.snap b/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__preview__PTH205_PTH205.py.snap new file mode 100644 index 0000000000..3d675e9241 --- /dev/null +++ b/crates/ruff_linter/src/rules/flake8_use_pathlib/snapshots/ruff_linter__rules__flake8_use_pathlib__tests__preview__PTH205_PTH205.py.snap @@ -0,0 +1,114 @@ +--- +source: crates/ruff_linter/src/rules/flake8_use_pathlib/mod.rs +--- +PTH205.py:6:1: PTH205 [*] `os.path.getctime` should be replaced by `Path.stat().st_ctime` + | +6 | os.path.getctime("filename") + | ^^^^^^^^^^^^^^^^ PTH205 +7 | os.path.getctime(b"filename") +8 | os.path.getctime(Path("filename")) + | + = help: Replace with `Path.stat(...).st_ctime` + +ℹ Safe fix +3 3 | from os.path import getctime +4 4 | +5 5 | +6 |-os.path.getctime("filename") + 6 |+Path("filename").stat().st_ctime +7 7 | os.path.getctime(b"filename") +8 8 | os.path.getctime(Path("filename")) +9 9 | + +PTH205.py:7:1: PTH205 [*] `os.path.getctime` should be replaced by `Path.stat().st_ctime` + | +6 | os.path.getctime("filename") +7 | os.path.getctime(b"filename") + | ^^^^^^^^^^^^^^^^ PTH205 +8 | os.path.getctime(Path("filename")) + | + = help: Replace with `Path.stat(...).st_ctime` + +ℹ Safe fix +4 4 | +5 5 | +6 6 | os.path.getctime("filename") +7 |-os.path.getctime(b"filename") + 7 |+Path(b"filename").stat().st_ctime +8 8 | os.path.getctime(Path("filename")) +9 9 | +10 10 | getctime("filename") + +PTH205.py:8:1: PTH205 [*] `os.path.getctime` should be replaced by `Path.stat().st_ctime` + | + 6 | os.path.getctime("filename") + 7 | os.path.getctime(b"filename") + 8 | os.path.getctime(Path("filename")) + | ^^^^^^^^^^^^^^^^ PTH205 + 9 | +10 | getctime("filename") + | + = help: Replace with `Path.stat(...).st_ctime` + +ℹ Safe fix +5 5 | +6 6 | os.path.getctime("filename") +7 7 | os.path.getctime(b"filename") +8 |-os.path.getctime(Path("filename")) + 8 |+Path("filename").stat().st_ctime +9 9 | +10 10 | getctime("filename") +11 11 | getctime(b"filename") + +PTH205.py:10:1: PTH205 [*] `os.path.getctime` should be replaced by `Path.stat().st_ctime` + | + 8 | os.path.getctime(Path("filename")) + 9 | +10 | getctime("filename") + | ^^^^^^^^ PTH205 +11 | getctime(b"filename") +12 | getctime(Path("filename")) + | + = help: Replace with `Path.stat(...).st_ctime` + +ℹ Safe fix +7 7 | os.path.getctime(b"filename") +8 8 | os.path.getctime(Path("filename")) +9 9 | +10 |-getctime("filename") + 10 |+Path("filename").stat().st_ctime +11 11 | getctime(b"filename") +12 12 | getctime(Path("filename")) + +PTH205.py:11:1: PTH205 [*] `os.path.getctime` should be replaced by `Path.stat().st_ctime` + | +10 | getctime("filename") +11 | getctime(b"filename") + | ^^^^^^^^ PTH205 +12 | getctime(Path("filename")) + | + = help: Replace with `Path.stat(...).st_ctime` + +ℹ Safe fix +8 8 | os.path.getctime(Path("filename")) +9 9 | +10 10 | getctime("filename") +11 |-getctime(b"filename") + 11 |+Path(b"filename").stat().st_ctime +12 12 | getctime(Path("filename")) + +PTH205.py:12:1: PTH205 [*] `os.path.getctime` should be replaced by `Path.stat().st_ctime` + | +10 | getctime("filename") +11 | getctime(b"filename") +12 | getctime(Path("filename")) + | ^^^^^^^^ PTH205 + | + = help: Replace with `Path.stat(...).st_ctime` + +ℹ Safe fix +9 9 | +10 10 | getctime("filename") +11 11 | getctime(b"filename") +12 |-getctime(Path("filename")) + 12 |+Path("filename").stat().st_ctime