From be451f5897b74dfdf57ddb72473d2a0a7920a4bf Mon Sep 17 00:00:00 2001 From: Simon Brugman Date: Sun, 22 Jan 2023 20:39:00 +0100 Subject: [PATCH] feat: final rule for this plugin (PTH024) --- README.md | 1 + .../test/fixtures/flake8_use_pathlib/py_path_1.py | 3 +++ .../test/fixtures/flake8_use_pathlib/py_path_2.py | 3 +++ ruff.schema.json | 1 + src/checkers/ast.rs | 1 + src/registry.rs | 1 + src/rules/flake8_use_pathlib/helpers.rs | 9 ++++++--- src/rules/flake8_use_pathlib/mod.rs | 14 ++++++++++++++ ...8_use_pathlib__tests__PTH124_py_path_1.py.snap | 15 +++++++++++++++ ...8_use_pathlib__tests__PTH124_py_path_2.py.snap | 15 +++++++++++++++ src/rules/flake8_use_pathlib/violations.rs | 11 +++++++++++ 11 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 resources/test/fixtures/flake8_use_pathlib/py_path_1.py create mode 100644 resources/test/fixtures/flake8_use_pathlib/py_path_2.py create mode 100644 src/rules/flake8_use_pathlib/snapshots/ruff__rules__flake8_use_pathlib__tests__PTH124_py_path_1.py.snap create mode 100644 src/rules/flake8_use_pathlib/snapshots/ruff__rules__flake8_use_pathlib__tests__PTH124_py_path_2.py.snap diff --git a/README.md b/README.md index c585170c6dff24..90a6580fc01708 100644 --- a/README.md +++ b/README.md @@ -1228,6 +1228,7 @@ For more, see [flake8-use-pathlib](https://pypi.org/project/flake8-use-pathlib/) | PTH121 | pathlib-samefile | `os.path.samefile` should be replaced by `.samefile()` | | | PTH122 | pathlib-splitext | `os.path.splitext` should be replaced by `.suffix` | | | PTH123 | pathlib-open | `open("foo")` should be replaced by`Path("foo").open()` | | +| PTH124 | pathlib-py-path | `py.path` is in maintenance mode, use pathlib instead | | ### Ruff-specific rules (RUF) diff --git a/resources/test/fixtures/flake8_use_pathlib/py_path_1.py b/resources/test/fixtures/flake8_use_pathlib/py_path_1.py new file mode 100644 index 00000000000000..27c6205c813509 --- /dev/null +++ b/resources/test/fixtures/flake8_use_pathlib/py_path_1.py @@ -0,0 +1,3 @@ +import py + +p = py.path.local("../foo") diff --git a/resources/test/fixtures/flake8_use_pathlib/py_path_2.py b/resources/test/fixtures/flake8_use_pathlib/py_path_2.py new file mode 100644 index 00000000000000..fc25529fb2e908 --- /dev/null +++ b/resources/test/fixtures/flake8_use_pathlib/py_path_2.py @@ -0,0 +1,3 @@ +from py.path import local as path + +p = path("/foo") diff --git a/ruff.schema.json b/ruff.schema.json index 68f5d083ddd914..792a4096ad8ad5 100644 --- a/ruff.schema.json +++ b/ruff.schema.json @@ -1662,6 +1662,7 @@ "PTH121", "PTH122", "PTH123", + "PTH124", "Q", "Q0", "Q00", diff --git a/src/checkers/ast.rs b/src/checkers/ast.rs index 4df5bde4c00302..94c5045e948ae1 100644 --- a/src/checkers/ast.rs +++ b/src/checkers/ast.rs @@ -2570,6 +2570,7 @@ where || self.settings.rules.enabled(&Rule::PathlibSamefile) || self.settings.rules.enabled(&Rule::PathlibSplitext) || self.settings.rules.enabled(&Rule::PathlibOpen) + || self.settings.rules.enabled(&Rule::PathlibPyPath) { flake8_use_pathlib::helpers::replaceable_by_pathlib(self, func); } diff --git a/src/registry.rs b/src/registry.rs index 0e508693762f5d..4d66462b283412 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -454,6 +454,7 @@ ruff_macros::define_rule_mapping!( PTH121 => rules::flake8_use_pathlib::violations::PathlibSamefile, PTH122 => rules::flake8_use_pathlib::violations::PathlibSplitext, PTH123 => rules::flake8_use_pathlib::violations::PathlibOpen, + PTH124 => rules::flake8_use_pathlib::violations::PathlibPyPath, // ruff RUF001 => violations::AmbiguousUnicodeCharacterString, RUF002 => violations::AmbiguousUnicodeCharacterDocstring, diff --git a/src/rules/flake8_use_pathlib/helpers.rs b/src/rules/flake8_use_pathlib/helpers.rs index 74cea9be2a6fe0..fbde27bc4242b6 100644 --- a/src/rules/flake8_use_pathlib/helpers.rs +++ b/src/rules/flake8_use_pathlib/helpers.rs @@ -6,9 +6,9 @@ use crate::registry::{Diagnostic, DiagnosticKind}; use crate::rules::flake8_use_pathlib::violations::{ PathlibAbspath, PathlibBasename, PathlibChmod, PathlibDirname, PathlibExists, PathlibExpanduser, PathlibGetcwd, PathlibIsAbs, PathlibIsDir, PathlibIsFile, PathlibIsLink, - PathlibJoin, PathlibMakedirs, PathlibMkdir, PathlibOpen, PathlibReadlink, PathlibRemove, - PathlibRename, PathlibReplace, PathlibRmdir, PathlibSamefile, PathlibSplitext, PathlibStat, - PathlibUnlink, + PathlibJoin, PathlibMakedirs, PathlibMkdir, PathlibOpen, PathlibPyPath, PathlibReadlink, + PathlibRemove, PathlibRename, PathlibReplace, PathlibRmdir, PathlibSamefile, PathlibSplitext, + PathlibStat, PathlibUnlink, }; enum OsCall { @@ -36,6 +36,7 @@ enum OsCall { Samefile, Splitext, Open, + PyPath, } pub fn replaceable_by_pathlib(checker: &mut Checker, expr: &Expr) { @@ -67,6 +68,7 @@ pub fn replaceable_by_pathlib(checker: &mut Checker, expr: &Expr) { ["os", "path", "samefile"] => Some(OsCall::Samefile), ["os", "path", "splitext"] => Some(OsCall::Splitext), ["", "open"] => Some(OsCall::Open), + ["py", "path", ""] => Some(OsCall::PyPath), _ => None, }) { @@ -96,6 +98,7 @@ pub fn replaceable_by_pathlib(checker: &mut Checker, expr: &Expr) { OsCall::Samefile => PathlibSamefile.into(), OsCall::Splitext => PathlibSplitext.into(), OsCall::Open => PathlibOpen.into(), + OsCall::PyPath => PathlibPyPath.into(), }, Range::from_located(expr), ); diff --git a/src/rules/flake8_use_pathlib/mod.rs b/src/rules/flake8_use_pathlib/mod.rs index a632d262a78392..27944b701469d6 100644 --- a/src/rules/flake8_use_pathlib/mod.rs +++ b/src/rules/flake8_use_pathlib/mod.rs @@ -53,4 +53,18 @@ mod tests { insta::assert_yaml_snapshot!(snapshot, diagnostics); Ok(()) } + + #[test_case(Rule::PathlibPyPath, Path::new("py_path_1.py"); "PTH024_1")] + #[test_case(Rule::PathlibPyPath, Path::new("py_path_2.py"); "PTH024_2")] + fn rules_pypath(rule_code: Rule, path: &Path) -> Result<()> { + let snapshot = format!("{}_{}", rule_code.code(), path.to_string_lossy()); + let diagnostics = test_path( + Path::new("./resources/test/fixtures/flake8_use_pathlib") + .join(path) + .as_path(), + &settings::Settings::for_rule(rule_code), + )?; + insta::assert_yaml_snapshot!(snapshot, diagnostics); + Ok(()) + } } diff --git a/src/rules/flake8_use_pathlib/snapshots/ruff__rules__flake8_use_pathlib__tests__PTH124_py_path_1.py.snap b/src/rules/flake8_use_pathlib/snapshots/ruff__rules__flake8_use_pathlib__tests__PTH124_py_path_1.py.snap new file mode 100644 index 00000000000000..b27363952472b1 --- /dev/null +++ b/src/rules/flake8_use_pathlib/snapshots/ruff__rules__flake8_use_pathlib__tests__PTH124_py_path_1.py.snap @@ -0,0 +1,15 @@ +--- +source: src/rules/flake8_use_pathlib/mod.rs +expression: diagnostics +--- +- kind: + PathlibPyPath: ~ + location: + row: 3 + column: 4 + end_location: + row: 3 + column: 17 + fix: ~ + parent: ~ + diff --git a/src/rules/flake8_use_pathlib/snapshots/ruff__rules__flake8_use_pathlib__tests__PTH124_py_path_2.py.snap b/src/rules/flake8_use_pathlib/snapshots/ruff__rules__flake8_use_pathlib__tests__PTH124_py_path_2.py.snap new file mode 100644 index 00000000000000..c5f6ad5501c67c --- /dev/null +++ b/src/rules/flake8_use_pathlib/snapshots/ruff__rules__flake8_use_pathlib__tests__PTH124_py_path_2.py.snap @@ -0,0 +1,15 @@ +--- +source: src/rules/flake8_use_pathlib/mod.rs +expression: diagnostics +--- +- kind: + PathlibPyPath: ~ + location: + row: 3 + column: 4 + end_location: + row: 3 + column: 8 + fix: ~ + parent: ~ + diff --git a/src/rules/flake8_use_pathlib/violations.rs b/src/rules/flake8_use_pathlib/violations.rs index fa59d098fc4460..c1d6f38a570fd3 100644 --- a/src/rules/flake8_use_pathlib/violations.rs +++ b/src/rules/flake8_use_pathlib/violations.rs @@ -266,3 +266,14 @@ impl Violation for PathlibOpen { format!("`open(\"foo\")` should be replaced by`Path(\"foo\").open()`") } } + +// PTH124 +define_violation!( + pub struct PathlibPyPath; +); +impl Violation for PathlibPyPath { + #[derive_message_formats] + fn message(&self) -> String { + format!("`py.path` is in maintenance mode, use pathlib instead") + } +}