mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-24 05:25:17 +00:00
Preserve quotes in generated byte strings (#15778)
Some checks are pending
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / benchmarks (push) Blocked by required conditions
Some checks are pending
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / benchmarks (push) Blocked by required conditions
## Summary This is a very closely related follow-up to #15726, adding the same quote-preserving behavior to bytestrings. Only one rule (UP018) was affected this time, and it was easy to mirror the plain string changes. ## Test Plan Existing tests
This commit is contained in:
parent
9c938442e5
commit
98d20a8219
5 changed files with 76 additions and 35 deletions
|
@ -153,8 +153,8 @@ impl<'a> Generator<'a> {
|
|||
self.p(s.as_str());
|
||||
}
|
||||
|
||||
fn p_bytes_repr(&mut self, s: &[u8]) {
|
||||
let escape = AsciiEscape::with_preferred_quote(s, self.quote);
|
||||
fn p_bytes_repr(&mut self, s: &[u8], quote: Quote) {
|
||||
let escape = AsciiEscape::with_preferred_quote(s, quote);
|
||||
if let Some(len) = escape.layout().len {
|
||||
self.buffer.reserve(len);
|
||||
}
|
||||
|
@ -1100,7 +1100,7 @@ impl<'a> Generator<'a> {
|
|||
let mut first = true;
|
||||
for bytes_literal in value {
|
||||
self.p_delim(&mut first, " ");
|
||||
self.p_bytes_repr(&bytes_literal.value);
|
||||
self.p_bytes_repr(&bytes_literal.value, bytes_literal.flags.quote_style());
|
||||
}
|
||||
}
|
||||
Expr::NumberLiteral(ast::ExprNumberLiteral { value, .. }) => {
|
||||
|
@ -1739,11 +1739,11 @@ class Foo:
|
|||
|
||||
#[test]
|
||||
fn quote() {
|
||||
assert_eq!(round_trip(r#""hello""#), r#""hello""#);
|
||||
assert_round_trip!(r#""hello""#);
|
||||
assert_round_trip!(r"'hello'");
|
||||
assert_round_trip!(r"u'hello'");
|
||||
assert_round_trip!(r"r'hello'");
|
||||
assert_eq!(round_trip(r"b'hello'"), r#"b"hello""#);
|
||||
assert_round_trip!(r"b'hello'");
|
||||
assert_eq!(round_trip(r#"("abc" "def" "ghi")"#), r#""abc" "def" "ghi""#);
|
||||
assert_eq!(round_trip(r#""he\"llo""#), r#"'he"llo'"#);
|
||||
assert_eq!(round_trip(r#"f"abc{'def'}{1}""#), r#"f"abc{'def'}{1}""#);
|
||||
|
@ -1806,25 +1806,28 @@ if True:
|
|||
$end,
|
||||
);
|
||||
};
|
||||
($quote:expr, $start:expr) => {
|
||||
round_trip_with!($quote, $start, $start);
|
||||
};
|
||||
}
|
||||
|
||||
// setting Generator::quote works for bytestrings
|
||||
round_trip_with!(Quote::Double, r#"b"hello""#, r#"b"hello""#);
|
||||
round_trip_with!(Quote::Single, r#"b"hello""#, r"b'hello'");
|
||||
round_trip_with!(Quote::Double, r"b'hello'", r#"b"hello""#);
|
||||
round_trip_with!(Quote::Single, r"b'hello'", r"b'hello'");
|
||||
|
||||
// and for f-strings
|
||||
// setting Generator::quote works for f-strings
|
||||
round_trip_with!(Quote::Double, r#"f"hello""#, r#"f"hello""#);
|
||||
round_trip_with!(Quote::Single, r#"f"hello""#, r"f'hello'");
|
||||
round_trip_with!(Quote::Double, r"f'hello'", r#"f"hello""#);
|
||||
round_trip_with!(Quote::Single, r"f'hello'", r"f'hello'");
|
||||
|
||||
// but not for string literals, where the `Quote` is taken directly from their flags
|
||||
round_trip_with!(Quote::Double, r#""hello""#, r#""hello""#);
|
||||
round_trip_with!(Quote::Single, r#""hello""#, r#""hello""#); // no effect
|
||||
round_trip_with!(Quote::Double, r"'hello'", r#"'hello'"#); // no effect
|
||||
round_trip_with!(Quote::Single, r"'hello'", r"'hello'");
|
||||
// but not for bytestrings
|
||||
round_trip_with!(Quote::Double, r#"b"hello""#);
|
||||
round_trip_with!(Quote::Single, r#"b"hello""#); // no effect
|
||||
round_trip_with!(Quote::Double, r"b'hello'"); // no effect
|
||||
round_trip_with!(Quote::Single, r"b'hello'");
|
||||
|
||||
// or for string literals, where the `Quote` is taken directly from their flags
|
||||
round_trip_with!(Quote::Double, r#""hello""#);
|
||||
round_trip_with!(Quote::Single, r#""hello""#); // no effect
|
||||
round_trip_with!(Quote::Double, r"'hello'"); // no effect
|
||||
round_trip_with!(Quote::Single, r"'hello'");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue