Skip to content

Commit

Permalink
feat: open (PTH023) + parentheses
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrugman committed Jan 22, 2023
1 parent 497d734 commit 6668929
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 6 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1205,7 +1205,7 @@ For more, see [flake8-use-pathlib](https://pypi.org/project/flake8-use-pathlib/)
| Code | Name | Message | Fix |
| ---- | ---- | ------- | --- |
| PTH100 | pathlib-abspath | `os.path.abspath` should be replaced by `.resolve()` | |
| PTH101 | pathlib-chmod | `os.chmod` should be replaced by `.chmod` | |
| PTH101 | pathlib-chmod | `os.chmod` should be replaced by `.chmod()` | |
| PTH102 | pathlib-mkdir | `os.mkdir` should be replaced by `.mkdir()` | |
| PTH103 | pathlib-makedirs | `os.makedirs` should be replaced by `.mkdir(parents=True)` | |
| PTH104 | pathlib-rename | `os.rename` should be replaced by `.rename()` | |
Expand All @@ -1220,13 +1220,14 @@ For more, see [flake8-use-pathlib](https://pypi.org/project/flake8-use-pathlib/)
| PTH113 | pathlib-is-file | `os.path.isfile` should be replaced by `.is_file()` | |
| PTH114 | pathlib-is-link | `os.path.islink` should be replaced by `.is_symlink()` | |
| PTH115 | pathlib-readlink | `os.readlink(` should be replaced by `.readlink()` | |
| PTH116 | pathlib-stat | `os.stat` should be replaced by `.stat()` or `.owner()` or `.group` | |
| PTH116 | pathlib-stat | `os.stat` should be replaced by `.stat()` or `.owner()` or `.group()` | |
| PTH117 | pathlib-is-abs | `os.path.isabs` should be replaced by `.is_absolute()` | |
| PTH118 | pathlib-join | `os.path.join` should be replaced by foo_path / "bar" | |
| PTH119 | pathlib-basename | `os.path.basename` should be replaced by `.name` | |
| PTH120 | pathlib-dirname | `os.path.dirname` should be replaced by `.parent` | |
| 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()` | |

### Ruff-specific rules (RUF)

Expand Down
3 changes: 3 additions & 0 deletions resources/test/fixtures/flake8_use_pathlib/full_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@
os.path.dirname(p)
os.path.samefile(p)
os.path.splitext(p)
with open(p) as fp:
fp.read()
open(p).close()
3 changes: 3 additions & 0 deletions resources/test/fixtures/flake8_use_pathlib/import_from.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@
dirname(p)
samefile(p)
splitext(p)
with open(p) as fp:
fp.read()
open(p).close()
1 change: 1 addition & 0 deletions ruff.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1661,6 +1661,7 @@
"PTH120",
"PTH121",
"PTH122",
"PTH123",
"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 @@ -2569,6 +2569,7 @@ where
|| self.settings.rules.enabled(&Rule::PathlibBasename)
|| self.settings.rules.enabled(&Rule::PathlibSamefile)
|| self.settings.rules.enabled(&Rule::PathlibSplitext)
|| self.settings.rules.enabled(&Rule::PathlibOpen)
{
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 @@ -453,6 +453,7 @@ ruff_macros::define_rule_mapping!(
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,
// ruff
RUF001 => violations::AmbiguousUnicodeCharacterString,
RUF002 => violations::AmbiguousUnicodeCharacterDocstring,
Expand Down
8 changes: 6 additions & 2 deletions src/rules/flake8_use_pathlib/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +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, PathlibReadlink, PathlibRemove, PathlibRename,
PathlibReplace, PathlibRmdir, PathlibSamefile, PathlibSplitext, PathlibStat, PathlibUnlink,
PathlibJoin, PathlibMakedirs, PathlibMkdir, PathlibOpen, PathlibReadlink, PathlibRemove,
PathlibRename, PathlibReplace, PathlibRmdir, PathlibSamefile, PathlibSplitext, PathlibStat,
PathlibUnlink,
};

enum OsCall {
Expand All @@ -34,6 +35,7 @@ enum OsCall {
Dirname,
Samefile,
Splitext,
Open,
}

pub fn replaceable_by_pathlib(checker: &mut Checker, expr: &Expr) {
Expand Down Expand Up @@ -64,6 +66,7 @@ pub fn replaceable_by_pathlib(checker: &mut Checker, expr: &Expr) {
["os", "path", "dirname"] => Some(OsCall::Dirname),
["os", "path", "samefile"] => Some(OsCall::Samefile),
["os", "path", "splitext"] => Some(OsCall::Splitext),
["", "open"] => Some(OsCall::Open),
_ => None,
})
{
Expand Down Expand Up @@ -92,6 +95,7 @@ pub fn replaceable_by_pathlib(checker: &mut Checker, expr: &Expr) {
OsCall::Dirname => PathlibDirname.into(),
OsCall::Samefile => PathlibSamefile.into(),
OsCall::Splitext => PathlibSplitext.into(),
OsCall::Open => PathlibOpen.into(),
},
Range::from_located(expr),
);
Expand Down
1 change: 1 addition & 0 deletions src/rules/flake8_use_pathlib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ mod tests {
Rule::PathlibDirname,
Rule::PathlibSamefile,
Rule::PathlibSplitext,
Rule::PathlibOpen,
]),
)?;
insta::assert_yaml_snapshot!(snapshot, diagnostics);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,24 @@ expression: diagnostics
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: ~

Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,24 @@ expression: diagnostics
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: ~

15 changes: 13 additions & 2 deletions src/rules/flake8_use_pathlib/violations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ define_violation!(
impl Violation for PathlibChmod {
#[derive_message_formats]
fn message(&self) -> String {
format!("`os.chmod` should be replaced by `.chmod`")
format!("`os.chmod` should be replaced by `.chmod()`")
}
}

Expand Down Expand Up @@ -186,7 +186,7 @@ define_violation!(
impl Violation for PathlibStat {
#[derive_message_formats]
fn message(&self) -> String {
format!("`os.stat` should be replaced by `.stat()` or `.owner()` or `.group`")
format!("`os.stat` should be replaced by `.stat()` or `.owner()` or `.group()`")
}
}

Expand Down Expand Up @@ -255,3 +255,14 @@ impl Violation for PathlibSplitext {
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()`")
}
}

0 comments on commit 6668929

Please sign in to comment.