Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions crates/pyrefly_types/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1267,6 +1267,13 @@ pub mod tests {
Type::Literal(Lit::Bool(false)).to_string(),
"Literal[False]"
);
assert_eq!(
Type::Literal(Lit::Bytes(
vec![b' ', b'\t', b'\n', b'\r', 0x0b, 0x0c].into_boxed_slice()
))
.to_string(),
r"Literal[b' \t\n\r\x0b\x0c']"
);

// Enum literals (not all of these types make sense, we're only providing what's relevant)
let my_enum = ClassType::new(fake_class("MyEnum", "mod.ule", 5), TArgs::default());
Expand Down
14 changes: 9 additions & 5 deletions crates/pyrefly_types/src/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* LICENSE file in the root directory of this source tree.
*/

use std::char;
use std::fmt;
use std::fmt::Display;

Expand Down Expand Up @@ -62,10 +61,15 @@ impl Display for Lit {
}
Lit::Bytes(bytes) => {
write!(f, "b'")?;
for byte in bytes {
match char::from_u32(*byte as u32) {
Some(ch) => write!(f, "{ch}")?,
None => write!(f, "\\x{byte:02x}")?,
for byte in bytes.iter().copied() {
match byte {
b'\t' => write!(f, "\\t")?,
b'\n' => write!(f, "\\n")?,
b'\r' => write!(f, "\\r")?,
b'\\' => write!(f, "\\\\")?,
b'\'' => write!(f, "\\'")?,
0x20..=0x7e => write!(f, "{}", byte as char)?,
_ => write!(f, "\\x{byte:02x}")?,
}
}
write!(f, "'")
Expand Down