mirror of https://github.com/astral-sh/ruff
[`refurb`] Preserve argument ordering in autofix (`FURB103`) (#20790)
Fixes https://github.com/astral-sh/ruff/issues/20785
This commit is contained in:
parent
1d111c8780
commit
b93d8f2b9f
|
|
@ -145,3 +145,11 @@ with open("file.txt", "w") as f:
|
||||||
with open("file.txt", "w") as f:
|
with open("file.txt", "w") as f:
|
||||||
for line in text:
|
for line in text:
|
||||||
f.write(line)
|
f.write(line)
|
||||||
|
|
||||||
|
# See: https://github.com/astral-sh/ruff/issues/20785
|
||||||
|
import json
|
||||||
|
|
||||||
|
data = {"price": 100}
|
||||||
|
|
||||||
|
with open("test.json", "wb") as f:
|
||||||
|
f.write(json.dumps(data, indent=4).encode("utf-8"))
|
||||||
|
|
@ -5,7 +5,6 @@ use ruff_python_ast::{
|
||||||
relocate::relocate_expr,
|
relocate::relocate_expr,
|
||||||
visitor::{self, Visitor},
|
visitor::{self, Visitor},
|
||||||
};
|
};
|
||||||
|
|
||||||
use ruff_python_codegen::Generator;
|
use ruff_python_codegen::Generator;
|
||||||
use ruff_text_size::{Ranged, TextRange};
|
use ruff_text_size::{Ranged, TextRange};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -134,3 +134,14 @@ FURB103 `open` and `write` should be replaced by `Path("file.txt").write_text(fo
|
||||||
75 | f.write(foobar)
|
75 | f.write(foobar)
|
||||||
|
|
|
|
||||||
help: Replace with `Path("file.txt").write_text(foobar, newline="\r\n")`
|
help: Replace with `Path("file.txt").write_text(foobar, newline="\r\n")`
|
||||||
|
|
||||||
|
FURB103 `open` and `write` should be replaced by `Path("test.json")....`
|
||||||
|
--> FURB103.py:154:6
|
||||||
|
|
|
||||||
|
152 | data = {"price": 100}
|
||||||
|
153 |
|
||||||
|
154 | with open("test.json", "wb") as f:
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
155 | f.write(json.dumps(data, indent=4).encode("utf-8"))
|
||||||
|
|
|
||||||
|
help: Replace with `Path("test.json")....`
|
||||||
|
|
|
||||||
|
|
@ -257,4 +257,25 @@ help: Replace with `Path("file.txt").write_text(foobar, newline="\r\n")`
|
||||||
75 + pathlib.Path("file.txt").write_text(foobar, newline="\r\n")
|
75 + pathlib.Path("file.txt").write_text(foobar, newline="\r\n")
|
||||||
76 |
|
76 |
|
||||||
77 | # Non-errors.
|
77 | # Non-errors.
|
||||||
78 |
|
78 |
|
||||||
|
|
||||||
|
FURB103 [*] `open` and `write` should be replaced by `Path("test.json")....`
|
||||||
|
--> FURB103.py:154:6
|
||||||
|
|
|
||||||
|
152 | data = {"price": 100}
|
||||||
|
153 |
|
||||||
|
154 | with open("test.json", "wb") as f:
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
155 | f.write(json.dumps(data, indent=4).encode("utf-8"))
|
||||||
|
|
|
||||||
|
help: Replace with `Path("test.json")....`
|
||||||
|
148 |
|
||||||
|
149 | # See: https://github.com/astral-sh/ruff/issues/20785
|
||||||
|
150 | import json
|
||||||
|
151 + import pathlib
|
||||||
|
152 |
|
||||||
|
153 | data = {"price": 100}
|
||||||
|
154 |
|
||||||
|
- 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"))
|
||||||
|
|
|
||||||
|
|
@ -104,3 +104,14 @@ FURB103 `open` and `write` should be replaced by `Path("file.txt").write_text(ba
|
||||||
51 | # writes a single time to file and that bit they can replace.
|
51 | # writes a single time to file and that bit they can replace.
|
||||||
|
|
|
|
||||||
help: Replace with `Path("file.txt").write_text(bar(bar(a + x)))`
|
help: Replace with `Path("file.txt").write_text(bar(bar(a + x)))`
|
||||||
|
|
||||||
|
FURB103 `open` and `write` should be replaced by `Path("test.json")....`
|
||||||
|
--> FURB103.py:154:6
|
||||||
|
|
|
||||||
|
152 | data = {"price": 100}
|
||||||
|
153 |
|
||||||
|
154 | with open("test.json", "wb") as f:
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
155 | f.write(json.dumps(data, indent=4).encode("utf-8"))
|
||||||
|
|
|
||||||
|
help: Replace with `Path("test.json")....`
|
||||||
|
|
|
||||||
|
|
@ -3372,7 +3372,7 @@ impl Arguments {
|
||||||
pub fn arguments_source_order(&self) -> impl Iterator<Item = ArgOrKeyword<'_>> {
|
pub fn arguments_source_order(&self) -> impl Iterator<Item = ArgOrKeyword<'_>> {
|
||||||
let args = self.args.iter().map(ArgOrKeyword::Arg);
|
let args = self.args.iter().map(ArgOrKeyword::Arg);
|
||||||
let keywords = self.keywords.iter().map(ArgOrKeyword::Keyword);
|
let keywords = self.keywords.iter().map(ArgOrKeyword::Keyword);
|
||||||
args.merge_by(keywords, |left, right| left.start() < right.start())
|
args.merge_by(keywords, |left, right| left.start() <= right.start())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn inner_range(&self) -> TextRange {
|
pub fn inner_range(&self) -> TextRange {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue