From 99370cecf931e1f133a976acb84992936436413e Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Sun, 7 Jul 2024 23:37:27 +0100 Subject: [PATCH] [Ecosystem checks] `trio` has changed its default branch name to `main` (#12225) Fixes CI errors seen in https://github.com/astral-sh/ruff/pull/12224#issuecomment-2212594024 x-ref https://github.com/python-trio/trio/commit/17b3644f64b4bb10176e789dc23a8b57b1399f89 --- crates/ruff_linter/src/rules/refurb/helpers.rs | 16 +++++++++++++--- .../src/rules/refurb/rules/read_whole_file.rs | 7 ++++++- .../src/rules/refurb/rules/write_whole_file.rs | 15 +++++++++++++-- python/ruff-ecosystem/ruff_ecosystem/defaults.py | 2 +- 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/crates/ruff_linter/src/rules/refurb/helpers.rs b/crates/ruff_linter/src/rules/refurb/helpers.rs index d82f105d9b29c1..642e4b28652283 100644 --- a/crates/ruff_linter/src/rules/refurb/helpers.rs +++ b/crates/ruff_linter/src/rules/refurb/helpers.rs @@ -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`. @@ -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> { 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() } @@ -130,6 +133,7 @@ fn find_file_open<'a>( with: &'a ast::StmtWith, semantic: &'a SemanticModel<'a>, read_mode: bool, + python_version: PythonVersion, ) -> Option> { // We want to match `open(...) as var`. let ast::ExprCall { @@ -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); @@ -228,6 +232,7 @@ 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)> { let mut result: Vec<&ast::Keyword> = vec![]; let mut mode: Option = None; @@ -235,11 +240,16 @@ fn match_open_keywords( 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); } diff --git a/crates/ruff_linter/src/rules/refurb/rules/read_whole_file.rs b/crates/ruff_linter/src/rules/refurb/rules/read_whole_file.rs index e3e39cb022b5eb..b0c2424a552d59 100644 --- a/crates/ruff_linter/src/rules/refurb/rules/read_whole_file.rs +++ b/crates/ruff_linter/src/rules/refurb/rules/read_whole_file.rs @@ -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; } diff --git a/crates/ruff_linter/src/rules/refurb/rules/write_whole_file.rs b/crates/ruff_linter/src/rules/refurb/rules/write_whole_file.rs index 9898d1c8e0f41c..11934fe2da6cc1 100644 --- a/crates/ruff_linter/src/rules/refurb/rules/write_whole_file.rs +++ b/crates/ruff_linter/src/rules/refurb/rules/write_whole_file.rs @@ -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}; @@ -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; } @@ -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(), ) diff --git a/python/ruff-ecosystem/ruff_ecosystem/defaults.py b/python/ruff-ecosystem/ruff_ecosystem/defaults.py index 9350513227c40d..5c2f8c55d196a8 100644 --- a/python/ruff-ecosystem/ruff_ecosystem/defaults.py +++ b/python/ruff-ecosystem/ruff_ecosystem/defaults.py @@ -127,7 +127,7 @@ }, ), Project(repo=Repository(owner="agronholm", name="anyio", ref="master")), - Project(repo=Repository(owner="python-trio", name="trio", ref="master")), + Project(repo=Repository(owner="python-trio", name="trio", ref="main")), Project(repo=Repository(owner="wntrblm", name="nox", ref="main")), Project(repo=Repository(owner="pytest-dev", name="pytest", ref="main")), Project(repo=Repository(owner="encode", name="httpx", ref="master")),