mirror of
https://github.com/astral-sh/ruff.git
synced 2025-11-19 03:48:29 +00:00
[refurb] Fix FURB103 autofix (#21454)
This commit is contained in:
parent
3065f8dbbc
commit
7a546809c4
4 changed files with 90 additions and 27 deletions
|
|
@ -152,4 +152,13 @@ import json
|
|||
data = {"price": 100}
|
||||
|
||||
with open("test.json", "wb") as f:
|
||||
f.write(json.dumps(data, indent=4).encode("utf-8"))
|
||||
f.write(json.dumps(data, indent=4).encode("utf-8"))
|
||||
|
||||
# See: https://github.com/astral-sh/ruff/issues/21381
|
||||
with open("tmp_path/pyproject.toml", "w") as f:
|
||||
f.write(dedent(
|
||||
"""
|
||||
[project]
|
||||
other = 1.234
|
||||
""",
|
||||
))
|
||||
|
|
|
|||
|
|
@ -2,17 +2,15 @@ use ruff_diagnostics::{Applicability, Edit, Fix};
|
|||
use ruff_macros::{ViolationMetadata, derive_message_formats};
|
||||
use ruff_python_ast::{
|
||||
self as ast, Expr, Stmt,
|
||||
relocate::relocate_expr,
|
||||
visitor::{self, Visitor},
|
||||
};
|
||||
use ruff_python_codegen::Generator;
|
||||
use ruff_text_size::{Ranged, TextRange};
|
||||
use ruff_text_size::Ranged;
|
||||
|
||||
use crate::checkers::ast::Checker;
|
||||
use crate::fix::snippet::SourceCodeSnippet;
|
||||
use crate::importer::ImportRequest;
|
||||
use crate::rules::refurb::helpers::{FileOpen, find_file_opens};
|
||||
use crate::{FixAvailability, Violation};
|
||||
use crate::{FixAvailability, Locator, Violation};
|
||||
|
||||
/// ## What it does
|
||||
/// Checks for uses of `open` and `write` that can be replaced by `pathlib`
|
||||
|
|
@ -129,7 +127,7 @@ impl<'a> Visitor<'a> for WriteMatcher<'a, '_> {
|
|||
let open = self.candidates.remove(open);
|
||||
|
||||
if self.loop_counter == 0 {
|
||||
let suggestion = make_suggestion(&open, content, self.checker.generator());
|
||||
let suggestion = make_suggestion(&open, content, self.checker.locator());
|
||||
|
||||
let mut diagnostic = self.checker.report_diagnostic(
|
||||
WriteWholeFile {
|
||||
|
|
@ -172,27 +170,21 @@ fn match_write_call(expr: &Expr) -> Option<(&Expr, &Expr)> {
|
|||
Some((&*attr.value, call.arguments.args.first()?))
|
||||
}
|
||||
|
||||
fn make_suggestion(open: &FileOpen<'_>, arg: &Expr, generator: Generator) -> String {
|
||||
let name = ast::ExprName {
|
||||
id: open.mode.pathlib_method(),
|
||||
ctx: ast::ExprContext::Load,
|
||||
range: TextRange::default(),
|
||||
node_index: ruff_python_ast::AtomicNodeIndex::NONE,
|
||||
};
|
||||
let mut arg = arg.clone();
|
||||
relocate_expr(&mut arg, TextRange::default());
|
||||
let call = ast::ExprCall {
|
||||
func: Box::new(name.into()),
|
||||
arguments: ast::Arguments {
|
||||
args: Box::new([arg]),
|
||||
keywords: open.keywords.iter().copied().cloned().collect(),
|
||||
range: TextRange::default(),
|
||||
node_index: ruff_python_ast::AtomicNodeIndex::NONE,
|
||||
},
|
||||
range: TextRange::default(),
|
||||
node_index: ruff_python_ast::AtomicNodeIndex::NONE,
|
||||
};
|
||||
generator.expr(&call.into())
|
||||
fn make_suggestion(open: &FileOpen<'_>, arg: &Expr, locator: &Locator) -> String {
|
||||
let method_name = open.mode.pathlib_method();
|
||||
let arg_code = locator.slice(arg.range());
|
||||
|
||||
if open.keywords.is_empty() {
|
||||
format!("{method_name}({arg_code})")
|
||||
} else {
|
||||
format!(
|
||||
"{method_name}({arg_code}, {})",
|
||||
itertools::join(
|
||||
open.keywords.iter().map(|kw| locator.slice(kw.range())),
|
||||
", "
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn generate_fix(
|
||||
|
|
|
|||
|
|
@ -279,3 +279,34 @@ help: Replace with `Path("test.json")....`
|
|||
- with open("test.json", "wb") as f:
|
||||
- f.write(json.dumps(data, indent=4).encode("utf-8"))
|
||||
155 + pathlib.Path("test.json").write_bytes(json.dumps(data, indent=4).encode("utf-8"))
|
||||
156 |
|
||||
157 | # See: https://github.com/astral-sh/ruff/issues/21381
|
||||
158 | with open("tmp_path/pyproject.toml", "w") as f:
|
||||
|
||||
FURB103 [*] `open` and `write` should be replaced by `Path("tmp_path/pyproject.toml")....`
|
||||
--> FURB103.py:158:6
|
||||
|
|
||||
157 | # See: https://github.com/astral-sh/ruff/issues/21381
|
||||
158 | with open("tmp_path/pyproject.toml", "w") as f:
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
159 | f.write(dedent(
|
||||
160 | """
|
||||
|
|
||||
help: Replace with `Path("tmp_path/pyproject.toml")....`
|
||||
148 |
|
||||
149 | # See: https://github.com/astral-sh/ruff/issues/20785
|
||||
150 | import json
|
||||
151 + import pathlib
|
||||
152 |
|
||||
153 | data = {"price": 100}
|
||||
154 |
|
||||
--------------------------------------------------------------------------------
|
||||
156 | f.write(json.dumps(data, indent=4).encode("utf-8"))
|
||||
157 |
|
||||
158 | # See: https://github.com/astral-sh/ruff/issues/21381
|
||||
- with open("tmp_path/pyproject.toml", "w") as f:
|
||||
- f.write(dedent(
|
||||
159 + pathlib.Path("tmp_path/pyproject.toml").write_text(dedent(
|
||||
160 | """
|
||||
161 | [project]
|
||||
162 | other = 1.234
|
||||
|
|
|
|||
|
|
@ -209,3 +209,34 @@ help: Replace with `Path("test.json")....`
|
|||
- with open("test.json", "wb") as f:
|
||||
- f.write(json.dumps(data, indent=4).encode("utf-8"))
|
||||
155 + pathlib.Path("test.json").write_bytes(json.dumps(data, indent=4).encode("utf-8"))
|
||||
156 |
|
||||
157 | # See: https://github.com/astral-sh/ruff/issues/21381
|
||||
158 | with open("tmp_path/pyproject.toml", "w") as f:
|
||||
|
||||
FURB103 [*] `open` and `write` should be replaced by `Path("tmp_path/pyproject.toml")....`
|
||||
--> FURB103.py:158:6
|
||||
|
|
||||
157 | # See: https://github.com/astral-sh/ruff/issues/21381
|
||||
158 | with open("tmp_path/pyproject.toml", "w") as f:
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
159 | f.write(dedent(
|
||||
160 | """
|
||||
|
|
||||
help: Replace with `Path("tmp_path/pyproject.toml")....`
|
||||
148 |
|
||||
149 | # See: https://github.com/astral-sh/ruff/issues/20785
|
||||
150 | import json
|
||||
151 + import pathlib
|
||||
152 |
|
||||
153 | data = {"price": 100}
|
||||
154 |
|
||||
--------------------------------------------------------------------------------
|
||||
156 | f.write(json.dumps(data, indent=4).encode("utf-8"))
|
||||
157 |
|
||||
158 | # See: https://github.com/astral-sh/ruff/issues/21381
|
||||
- with open("tmp_path/pyproject.toml", "w") as f:
|
||||
- f.write(dedent(
|
||||
159 + pathlib.Path("tmp_path/pyproject.toml").write_text(dedent(
|
||||
160 | """
|
||||
161 | [project]
|
||||
162 | other = 1.234
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue