Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't suggest replacing builtin.open() with Path.open() if the latter doesn't support all options #7637

Merged
merged 2 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,14 @@
fp.read()
open(p).close()
os.getcwdb(p)

# https://github.com/astral-sh/ruff/issues/7620
def opener(path, flags):
return os.open(path, flags, dir_fd=os.open('somedir', os.O_RDONLY))


open(p, closefd=False)
open(p, opener=opener)
open(p, mode='r', buffering=- 1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
open(p, 'r', - 1, None, None, None, True, None)
open(p, 'r', - 1, None, None, None, False, opener)
2 changes: 1 addition & 1 deletion crates/ruff_linter/src/checkers/ast/analyze/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ pub(crate) fn expression(expr: &Expr, checker: &mut Checker) {
Rule::OsPathGetctime,
Rule::Glob,
]) {
flake8_use_pathlib::rules::replaceable_by_pathlib(checker, func);
flake8_use_pathlib::rules::replaceable_by_pathlib(checker, call);
}
if checker.enabled(Rule::PathConstructorCurrentDirectory) {
flake8_use_pathlib::rules::path_constructor_current_directory(checker, expr, func);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ruff_diagnostics::{Diagnostic, DiagnosticKind};
use ruff_python_ast::Expr;
use ruff_python_ast::{Constant, ExprCall};
use ruff_text_size::Ranged;

use crate::checkers::ast::Checker;
Expand All @@ -15,11 +15,11 @@ use crate::rules::flake8_use_pathlib::violations::{
};
use crate::settings::types::PythonVersion;

pub(crate) fn replaceable_by_pathlib(checker: &mut Checker, expr: &Expr) {
pub(crate) fn replaceable_by_pathlib(checker: &mut Checker, call: &ExprCall) {
if let Some(diagnostic_kind) =
checker
.semantic()
.resolve_call_path(expr)
.resolve_call_path(&call.func)
.and_then(|call_path| match call_path.as_slice() {
// PTH100
["os", "path", "abspath"] => Some(OsPathAbspath.into()),
Expand Down Expand Up @@ -86,7 +86,39 @@ pub(crate) fn replaceable_by_pathlib(checker: &mut Checker, expr: &Expr) {
// PTH205
["os", "path", "getctime"] => Some(OsPathGetctime.into()),
// PTH123
["" | "builtin", "open"] => Some(BuiltinOpen.into()),
["" | "builtin", "open"] => {
// `closefd` and `openener` are not supported by pathlib, so check if they are
// are set to non-default values.
// https://github.com/astral-sh/ruff/issues/7620
// Signature as of Python 3.11 (https://docs.python.org/3/library/functions.html#open):
// ```
// 0 1 2 3 4 5
// open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None,
// 6 7
// closefd=True, opener=None)
// ^^^^ ^^^^
// ```
if call
.arguments
.find_argument("closefd", 6)
.is_some_and(|expr| {
!expr.as_constant_expr().is_some_and(|constant| {
matches!(constant.value, Constant::Bool(true))
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can these be done with a single match? Like:

matches!(expr, Expr::Constant(ast::ExprConstant { value: Constant::Bool(true), .. }))

})
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a .is_default_value("closefd", 6, Constant::Bool(true)) helper? If no, should i create one?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think what you have here is fine personally.

|| call
.arguments
.find_argument("opener", 7)
.is_some_and(|expr| {
!expr.as_constant_expr().is_some_and(|constant| {
matches!(constant.value, Constant::None)
})
})
{
return None;
}
Some(BuiltinOpen.into())
}
// PTH124
["py", "path", "local"] => Some(PyPath.into()),
// PTH207
Expand All @@ -110,7 +142,7 @@ pub(crate) fn replaceable_by_pathlib(checker: &mut Checker, expr: &Expr) {
_ => None,
})
{
let diagnostic = Diagnostic::new::<DiagnosticKind>(diagnostic_kind, expr.range());
let diagnostic = Diagnostic::new::<DiagnosticKind>(diagnostic_kind, call.func.range());

if checker.enabled(diagnostic.kind.rule()) {
checker.diagnostics.push(diagnostic);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,27 @@ full_name.py:35:1: PTH109 `os.getcwd()` should be replaced by `Path.cwd()`
34 | open(p).close()
35 | os.getcwdb(p)
| ^^^^^^^^^^ PTH109
36 |
37 | # https://github.com/astral-sh/ruff/issues/7620
|

full_name.py:44:1: PTH123 `open()` should be replaced by `Path.open()`
|
42 | open(p, closefd=False)
43 | open(p, opener=opener)
44 | open(p, mode='r', buffering=- 1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
| ^^^^ PTH123
45 | open(p, 'r', - 1, None, None, None, True, None)
46 | open(p, 'r', - 1, None, None, None, False, opener)
|

full_name.py:45:1: PTH123 `open()` should be replaced by `Path.open()`
|
43 | open(p, opener=opener)
44 | open(p, mode='r', buffering=- 1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
45 | open(p, 'r', - 1, None, None, None, True, None)
| ^^^^ PTH123
46 | open(p, 'r', - 1, None, None, None, False, opener)
|