Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
@@ -0,0 +1,35 @@
import glob
import os
import os.path

bytes_path = b"file.py"
bytes_dir = b"directory"
bytes_src = b"src"
bytes_dst = b"dst"

open(bytes_path)
os.chmod(bytes_path, 0o444)
os.mkdir(bytes_dir)
os.makedirs(bytes_dir)
os.rename(bytes_src, bytes_dst)
os.replace(bytes_src, bytes_dst)
os.rmdir(bytes_dir)
os.remove(bytes_path)
os.unlink(bytes_path)
os.readlink(bytes_path)
os.stat(bytes_path)
os.path.exists(bytes_path)
os.path.expanduser(bytes_path)
os.path.isdir(bytes_dir)
os.path.isfile(bytes_path)
os.path.islink(bytes_path)
os.path.isabs(bytes_path)
os.path.join(bytes_dir, bytes_path)
os.path.basename(bytes_path)
os.path.dirname(bytes_path)
os.path.samefile(bytes_src, bytes_dst)
os.path.splitext(bytes_path)
os.listdir(bytes_dir)
os.symlink(bytes_src, bytes_dst)
glob.glob(bytes_path)
glob.iglob(bytes_path)
32 changes: 32 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 @@ -67,6 +67,10 @@ pub(crate) fn check_os_pathlib_single_arg_calls(
return;
};

if is_bytes_path(arg, checker.semantic()) {
return;
}

let arg_code = checker.locator().slice(arg.range());
let range = call.range();

Expand Down Expand Up @@ -134,6 +138,23 @@ pub(crate) fn is_file_descriptor(expr: &Expr, semantic: &SemanticModel) -> bool
typing::is_int(binding, semantic)
}

/// Returns `true` if the given expression looks like a bytes path.
pub(crate) fn is_bytes_path(expr: &Expr, semantic: &SemanticModel) -> bool {
if matches!(expr, Expr::BytesLiteral(_)) {
return true;
}

let Some(name) = get_name_expr(expr) else {
return false;
};

let Some(binding) = semantic.only_binding(name).map(|id| semantic.binding(id)) else {
return false;
};

typing::is_bytes(binding, semantic)
}

#[expect(clippy::too_many_arguments)]
pub(crate) fn check_os_pathlib_two_arg_calls(
checker: &Checker,
Expand All @@ -145,6 +166,17 @@ pub(crate) fn check_os_pathlib_two_arg_calls(
violation: impl Violation,
applicability: Applicability,
) {
if let (Some(path_expr), Some(second_expr)) = (
call.arguments.find_argument_value(path_arg, 0),
call.arguments.find_argument_value(second_arg, 1),
) {
if is_bytes_path(path_expr, checker.semantic())
|| is_bytes_path(second_expr, checker.semantic())
{
return;
}
}

let range = call.range();
let mut diagnostic = checker.report_diagnostic(violation, call.func.range());

Expand Down
2 changes: 2 additions & 0 deletions crates/ruff_linter/src/rules/flake8_use_pathlib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ mod tests {
use crate::{assert_diagnostics, assert_diagnostics_diff};

#[test_case(Path::new("full_name.py"))]
#[test_case(Path::new("bytes_paths.py"))]
#[test_case(Path::new("import_as.py"))]
#[test_case(Path::new("import_from_as.py"))]
#[test_case(Path::new("import_from.py"))]
Expand Down Expand Up @@ -106,6 +107,7 @@ mod tests {
}

#[test_case(Path::new("full_name.py"))]
#[test_case(Path::new("bytes_paths.py"))]
#[test_case(Path::new("import_as.py"))]
#[test_case(Path::new("import_from_as.py"))]
#[test_case(Path::new("import_from.py"))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use crate::checkers::ast::Checker;
use crate::importer::ImportRequest;
use crate::preview::is_fix_builtin_open_enabled;
use crate::rules::flake8_use_pathlib::helpers::{
has_unknown_keywords_or_starred_expr, is_argument_non_default, is_file_descriptor,
is_pathlib_path_call,
has_unknown_keywords_or_starred_expr, is_argument_non_default, is_bytes_path,
is_file_descriptor, is_pathlib_path_call,
};
use crate::{FixAvailability, Violation};

Expand Down Expand Up @@ -100,7 +100,9 @@ pub(crate) fn builtin_open(checker: &Checker, call: &ExprCall, segments: &[&str]
)
})
|| is_argument_non_default(&call.arguments, "opener", 7)
|| file_arg.is_some_and(|expr| is_file_descriptor(expr, checker.semantic()))
|| file_arg.is_some_and(|expr| {
is_file_descriptor(expr, checker.semantic()) || is_bytes_path(expr, checker.semantic())
})
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use crate::checkers::ast::Checker;
use crate::importer::ImportRequest;
use crate::preview::is_fix_os_chmod_enabled;
use crate::rules::flake8_use_pathlib::helpers::{
has_unknown_keywords_or_starred_expr, is_file_descriptor, is_keyword_only_argument_non_default,
is_pathlib_path_call,
has_unknown_keywords_or_starred_expr, is_bytes_path, is_file_descriptor,
is_keyword_only_argument_non_default, is_pathlib_path_call,
};
use crate::{FixAvailability, Violation};

Expand Down Expand Up @@ -81,8 +81,9 @@ pub(crate) fn os_chmod(checker: &Checker, call: &ExprCall, segments: &[&str]) {
// ```
let path_arg = call.arguments.find_argument_value("path", 0);

if path_arg.is_some_and(|expr| is_file_descriptor(expr, checker.semantic()))
|| is_keyword_only_argument_non_default(&call.arguments, "dir_fd")
if path_arg.is_some_and(|expr| {
is_file_descriptor(expr, checker.semantic()) || is_bytes_path(expr, checker.semantic())
}) || is_keyword_only_argument_non_default(&call.arguments, "dir_fd")
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::checkers::ast::Checker;
use crate::importer::ImportRequest;
use crate::preview::is_fix_os_makedirs_enabled;
use crate::rules::flake8_use_pathlib::helpers::{
has_unknown_keywords_or_starred_expr, is_pathlib_path_call,
has_unknown_keywords_or_starred_expr, is_bytes_path, is_pathlib_path_call,
};
use crate::{FixAvailability, Violation};

Expand Down Expand Up @@ -72,13 +72,17 @@ pub(crate) fn os_makedirs(checker: &Checker, call: &ExprCall, segments: &[&str])
return;
}

let range = call.range();
let mut diagnostic = checker.report_diagnostic(OsMakedirs, call.func.range());

let Some(name) = call.arguments.find_argument_value("name", 0) else {
return;
};

if is_bytes_path(name, checker.semantic()) {
return;
}

let range = call.range();
let mut diagnostic = checker.report_diagnostic(OsMakedirs, call.func.range());

if !is_fix_os_makedirs_enabled(checker.settings()) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::checkers::ast::Checker;
use crate::importer::ImportRequest;
use crate::preview::is_fix_os_mkdir_enabled;
use crate::rules::flake8_use_pathlib::helpers::{
has_unknown_keywords_or_starred_expr, is_keyword_only_argument_non_default,
has_unknown_keywords_or_starred_expr, is_bytes_path, is_keyword_only_argument_non_default,
is_pathlib_path_call,
};
use crate::{FixAvailability, Violation};
Expand Down Expand Up @@ -82,13 +82,17 @@ pub(crate) fn os_mkdir(checker: &Checker, call: &ExprCall, segments: &[&str]) {
return;
}

let range = call.range();
let mut diagnostic = checker.report_diagnostic(OsMkdir, call.func.range());

let Some(path) = call.arguments.find_argument_value("path", 0) else {
return;
};

if is_bytes_path(path, checker.semantic()) {
return;
}

let range = call.range();
let mut diagnostic = checker.report_diagnostic(OsMkdir, call.func.range());

if !is_fix_os_mkdir_enabled(checker.settings()) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::checkers::ast::Checker;
use crate::importer::ImportRequest;
use crate::preview::is_fix_os_symlink_enabled;
use crate::rules::flake8_use_pathlib::helpers::{
has_unknown_keywords_or_starred_expr, is_keyword_only_argument_non_default,
has_unknown_keywords_or_starred_expr, is_bytes_path, is_keyword_only_argument_non_default,
is_pathlib_path_call,
};
use crate::{FixAvailability, Violation};
Expand Down Expand Up @@ -80,6 +80,17 @@ pub(crate) fn os_symlink(checker: &Checker, call: &ExprCall, segments: &[&str])
return;
}

let (Some(src), Some(dst)) = (
call.arguments.find_argument_value("src", 0),
call.arguments.find_argument_value("dst", 1),
) else {
return;
};

if is_bytes_path(src, checker.semantic()) || is_bytes_path(dst, checker.semantic()) {
return;
}

let range = call.range();
let mut diagnostic = checker.report_diagnostic(OsSymlink, call.func.range());

Expand All @@ -98,13 +109,6 @@ pub(crate) fn os_symlink(checker: &Checker, call: &ExprCall, segments: &[&str])
return;
}

let (Some(src), Some(dst)) = (
call.arguments.find_argument_value("src", 0),
call.arguments.find_argument_value("dst", 1),
) else {
return;
};

diagnostic.try_set_fix(|| {
let (import_edit, binding) = checker.importer().get_or_import_symbol(
&ImportRequest::import("pathlib", "Path"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use ruff_text_size::Ranged;

use crate::checkers::ast::Checker;
use crate::rules::flake8_use_pathlib::helpers::{
is_file_descriptor, is_keyword_only_argument_non_default,
is_bytes_path, is_file_descriptor, is_keyword_only_argument_non_default,
};
use crate::rules::flake8_use_pathlib::{
rules::Glob,
Expand All @@ -28,25 +28,38 @@ pub(crate) fn replaceable_by_pathlib(checker: &Checker, call: &ExprCall) {
if call
.arguments
.find_argument_value("path", 0)
.is_some_and(|expr| is_file_descriptor(expr, checker.semantic()))
.is_some_and(|expr| {
is_file_descriptor(expr, checker.semantic())
|| is_bytes_path(expr, checker.semantic())
})
|| is_keyword_only_argument_non_default(&call.arguments, "dir_fd")
{
return;
}
checker.report_diagnostic_if_enabled(OsStat, range)
}
// PTH118
["os", "path", "join"] => checker.report_diagnostic_if_enabled(
OsPathJoin {
module: "path".to_string(),
joiner: if call.arguments.args.iter().any(Expr::is_starred_expr) {
Joiner::Joinpath
} else {
Joiner::Slash
["os", "path", "join"] => {
if call
.arguments
.args
.iter()
.any(|expr| is_bytes_path(expr, checker.semantic()))
{
return;
}
checker.report_diagnostic_if_enabled(
OsPathJoin {
module: "path".to_string(),
joiner: if call.arguments.args.iter().any(Expr::is_starred_expr) {
Joiner::Joinpath
} else {
Joiner::Slash
},
},
},
range,
),
range,
)
}
["os", "sep", "join"] => checker.report_diagnostic_if_enabled(
OsPathJoin {
module: "sep".to_string(),
Expand All @@ -59,7 +72,16 @@ pub(crate) fn replaceable_by_pathlib(checker: &Checker, call: &ExprCall) {
range,
),
// PTH122
["os", "path", "splitext"] => checker.report_diagnostic_if_enabled(OsPathSplitext, range),
["os", "path", "splitext"] => {
if call
.arguments
.find_argument_value("p", 0)
.is_some_and(|expr| is_bytes_path(expr, checker.semantic()))
{
return;
}
checker.report_diagnostic_if_enabled(OsPathSplitext, range)
}
// PTH124
["py", "path", "local"] => checker.report_diagnostic_if_enabled(PyPath, range),
// PTH207
Expand All @@ -73,6 +95,13 @@ pub(crate) fn replaceable_by_pathlib(checker: &Checker, call: &ExprCall) {
if is_keyword_only_argument_non_default(&call.arguments, "dir_fd") {
return;
}
if call
.arguments
.find_argument_value("pathname", 0)
.is_some_and(|expr| is_bytes_path(expr, checker.semantic()))
{
return;
}

checker.report_diagnostic_if_enabled(
Glob {
Expand All @@ -92,6 +121,13 @@ pub(crate) fn replaceable_by_pathlib(checker: &Checker, call: &ExprCall) {
if is_keyword_only_argument_non_default(&call.arguments, "dir_fd") {
return;
}
if call
.arguments
.find_argument_value("pathname", 0)
.is_some_and(|expr| is_bytes_path(expr, checker.semantic()))
{
return;
}

checker.report_diagnostic_if_enabled(
Glob {
Expand All @@ -105,7 +141,10 @@ pub(crate) fn replaceable_by_pathlib(checker: &Checker, call: &ExprCall) {
if call
.arguments
.find_argument_value("path", 0)
.is_some_and(|expr| is_file_descriptor(expr, checker.semantic()))
.is_some_and(|expr| {
is_file_descriptor(expr, checker.semantic())
|| is_bytes_path(expr, checker.semantic())
})
{
return;
}
Expand Down
Loading