Skip to content

Commit

Permalink
feat: final rule for this plugin (PTH024)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrugman committed Jan 22, 2023
1 parent 6668929 commit be451f5
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
3 changes: 3 additions & 0 deletions resources/test/fixtures/flake8_use_pathlib/py_path_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import py

p = py.path.local("../foo")
3 changes: 3 additions & 0 deletions resources/test/fixtures/flake8_use_pathlib/py_path_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from py.path import local as path

p = path("/foo")
1 change: 1 addition & 0 deletions ruff.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1662,6 +1662,7 @@
"PTH121",
"PTH122",
"PTH123",
"PTH124",
"Q",
"Q0",
"Q00",
Expand Down
1 change: 1 addition & 0 deletions src/checkers/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 6 additions & 3 deletions src/rules/flake8_use_pathlib/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -36,6 +36,7 @@ enum OsCall {
Samefile,
Splitext,
Open,
PyPath,
}

pub fn replaceable_by_pathlib(checker: &mut Checker, expr: &Expr) {
Expand Down Expand Up @@ -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,
})
{
Expand Down Expand Up @@ -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),
);
Expand Down
14 changes: 14 additions & 0 deletions src/rules/flake8_use_pathlib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
}
Original file line number Diff line number Diff line change
@@ -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: ~

Original file line number Diff line number Diff line change
@@ -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: ~

11 changes: 11 additions & 0 deletions src/rules/flake8_use_pathlib/violations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

0 comments on commit be451f5

Please sign in to comment.