Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pathlib import Path, PurePath
from pathlib import Path, PurePath, PosixPath, PurePosixPath, WindowsPath, PureWindowsPath
from pathlib import Path as pth


Expand Down Expand Up @@ -68,3 +68,11 @@
PurePath(".", "folder")

Path()

from importlib.metadata import PackagePath

_ = PosixPath(".")
_ = PurePosixPath(".")
_ = WindowsPath(".")
_ = PureWindowsPath(".")
_ = PackagePath(".")
14 changes: 14 additions & 0 deletions crates/ruff_linter/src/rules/flake8_use_pathlib/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ pub(crate) fn is_pathlib_path_call(checker: &Checker, expr: &Expr) -> bool {
})
}

/// Check if the given segments represent a pathlib Path subclass or `PackagePath`.
pub(crate) fn is_pure_path_subclass(segments: &[&str]) -> bool {
let is_pathlib = matches!(
segments,
[
"pathlib",
"Path" | "PurePath" | "PosixPath" | "PurePosixPath" | "WindowsPath" | "PureWindowsPath"
]
);
let is_packagepath = matches!(segments, ["importlib", "metadata", "PackagePath"]);

is_pathlib || is_packagepath
}

/// We check functions that take only 1 argument, this does not apply to functions
/// with `dir_fd` argument, because `dir_fd` is not supported by pathlib,
/// so check if it's set to non-default values
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use ruff_text_size::{Ranged, TextRange};

use crate::checkers::ast::Checker;
use crate::fix::edits::{Parentheses, remove_argument};
use crate::rules::flake8_use_pathlib::helpers::is_pure_path_subclass;
use crate::{AlwaysFixableViolation, Applicability, Edit, Fix};

/// ## What it does
Expand Down Expand Up @@ -69,7 +70,7 @@ pub(crate) fn path_constructor_current_directory(

let arguments = &call.arguments;

if !matches!(segments, ["pathlib", "Path" | "PurePath"]) {
if !is_pure_path_subclass(segments) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,98 @@ PTH201.py:55:5: PTH201 [*] Do not pass the current directory explicitly to `Path
58 58 | "frag" # Comment
59 59 | 'ment'
60 60 | )

PTH201.py:74:15: PTH201 [*] Do not pass the current directory explicitly to `Path`
|
72 | from importlib.metadata import PackagePath
73 |
74 | _ = PosixPath(".")
| ^^^ PTH201
75 | _ = PurePosixPath(".")
76 | _ = WindowsPath(".")
|
= help: Remove the current directory argument

ℹ Safe fix
71 71 |
72 72 | from importlib.metadata import PackagePath
73 73 |
74 |-_ = PosixPath(".")
74 |+_ = PosixPath()
75 75 | _ = PurePosixPath(".")
76 76 | _ = WindowsPath(".")
77 77 | _ = PureWindowsPath(".")

PTH201.py:75:19: PTH201 [*] Do not pass the current directory explicitly to `Path`
|
74 | _ = PosixPath(".")
75 | _ = PurePosixPath(".")
| ^^^ PTH201
76 | _ = WindowsPath(".")
77 | _ = PureWindowsPath(".")
|
= help: Remove the current directory argument

ℹ Safe fix
72 72 | from importlib.metadata import PackagePath
73 73 |
74 74 | _ = PosixPath(".")
75 |-_ = PurePosixPath(".")
75 |+_ = PurePosixPath()
76 76 | _ = WindowsPath(".")
77 77 | _ = PureWindowsPath(".")
78 78 | _ = PackagePath(".")

PTH201.py:76:17: PTH201 [*] Do not pass the current directory explicitly to `Path`
|
74 | _ = PosixPath(".")
75 | _ = PurePosixPath(".")
76 | _ = WindowsPath(".")
| ^^^ PTH201
77 | _ = PureWindowsPath(".")
78 | _ = PackagePath(".")
|
= help: Remove the current directory argument

ℹ Safe fix
73 73 |
74 74 | _ = PosixPath(".")
75 75 | _ = PurePosixPath(".")
76 |-_ = WindowsPath(".")
76 |+_ = WindowsPath()
77 77 | _ = PureWindowsPath(".")
78 78 | _ = PackagePath(".")

PTH201.py:77:21: PTH201 [*] Do not pass the current directory explicitly to `Path`
|
75 | _ = PurePosixPath(".")
76 | _ = WindowsPath(".")
77 | _ = PureWindowsPath(".")
| ^^^ PTH201
78 | _ = PackagePath(".")
|
= help: Remove the current directory argument

ℹ Safe fix
74 74 | _ = PosixPath(".")
75 75 | _ = PurePosixPath(".")
76 76 | _ = WindowsPath(".")
77 |-_ = PureWindowsPath(".")
77 |+_ = PureWindowsPath()
78 78 | _ = PackagePath(".")

PTH201.py:78:17: PTH201 [*] Do not pass the current directory explicitly to `Path`
|
76 | _ = WindowsPath(".")
77 | _ = PureWindowsPath(".")
78 | _ = PackagePath(".")
| ^^^ PTH201
|
= help: Remove the current directory argument

ℹ Safe fix
75 75 | _ = PurePosixPath(".")
76 76 | _ = WindowsPath(".")
77 77 | _ = PureWindowsPath(".")
78 |-_ = PackagePath(".")
78 |+_ = PackagePath()
Loading