Skip to content

Commit

Permalink
[Ecosystem checks] trio has changed its default branch name to `mai…
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWaygood authored and evanrittenhouse committed Jul 8, 2024
1 parent d9c15e7 commit 50d3a1f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
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
15 changes: 13 additions & 2 deletions crates/ruff_linter/src/rules/refurb/rules/write_whole_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use ruff_text_size::{Ranged, TextRange};

use crate::checkers::ast::Checker;
use crate::fix::snippet::SourceCodeSnippet;
use crate::settings::types::PythonVersion;

use super::super::helpers::{find_file_opens, FileOpen};

Expand Down Expand Up @@ -59,7 +60,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 All @@ -79,7 +85,12 @@ pub(crate) fn write_whole_file(checker: &mut Checker, with: &ast::StmtWith) {
Diagnostic::new(
WriteWholeFile {
filename: SourceCodeSnippet::from_str(&checker.generator().expr(open.filename)),
suggestion: make_suggestion(open, content, checker.generator()),
suggestion: make_suggestion(
open,
content,
checker.generator(),
checker.settings.target_version,
),
},
open.item.range(),
)
Expand Down

0 comments on commit 50d3a1f

Please sign in to comment.