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

Restrict fowarding newline argument in open() calls to Python versions >= 3.10 #12244

Merged
merged 3 commits into from
Jul 9, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions crates/ruff_linter/src/rules/refurb/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use ruff_python_codegen::Generator;
use ruff_python_semantic::{BindingId, ResolvedReference, SemanticModel};
use ruff_text_size::{Ranged, TextRange};

use crate::settings::types::PythonVersion;

/// Format a code snippet to call `name.method()`.
pub(super) fn generate_method_call(name: Name, method: &str, generator: Generator) -> String {
// Construct `name`.
Expand Down Expand Up @@ -117,10 +119,11 @@ pub(super) fn find_file_opens<'a>(
with: &'a ast::StmtWith,
semantic: &'a SemanticModel<'a>,
read_mode: bool,
python_version: PythonVersion,
) -> Vec<FileOpen<'a>> {
with.items
.iter()
.filter_map(|item| find_file_open(item, with, semantic, read_mode))
.filter_map(|item| find_file_open(item, with, semantic, read_mode, python_version))
.collect()
}

Expand All @@ -130,6 +133,7 @@ fn find_file_open<'a>(
with: &'a ast::StmtWith,
semantic: &'a SemanticModel<'a>,
read_mode: bool,
python_version: PythonVersion,
) -> Option<FileOpen<'a>> {
// We want to match `open(...) as var`.
let ast::ExprCall {
Expand Down Expand Up @@ -157,7 +161,7 @@ fn find_file_open<'a>(
let (filename, pos_mode) = match_open_args(args)?;

// Match keyword arguments, get keyword arguments to forward and possibly mode.
let (keywords, kw_mode) = match_open_keywords(keywords, read_mode)?;
let (keywords, kw_mode) = match_open_keywords(keywords, read_mode, python_version)?;

let mode = kw_mode.unwrap_or(pos_mode);

Expand Down Expand Up @@ -228,18 +232,24 @@ fn match_open_args(args: &[Expr]) -> Option<(&Expr, OpenMode)> {
fn match_open_keywords(
keywords: &[ast::Keyword],
read_mode: bool,
target_version: PythonVersion,
) -> Option<(Vec<&ast::Keyword>, Option<OpenMode>)> {
let mut result: Vec<&ast::Keyword> = vec![];
let mut mode: Option<OpenMode> = None;

for keyword in keywords {
match keyword.arg.as_ref()?.as_str() {
"encoding" | "errors" => result.push(keyword),
// newline is only valid for write_text
"newline" => {
if read_mode {
// newline is only valid for write_text
return None;
} else if target_version < PythonVersion::Py310 {
// Pathlib on versions earlier than 3.10 doesn't support the `newline`
// argument, so we can't forward it
continue;
}

result.push(keyword);
}

Expand Down
7 changes: 6 additions & 1 deletion crates/ruff_linter/src/rules/refurb/rules/read_whole_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ pub(crate) fn read_whole_file(checker: &mut Checker, with: &ast::StmtWith) {
}

// First we go through all the items in the statement and find all `open` operations.
let candidates = find_file_opens(with, checker.semantic(), true);
let candidates = find_file_opens(
with,
checker.semantic(),
true,
checker.settings.target_version,
);
if candidates.is_empty() {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ pub(crate) fn write_whole_file(checker: &mut Checker, with: &ast::StmtWith) {
}

// First we go through all the items in the statement and find all `open` operations.
let candidates = find_file_opens(with, checker.semantic(), false);
let candidates = find_file_opens(
with,
checker.semantic(),
false,
checker.settings.target_version,
);
if candidates.is_empty() {
return;
}
Expand Down
Loading