feat: flake8-use-pathlib PTH100-124 (#2090)

This commit is contained in:
Simon Brugman
2023-01-22 21:17:25 +01:00
committed by GitHub
parent 6a6a792562
commit 11f06055a0
22 changed files with 1748 additions and 3 deletions

View File

@@ -36,8 +36,8 @@ use crate::rules::{
flake8_bugbear, flake8_builtins, flake8_comprehensions, flake8_datetimez, flake8_debugger,
flake8_errmsg, flake8_implicit_str_concat, flake8_import_conventions, flake8_pie, flake8_print,
flake8_pytest_style, flake8_return, flake8_simplify, flake8_tidy_imports, flake8_type_checking,
flake8_unused_arguments, mccabe, pandas_vet, pep8_naming, pycodestyle, pydocstyle, pyflakes,
pygrep_hooks, pylint, pyupgrade, ruff, tryceratops,
flake8_unused_arguments, flake8_use_pathlib, mccabe, pandas_vet, pep8_naming, pycodestyle,
pydocstyle, pyflakes, pygrep_hooks, pylint, pyupgrade, ruff, tryceratops,
};
use crate::settings::types::PythonVersion;
use crate::settings::{flags, Settings};
@@ -2545,6 +2545,35 @@ where
{
flake8_simplify::rules::open_file_with_context_handler(self, func);
}
// flake8-use-pathlib
if self.settings.rules.enabled(&Rule::PathlibAbspath)
|| self.settings.rules.enabled(&Rule::PathlibChmod)
|| self.settings.rules.enabled(&Rule::PathlibMkdir)
|| self.settings.rules.enabled(&Rule::PathlibMakedirs)
|| self.settings.rules.enabled(&Rule::PathlibRename)
|| self.settings.rules.enabled(&Rule::PathlibReplace)
|| self.settings.rules.enabled(&Rule::PathlibRmdir)
|| self.settings.rules.enabled(&Rule::PathlibRemove)
|| self.settings.rules.enabled(&Rule::PathlibUnlink)
|| self.settings.rules.enabled(&Rule::PathlibGetcwd)
|| self.settings.rules.enabled(&Rule::PathlibExists)
|| self.settings.rules.enabled(&Rule::PathlibExpanduser)
|| self.settings.rules.enabled(&Rule::PathlibIsDir)
|| self.settings.rules.enabled(&Rule::PathlibIsFile)
|| self.settings.rules.enabled(&Rule::PathlibIsLink)
|| self.settings.rules.enabled(&Rule::PathlibReadlink)
|| self.settings.rules.enabled(&Rule::PathlibStat)
|| self.settings.rules.enabled(&Rule::PathlibIsAbs)
|| self.settings.rules.enabled(&Rule::PathlibJoin)
|| self.settings.rules.enabled(&Rule::PathlibBasename)
|| 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);
}
}
ExprKind::Dict { keys, values } => {
if self

View File

@@ -429,6 +429,32 @@ ruff_macros::define_rule_mapping!(
// tryceratops
TRY004 => rules::tryceratops::rules::PreferTypeError,
TRY300 => rules::tryceratops::rules::TryConsiderElse,
// flake8-use-pathlib
PTH100 => rules::flake8_use_pathlib::violations::PathlibAbspath,
PTH101 => rules::flake8_use_pathlib::violations::PathlibChmod,
PTH102 => rules::flake8_use_pathlib::violations::PathlibMkdir,
PTH103 => rules::flake8_use_pathlib::violations::PathlibMakedirs,
PTH104 => rules::flake8_use_pathlib::violations::PathlibRename,
PTH105 => rules::flake8_use_pathlib::violations::PathlibReplace,
PTH106 => rules::flake8_use_pathlib::violations::PathlibRmdir,
PTH107 => rules::flake8_use_pathlib::violations::PathlibRemove,
PTH108 => rules::flake8_use_pathlib::violations::PathlibUnlink,
PTH109 => rules::flake8_use_pathlib::violations::PathlibGetcwd,
PTH110 => rules::flake8_use_pathlib::violations::PathlibExists,
PTH111 => rules::flake8_use_pathlib::violations::PathlibExpanduser,
PTH112 => rules::flake8_use_pathlib::violations::PathlibIsDir,
PTH113 => rules::flake8_use_pathlib::violations::PathlibIsFile,
PTH114 => rules::flake8_use_pathlib::violations::PathlibIsLink,
PTH115 => rules::flake8_use_pathlib::violations::PathlibReadlink,
PTH116 => rules::flake8_use_pathlib::violations::PathlibStat,
PTH117 => rules::flake8_use_pathlib::violations::PathlibIsAbs,
PTH118 => rules::flake8_use_pathlib::violations::PathlibJoin,
PTH119 => rules::flake8_use_pathlib::violations::PathlibBasename,
PTH120 => rules::flake8_use_pathlib::violations::PathlibDirname,
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,
@@ -515,6 +541,8 @@ pub enum Linter {
Flake8TypeChecking,
#[prefix = "TRY"]
Tryceratops,
#[prefix = "PTH"]
Flake8UsePathlib,
#[prefix = "RUF"]
Ruff,
}

View File

@@ -0,0 +1,110 @@
use rustpython_ast::Expr;
use crate::ast::types::Range;
use crate::checkers::ast::Checker;
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, PathlibPyPath, PathlibReadlink,
PathlibRemove, PathlibRename, PathlibReplace, PathlibRmdir, PathlibSamefile, PathlibSplitext,
PathlibStat, PathlibUnlink,
};
enum OsCall {
Abspath,
Chmod,
Mkdir,
Makedirs,
Rename,
Replace,
Rmdir,
Remove,
Unlink,
Getcwd,
Exists,
Expanduser,
IsDir,
IsFile,
IsLink,
Readlink,
Stat,
IsAbs,
Join,
Basename,
Dirname,
Samefile,
Splitext,
Open,
PyPath,
}
pub fn replaceable_by_pathlib(checker: &mut Checker, expr: &Expr) {
if let Some(os_call) =
checker
.resolve_call_path(expr)
.and_then(|call_path| match call_path.as_slice() {
["os", "path", "abspath"] => Some(OsCall::Abspath),
["os", "chmod"] => Some(OsCall::Chmod),
["os", "mkdir"] => Some(OsCall::Mkdir),
["os", "makedirs"] => Some(OsCall::Makedirs),
["os", "rename"] => Some(OsCall::Rename),
["os", "replace"] => Some(OsCall::Replace),
["os", "rmdir"] => Some(OsCall::Rmdir),
["os", "remove"] => Some(OsCall::Remove),
["os", "unlink"] => Some(OsCall::Unlink),
["os", "getcwd"] => Some(OsCall::Getcwd),
["os", "path", "exists"] => Some(OsCall::Exists),
["os", "path", "expanduser"] => Some(OsCall::Expanduser),
["os", "path", "isdir"] => Some(OsCall::IsDir),
["os", "path", "isfile"] => Some(OsCall::IsFile),
["os", "path", "islink"] => Some(OsCall::IsLink),
["os", "readlink"] => Some(OsCall::Readlink),
["os", "stat"] => Some(OsCall::Stat),
["os", "path", "isabs"] => Some(OsCall::IsAbs),
["os", "path", "join"] => Some(OsCall::Join),
["os", "path", "basename"] => Some(OsCall::Basename),
["os", "path", "dirname"] => Some(OsCall::Dirname),
["os", "path", "samefile"] => Some(OsCall::Samefile),
["os", "path", "splitext"] => Some(OsCall::Splitext),
["", "open"] => Some(OsCall::Open),
["py", "path", "local"] => Some(OsCall::PyPath),
_ => None,
})
{
let diagnostic = Diagnostic::new::<DiagnosticKind>(
match os_call {
OsCall::Abspath => PathlibAbspath.into(),
OsCall::Chmod => PathlibChmod.into(),
OsCall::Mkdir => PathlibMkdir.into(),
OsCall::Makedirs => PathlibMakedirs.into(),
OsCall::Rename => PathlibRename.into(),
OsCall::Replace => PathlibReplace.into(),
OsCall::Rmdir => PathlibRmdir.into(),
OsCall::Remove => PathlibRemove.into(),
OsCall::Unlink => PathlibUnlink.into(),
OsCall::Getcwd => PathlibGetcwd.into(),
OsCall::Exists => PathlibExists.into(),
OsCall::Expanduser => PathlibExpanduser.into(),
OsCall::IsDir => PathlibIsDir.into(),
OsCall::IsFile => PathlibIsFile.into(),
OsCall::IsLink => PathlibIsLink.into(),
OsCall::Readlink => PathlibReadlink.into(),
OsCall::Stat => PathlibStat.into(),
OsCall::IsAbs => PathlibIsAbs.into(),
OsCall::Join => PathlibJoin.into(),
OsCall::Basename => PathlibBasename.into(),
OsCall::Dirname => PathlibDirname.into(),
OsCall::Samefile => PathlibSamefile.into(),
OsCall::Splitext => PathlibSplitext.into(),
OsCall::Open => PathlibOpen.into(),
OsCall::PyPath => PathlibPyPath.into(),
},
Range::from_located(expr),
);
if checker.settings.rules.enabled(diagnostic.kind.rule()) {
checker.diagnostics.push(diagnostic);
}
}
}

View File

@@ -0,0 +1,70 @@
//! Rules from [flake8-use-pathlib](https://pypi.org/project/flake8-use-pathlib/).
pub(crate) mod helpers;
pub(crate) mod violations;
#[cfg(test)]
mod tests {
use std::path::Path;
use anyhow::Result;
use test_case::test_case;
use crate::linter::test_path;
use crate::registry::Rule;
use crate::settings;
#[test_case(Path::new("full_name.py"); "PTH1_1")]
#[test_case(Path::new("import_as.py"); "PTH1_2")]
#[test_case(Path::new("import_from_as.py"); "PTH1_3")]
#[test_case(Path::new("import_from.py"); "PTH1_4")]
fn rules(path: &Path) -> Result<()> {
let snapshot = format!("{}", path.to_string_lossy());
let diagnostics = test_path(
Path::new("./resources/test/fixtures/flake8_use_pathlib")
.join(path)
.as_path(),
&settings::Settings::for_rules(vec![
Rule::PathlibAbspath,
Rule::PathlibChmod,
Rule::PathlibMkdir,
Rule::PathlibMakedirs,
Rule::PathlibRename,
Rule::PathlibReplace,
Rule::PathlibRmdir,
Rule::PathlibRemove,
Rule::PathlibUnlink,
Rule::PathlibGetcwd,
Rule::PathlibExists,
Rule::PathlibExpanduser,
Rule::PathlibIsDir,
Rule::PathlibIsFile,
Rule::PathlibIsLink,
Rule::PathlibReadlink,
Rule::PathlibStat,
Rule::PathlibIsAbs,
Rule::PathlibJoin,
Rule::PathlibBasename,
Rule::PathlibDirname,
Rule::PathlibSamefile,
Rule::PathlibSplitext,
Rule::PathlibOpen,
]),
)?;
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(())
}
}

View File

@@ -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: ~

View File

@@ -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: ~

View File

@@ -0,0 +1,255 @@
---
source: src/rules/flake8_use_pathlib/mod.rs
expression: diagnostics
---
- kind:
PathlibAbspath: ~
location:
row: 6
column: 4
end_location:
row: 6
column: 19
fix: ~
parent: ~
- kind:
PathlibChmod: ~
location:
row: 7
column: 5
end_location:
row: 7
column: 13
fix: ~
parent: ~
- kind:
PathlibMkdir: ~
location:
row: 8
column: 6
end_location:
row: 8
column: 14
fix: ~
parent: ~
- kind:
PathlibMakedirs: ~
location:
row: 9
column: 0
end_location:
row: 9
column: 11
fix: ~
parent: ~
- kind:
PathlibRename: ~
location:
row: 10
column: 0
end_location:
row: 10
column: 9
fix: ~
parent: ~
- kind:
PathlibReplace: ~
location:
row: 11
column: 0
end_location:
row: 11
column: 10
fix: ~
parent: ~
- kind:
PathlibRmdir: ~
location:
row: 12
column: 0
end_location:
row: 12
column: 8
fix: ~
parent: ~
- kind:
PathlibRemove: ~
location:
row: 13
column: 0
end_location:
row: 13
column: 9
fix: ~
parent: ~
- kind:
PathlibUnlink: ~
location:
row: 14
column: 0
end_location:
row: 14
column: 9
fix: ~
parent: ~
- kind:
PathlibGetcwd: ~
location:
row: 15
column: 0
end_location:
row: 15
column: 9
fix: ~
parent: ~
- kind:
PathlibExists: ~
location:
row: 16
column: 4
end_location:
row: 16
column: 18
fix: ~
parent: ~
- kind:
PathlibExpanduser: ~
location:
row: 17
column: 5
end_location:
row: 17
column: 23
fix: ~
parent: ~
- kind:
PathlibIsDir: ~
location:
row: 18
column: 6
end_location:
row: 18
column: 19
fix: ~
parent: ~
- kind:
PathlibIsFile: ~
location:
row: 19
column: 7
end_location:
row: 19
column: 21
fix: ~
parent: ~
- kind:
PathlibIsLink: ~
location:
row: 20
column: 8
end_location:
row: 20
column: 22
fix: ~
parent: ~
- kind:
PathlibReadlink: ~
location:
row: 21
column: 0
end_location:
row: 21
column: 11
fix: ~
parent: ~
- kind:
PathlibStat: ~
location:
row: 22
column: 0
end_location:
row: 22
column: 7
fix: ~
parent: ~
- kind:
PathlibIsAbs: ~
location:
row: 23
column: 0
end_location:
row: 23
column: 13
fix: ~
parent: ~
- kind:
PathlibJoin: ~
location:
row: 24
column: 0
end_location:
row: 24
column: 12
fix: ~
parent: ~
- kind:
PathlibBasename: ~
location:
row: 25
column: 0
end_location:
row: 25
column: 16
fix: ~
parent: ~
- kind:
PathlibDirname: ~
location:
row: 26
column: 0
end_location:
row: 26
column: 15
fix: ~
parent: ~
- kind:
PathlibSamefile: ~
location:
row: 27
column: 0
end_location:
row: 27
column: 16
fix: ~
parent: ~
- kind:
PathlibSplitext: ~
location:
row: 28
column: 0
end_location:
row: 28
column: 16
fix: ~
parent: ~
- kind:
PathlibOpen: ~
location:
row: 29
column: 5
end_location:
row: 29
column: 9
fix: ~
parent: ~
- kind:
PathlibOpen: ~
location:
row: 31
column: 0
end_location:
row: 31
column: 4
fix: ~
parent: ~

View File

@@ -0,0 +1,235 @@
---
source: src/rules/flake8_use_pathlib/mod.rs
expression: diagnostics
---
- kind:
PathlibAbspath: ~
location:
row: 6
column: 4
end_location:
row: 6
column: 17
fix: ~
parent: ~
- kind:
PathlibChmod: ~
location:
row: 7
column: 5
end_location:
row: 7
column: 14
fix: ~
parent: ~
- kind:
PathlibMkdir: ~
location:
row: 8
column: 6
end_location:
row: 8
column: 15
fix: ~
parent: ~
- kind:
PathlibMakedirs: ~
location:
row: 9
column: 0
end_location:
row: 9
column: 12
fix: ~
parent: ~
- kind:
PathlibRename: ~
location:
row: 10
column: 0
end_location:
row: 10
column: 10
fix: ~
parent: ~
- kind:
PathlibReplace: ~
location:
row: 11
column: 0
end_location:
row: 11
column: 11
fix: ~
parent: ~
- kind:
PathlibRmdir: ~
location:
row: 12
column: 0
end_location:
row: 12
column: 9
fix: ~
parent: ~
- kind:
PathlibRemove: ~
location:
row: 13
column: 0
end_location:
row: 13
column: 10
fix: ~
parent: ~
- kind:
PathlibUnlink: ~
location:
row: 14
column: 0
end_location:
row: 14
column: 10
fix: ~
parent: ~
- kind:
PathlibGetcwd: ~
location:
row: 15
column: 0
end_location:
row: 15
column: 10
fix: ~
parent: ~
- kind:
PathlibExists: ~
location:
row: 16
column: 4
end_location:
row: 16
column: 16
fix: ~
parent: ~
- kind:
PathlibExpanduser: ~
location:
row: 17
column: 5
end_location:
row: 17
column: 21
fix: ~
parent: ~
- kind:
PathlibIsDir: ~
location:
row: 18
column: 6
end_location:
row: 18
column: 17
fix: ~
parent: ~
- kind:
PathlibIsFile: ~
location:
row: 19
column: 7
end_location:
row: 19
column: 19
fix: ~
parent: ~
- kind:
PathlibIsLink: ~
location:
row: 20
column: 8
end_location:
row: 20
column: 20
fix: ~
parent: ~
- kind:
PathlibReadlink: ~
location:
row: 21
column: 0
end_location:
row: 21
column: 12
fix: ~
parent: ~
- kind:
PathlibStat: ~
location:
row: 22
column: 0
end_location:
row: 22
column: 8
fix: ~
parent: ~
- kind:
PathlibIsAbs: ~
location:
row: 23
column: 0
end_location:
row: 23
column: 11
fix: ~
parent: ~
- kind:
PathlibJoin: ~
location:
row: 24
column: 0
end_location:
row: 24
column: 10
fix: ~
parent: ~
- kind:
PathlibBasename: ~
location:
row: 25
column: 0
end_location:
row: 25
column: 14
fix: ~
parent: ~
- kind:
PathlibDirname: ~
location:
row: 26
column: 0
end_location:
row: 26
column: 13
fix: ~
parent: ~
- kind:
PathlibSamefile: ~
location:
row: 27
column: 0
end_location:
row: 27
column: 14
fix: ~
parent: ~
- kind:
PathlibSplitext: ~
location:
row: 28
column: 0
end_location:
row: 28
column: 14
fix: ~
parent: ~

View File

@@ -0,0 +1,255 @@
---
source: src/rules/flake8_use_pathlib/mod.rs
expression: diagnostics
---
- kind:
PathlibAbspath: ~
location:
row: 8
column: 4
end_location:
row: 8
column: 11
fix: ~
parent: ~
- kind:
PathlibChmod: ~
location:
row: 9
column: 5
end_location:
row: 9
column: 10
fix: ~
parent: ~
- kind:
PathlibMkdir: ~
location:
row: 10
column: 6
end_location:
row: 10
column: 11
fix: ~
parent: ~
- kind:
PathlibMakedirs: ~
location:
row: 11
column: 0
end_location:
row: 11
column: 8
fix: ~
parent: ~
- kind:
PathlibRename: ~
location:
row: 12
column: 0
end_location:
row: 12
column: 6
fix: ~
parent: ~
- kind:
PathlibReplace: ~
location:
row: 13
column: 0
end_location:
row: 13
column: 7
fix: ~
parent: ~
- kind:
PathlibRmdir: ~
location:
row: 14
column: 0
end_location:
row: 14
column: 5
fix: ~
parent: ~
- kind:
PathlibRemove: ~
location:
row: 15
column: 0
end_location:
row: 15
column: 6
fix: ~
parent: ~
- kind:
PathlibUnlink: ~
location:
row: 16
column: 0
end_location:
row: 16
column: 6
fix: ~
parent: ~
- kind:
PathlibGetcwd: ~
location:
row: 17
column: 0
end_location:
row: 17
column: 6
fix: ~
parent: ~
- kind:
PathlibExists: ~
location:
row: 18
column: 4
end_location:
row: 18
column: 10
fix: ~
parent: ~
- kind:
PathlibExpanduser: ~
location:
row: 19
column: 5
end_location:
row: 19
column: 15
fix: ~
parent: ~
- kind:
PathlibIsDir: ~
location:
row: 20
column: 6
end_location:
row: 20
column: 11
fix: ~
parent: ~
- kind:
PathlibIsFile: ~
location:
row: 21
column: 7
end_location:
row: 21
column: 13
fix: ~
parent: ~
- kind:
PathlibIsLink: ~
location:
row: 22
column: 8
end_location:
row: 22
column: 14
fix: ~
parent: ~
- kind:
PathlibReadlink: ~
location:
row: 23
column: 0
end_location:
row: 23
column: 8
fix: ~
parent: ~
- kind:
PathlibStat: ~
location:
row: 24
column: 0
end_location:
row: 24
column: 4
fix: ~
parent: ~
- kind:
PathlibIsAbs: ~
location:
row: 25
column: 0
end_location:
row: 25
column: 5
fix: ~
parent: ~
- kind:
PathlibJoin: ~
location:
row: 26
column: 0
end_location:
row: 26
column: 4
fix: ~
parent: ~
- kind:
PathlibBasename: ~
location:
row: 27
column: 0
end_location:
row: 27
column: 8
fix: ~
parent: ~
- kind:
PathlibDirname: ~
location:
row: 28
column: 0
end_location:
row: 28
column: 7
fix: ~
parent: ~
- kind:
PathlibSamefile: ~
location:
row: 29
column: 0
end_location:
row: 29
column: 8
fix: ~
parent: ~
- kind:
PathlibSplitext: ~
location:
row: 30
column: 0
end_location:
row: 30
column: 8
fix: ~
parent: ~
- kind:
PathlibOpen: ~
location:
row: 31
column: 5
end_location:
row: 31
column: 9
fix: ~
parent: ~
- kind:
PathlibOpen: ~
location:
row: 33
column: 0
end_location:
row: 33
column: 4
fix: ~
parent: ~

View File

@@ -0,0 +1,235 @@
---
source: src/rules/flake8_use_pathlib/mod.rs
expression: diagnostics
---
- kind:
PathlibAbspath: ~
location:
row: 13
column: 4
end_location:
row: 13
column: 12
fix: ~
parent: ~
- kind:
PathlibChmod: ~
location:
row: 14
column: 5
end_location:
row: 14
column: 11
fix: ~
parent: ~
- kind:
PathlibMkdir: ~
location:
row: 15
column: 6
end_location:
row: 15
column: 12
fix: ~
parent: ~
- kind:
PathlibMakedirs: ~
location:
row: 16
column: 0
end_location:
row: 16
column: 9
fix: ~
parent: ~
- kind:
PathlibRename: ~
location:
row: 17
column: 0
end_location:
row: 17
column: 7
fix: ~
parent: ~
- kind:
PathlibReplace: ~
location:
row: 18
column: 0
end_location:
row: 18
column: 8
fix: ~
parent: ~
- kind:
PathlibRmdir: ~
location:
row: 19
column: 0
end_location:
row: 19
column: 6
fix: ~
parent: ~
- kind:
PathlibRemove: ~
location:
row: 20
column: 0
end_location:
row: 20
column: 7
fix: ~
parent: ~
- kind:
PathlibUnlink: ~
location:
row: 21
column: 0
end_location:
row: 21
column: 7
fix: ~
parent: ~
- kind:
PathlibGetcwd: ~
location:
row: 22
column: 0
end_location:
row: 22
column: 7
fix: ~
parent: ~
- kind:
PathlibExists: ~
location:
row: 23
column: 4
end_location:
row: 23
column: 11
fix: ~
parent: ~
- kind:
PathlibExpanduser: ~
location:
row: 24
column: 5
end_location:
row: 24
column: 16
fix: ~
parent: ~
- kind:
PathlibIsDir: ~
location:
row: 25
column: 6
end_location:
row: 25
column: 12
fix: ~
parent: ~
- kind:
PathlibIsFile: ~
location:
row: 26
column: 7
end_location:
row: 26
column: 14
fix: ~
parent: ~
- kind:
PathlibIsLink: ~
location:
row: 27
column: 8
end_location:
row: 27
column: 15
fix: ~
parent: ~
- kind:
PathlibReadlink: ~
location:
row: 28
column: 0
end_location:
row: 28
column: 9
fix: ~
parent: ~
- kind:
PathlibStat: ~
location:
row: 29
column: 0
end_location:
row: 29
column: 5
fix: ~
parent: ~
- kind:
PathlibIsAbs: ~
location:
row: 30
column: 0
end_location:
row: 30
column: 6
fix: ~
parent: ~
- kind:
PathlibJoin: ~
location:
row: 31
column: 0
end_location:
row: 31
column: 5
fix: ~
parent: ~
- kind:
PathlibBasename: ~
location:
row: 32
column: 0
end_location:
row: 32
column: 9
fix: ~
parent: ~
- kind:
PathlibDirname: ~
location:
row: 33
column: 0
end_location:
row: 33
column: 8
fix: ~
parent: ~
- kind:
PathlibSamefile: ~
location:
row: 34
column: 0
end_location:
row: 34
column: 9
fix: ~
parent: ~
- kind:
PathlibSplitext: ~
location:
row: 35
column: 0
end_location:
row: 35
column: 9
fix: ~
parent: ~

View File

@@ -0,0 +1,279 @@
use ruff_macros::derive_message_formats;
use crate::define_violation;
use crate::violation::Violation;
// PTH100
define_violation!(
pub struct PathlibAbspath;
);
impl Violation for PathlibAbspath {
#[derive_message_formats]
fn message(&self) -> String {
format!("`os.path.abspath` should be replaced by `.resolve()`")
}
}
// PTH101
define_violation!(
pub struct PathlibChmod;
);
impl Violation for PathlibChmod {
#[derive_message_formats]
fn message(&self) -> String {
format!("`os.chmod` should be replaced by `.chmod()`")
}
}
// PTH102
define_violation!(
pub struct PathlibMakedirs;
);
impl Violation for PathlibMakedirs {
#[derive_message_formats]
fn message(&self) -> String {
format!("`os.makedirs` should be replaced by `.mkdir(parents=True)`")
}
}
// PTH103
define_violation!(
pub struct PathlibMkdir;
);
impl Violation for PathlibMkdir {
#[derive_message_formats]
fn message(&self) -> String {
format!("`os.mkdir` should be replaced by `.mkdir()`")
}
}
// PTH104
define_violation!(
pub struct PathlibRename;
);
impl Violation for PathlibRename {
#[derive_message_formats]
fn message(&self) -> String {
format!("`os.rename` should be replaced by `.rename()`")
}
}
// PTH105
define_violation!(
pub struct PathlibReplace;
);
impl Violation for PathlibReplace {
#[derive_message_formats]
fn message(&self) -> String {
format!("`os.replace`should be replaced by `.replace()`")
}
}
// PTH106
define_violation!(
pub struct PathlibRmdir;
);
impl Violation for PathlibRmdir {
#[derive_message_formats]
fn message(&self) -> String {
format!("`os.rmdir` should be replaced by `.rmdir()`")
}
}
// PTH107
define_violation!(
pub struct PathlibRemove;
);
impl Violation for PathlibRemove {
#[derive_message_formats]
fn message(&self) -> String {
format!("`os.remove` should be replaced by `.unlink()`")
}
}
// PTH108
define_violation!(
pub struct PathlibUnlink;
);
impl Violation for PathlibUnlink {
#[derive_message_formats]
fn message(&self) -> String {
format!("`os.unlink` should be replaced by `.unlink()`")
}
}
// PTH109
define_violation!(
pub struct PathlibGetcwd;
);
impl Violation for PathlibGetcwd {
#[derive_message_formats]
fn message(&self) -> String {
format!("`os.getcwd()` should be replaced by `Path.cwd()`")
}
}
// PTH110
define_violation!(
pub struct PathlibExists;
);
impl Violation for PathlibExists {
#[derive_message_formats]
fn message(&self) -> String {
format!("`os.path.exists` should be replaced by `.exists()`")
}
}
// PTH111
define_violation!(
pub struct PathlibExpanduser;
);
impl Violation for PathlibExpanduser {
#[derive_message_formats]
fn message(&self) -> String {
format!("`os.path.expanduser` should be replaced by `.expanduser()`")
}
}
// PTH112
define_violation!(
pub struct PathlibIsDir;
);
impl Violation for PathlibIsDir {
#[derive_message_formats]
fn message(&self) -> String {
format!("`os.path.isdir` should be replaced by `.is_dir()`")
}
}
// PTH113
define_violation!(
pub struct PathlibIsFile;
);
impl Violation for PathlibIsFile {
#[derive_message_formats]
fn message(&self) -> String {
format!("`os.path.isfile` should be replaced by `.is_file()`")
}
}
// PTH114
define_violation!(
pub struct PathlibIsLink;
);
impl Violation for PathlibIsLink {
#[derive_message_formats]
fn message(&self) -> String {
format!("`os.path.islink` should be replaced by `.is_symlink()`")
}
}
// PTH115
define_violation!(
pub struct PathlibReadlink;
);
impl Violation for PathlibReadlink {
#[derive_message_formats]
fn message(&self) -> String {
format!("`os.readlink(` should be replaced by `.readlink()`")
}
}
// PTH116
define_violation!(
pub struct PathlibStat;
);
impl Violation for PathlibStat {
#[derive_message_formats]
fn message(&self) -> String {
format!("`os.stat` should be replaced by `.stat()` or `.owner()` or `.group()`")
}
}
// PTH117
define_violation!(
pub struct PathlibIsAbs;
);
impl Violation for PathlibIsAbs {
#[derive_message_formats]
fn message(&self) -> String {
format!("`os.path.isabs` should be replaced by `.is_absolute()`")
}
}
// PTH118
define_violation!(
pub struct PathlibJoin;
);
impl Violation for PathlibJoin {
#[derive_message_formats]
fn message(&self) -> String {
format!("`os.path.join` should be replaced by foo_path / \"bar\"")
}
}
// PTH119
define_violation!(
pub struct PathlibBasename;
);
impl Violation for PathlibBasename {
#[derive_message_formats]
fn message(&self) -> String {
format!("`os.path.basename` should be replaced by `.name`")
}
}
// PTH120
define_violation!(
pub struct PathlibDirname;
);
impl Violation for PathlibDirname {
#[derive_message_formats]
fn message(&self) -> String {
format!("`os.path.dirname` should be replaced by `.parent`")
}
}
// PTH121
define_violation!(
pub struct PathlibSamefile;
);
impl Violation for PathlibSamefile {
#[derive_message_formats]
fn message(&self) -> String {
format!("`os.path.samefile` should be replaced by `.samefile()`")
}
}
// PTH122
define_violation!(
pub struct PathlibSplitext;
);
impl Violation for PathlibSplitext {
#[derive_message_formats]
fn message(&self) -> String {
format!("`os.path.splitext` should be replaced by `.suffix`")
}
}
// PTH123
define_violation!(
pub struct PathlibOpen;
);
impl Violation for PathlibOpen {
#[derive_message_formats]
fn message(&self) -> String {
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")
}
}

View File

@@ -25,6 +25,7 @@ pub mod flake8_simplify;
pub mod flake8_tidy_imports;
pub mod flake8_type_checking;
pub mod flake8_unused_arguments;
pub mod flake8_use_pathlib;
pub mod isort;
pub mod mccabe;
pub mod pandas_vet;