mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-16 00:20:38 +00:00
Added bytes type and some inference (#13061)
## Summary This PR adds the `bytes` type to red-knot: - Added the `bytes` type - Added support for bytes literals - Support for the `+` operator Improves on #12701 Big TODO on supporting and normalizing r-prefixed bytestrings (`rb"hello\n"`) ## Test Plan Added a test for a bytes literals, concatenation, and corner values
This commit is contained in:
parent
2edd32aa31
commit
b9c8113a8a
6 changed files with 73 additions and 6 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -1926,6 +1926,7 @@ dependencies = [
|
||||||
"ruff_db",
|
"ruff_db",
|
||||||
"ruff_index",
|
"ruff_index",
|
||||||
"ruff_python_ast",
|
"ruff_python_ast",
|
||||||
|
"ruff_python_literal",
|
||||||
"ruff_python_parser",
|
"ruff_python_parser",
|
||||||
"ruff_python_stdlib",
|
"ruff_python_stdlib",
|
||||||
"ruff_source_file",
|
"ruff_source_file",
|
||||||
|
|
|
@ -17,6 +17,7 @@ ruff_python_ast = { workspace = true }
|
||||||
ruff_python_stdlib = { workspace = true }
|
ruff_python_stdlib = { workspace = true }
|
||||||
ruff_source_file = { workspace = true }
|
ruff_source_file = { workspace = true }
|
||||||
ruff_text_size = { workspace = true }
|
ruff_text_size = { workspace = true }
|
||||||
|
ruff_python_literal = { workspace = true }
|
||||||
|
|
||||||
anyhow = { workspace = true }
|
anyhow = { workspace = true }
|
||||||
bitflags = { workspace = true }
|
bitflags = { workspace = true }
|
||||||
|
|
|
@ -181,6 +181,8 @@ pub enum Type<'db> {
|
||||||
IntLiteral(i64),
|
IntLiteral(i64),
|
||||||
/// A boolean literal, either `True` or `False`.
|
/// A boolean literal, either `True` or `False`.
|
||||||
BooleanLiteral(bool),
|
BooleanLiteral(bool),
|
||||||
|
/// A bytes literal
|
||||||
|
BytesLiteral(BytesLiteralType<'db>),
|
||||||
// TODO protocols, callable types, overloads, generics, type vars
|
// TODO protocols, callable types, overloads, generics, type vars
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -276,6 +278,10 @@ impl<'db> Type<'db> {
|
||||||
Type::Unknown
|
Type::Unknown
|
||||||
}
|
}
|
||||||
Type::BooleanLiteral(_) => Type::Unknown,
|
Type::BooleanLiteral(_) => Type::Unknown,
|
||||||
|
Type::BytesLiteral(_) => {
|
||||||
|
// TODO defer to Type::Instance(<bytes from typeshed>).member
|
||||||
|
Type::Unknown
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -372,6 +378,12 @@ pub struct IntersectionType<'db> {
|
||||||
negative: FxOrderSet<Type<'db>>,
|
negative: FxOrderSet<Type<'db>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[salsa::interned]
|
||||||
|
pub struct BytesLiteralType<'db> {
|
||||||
|
#[return_ref]
|
||||||
|
value: Box<[u8]>,
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
|
|
|
@ -2,6 +2,9 @@
|
||||||
|
|
||||||
use std::fmt::{Display, Formatter};
|
use std::fmt::{Display, Formatter};
|
||||||
|
|
||||||
|
use ruff_python_ast::str::Quote;
|
||||||
|
use ruff_python_literal::escape::AsciiEscape;
|
||||||
|
|
||||||
use crate::types::{IntersectionType, Type, UnionType};
|
use crate::types::{IntersectionType, Type, UnionType};
|
||||||
use crate::Db;
|
use crate::Db;
|
||||||
|
|
||||||
|
@ -38,6 +41,14 @@ impl Display for DisplayType<'_> {
|
||||||
Type::BooleanLiteral(boolean) => {
|
Type::BooleanLiteral(boolean) => {
|
||||||
write!(f, "Literal[{}]", if *boolean { "True" } else { "False" })
|
write!(f, "Literal[{}]", if *boolean { "True" } else { "False" })
|
||||||
}
|
}
|
||||||
|
Type::BytesLiteral(bytes) => {
|
||||||
|
let escape =
|
||||||
|
AsciiEscape::with_preferred_quote(bytes.value(self.db).as_ref(), Quote::Double);
|
||||||
|
|
||||||
|
f.write_str("Literal[")?;
|
||||||
|
escape.bytes_repr().write(f)?;
|
||||||
|
f.write_str("]")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,8 +43,8 @@ use crate::semantic_index::symbol::{FileScopeId, NodeWithScopeKind, NodeWithScop
|
||||||
use crate::semantic_index::SemanticIndex;
|
use crate::semantic_index::SemanticIndex;
|
||||||
use crate::types::diagnostic::{TypeCheckDiagnostic, TypeCheckDiagnostics};
|
use crate::types::diagnostic::{TypeCheckDiagnostic, TypeCheckDiagnostics};
|
||||||
use crate::types::{
|
use crate::types::{
|
||||||
builtins_symbol_ty_by_name, definitions_ty, global_symbol_ty_by_name, ClassType, FunctionType,
|
builtins_symbol_ty_by_name, definitions_ty, global_symbol_ty_by_name, BytesLiteralType,
|
||||||
Name, Type, UnionBuilder,
|
ClassType, FunctionType, Name, Type, UnionBuilder,
|
||||||
};
|
};
|
||||||
use crate::Db;
|
use crate::Db;
|
||||||
|
|
||||||
|
@ -1206,9 +1206,12 @@ impl<'db> TypeInferenceBuilder<'db> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::unused_self)]
|
#[allow(clippy::unused_self)]
|
||||||
fn infer_bytes_literal_expression(&mut self, _literal: &ast::ExprBytesLiteral) -> Type<'db> {
|
fn infer_bytes_literal_expression(&mut self, literal: &ast::ExprBytesLiteral) -> Type<'db> {
|
||||||
// TODO
|
// TODO: ignoring r/R prefixes for now, should normalize bytes values
|
||||||
Type::Unknown
|
Type::BytesLiteral(BytesLiteralType::new(
|
||||||
|
self.db,
|
||||||
|
literal.value.bytes().collect(),
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn infer_fstring_expression(&mut self, fstring: &ast::ExprFString) -> Type<'db> {
|
fn infer_fstring_expression(&mut self, fstring: &ast::ExprFString) -> Type<'db> {
|
||||||
|
@ -1684,6 +1687,7 @@ impl<'db> TypeInferenceBuilder<'db> {
|
||||||
let left_ty = self.infer_expression(left);
|
let left_ty = self.infer_expression(left);
|
||||||
let right_ty = self.infer_expression(right);
|
let right_ty = self.infer_expression(right);
|
||||||
|
|
||||||
|
// TODO flatten the matches by matching on (left_ty, right_ty, op)
|
||||||
match left_ty {
|
match left_ty {
|
||||||
Type::Any => Type::Any,
|
Type::Any => Type::Any,
|
||||||
Type::Unknown => Type::Unknown,
|
Type::Unknown => Type::Unknown,
|
||||||
|
@ -1722,6 +1726,22 @@ impl<'db> TypeInferenceBuilder<'db> {
|
||||||
_ => Type::Unknown, // TODO
|
_ => Type::Unknown, // TODO
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Type::BytesLiteral(lhs) => {
|
||||||
|
match right_ty {
|
||||||
|
Type::BytesLiteral(rhs) => {
|
||||||
|
match op {
|
||||||
|
ast::Operator::Add => Type::BytesLiteral(BytesLiteralType::new(
|
||||||
|
self.db,
|
||||||
|
[lhs.value(self.db).as_ref(), rhs.value(self.db).as_ref()]
|
||||||
|
.concat()
|
||||||
|
.into_boxed_slice(),
|
||||||
|
)),
|
||||||
|
_ => Type::Unknown, // TODO
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => Type::Unknown, // TODO
|
||||||
|
}
|
||||||
|
}
|
||||||
_ => Type::Unknown, // TODO
|
_ => Type::Unknown, // TODO
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2235,6 +2255,28 @@ mod tests {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn bytes_type() -> anyhow::Result<()> {
|
||||||
|
let mut db = setup_db();
|
||||||
|
|
||||||
|
db.write_dedented(
|
||||||
|
"src/a.py",
|
||||||
|
"
|
||||||
|
w = b'red' b'knot'
|
||||||
|
x = b'hello'
|
||||||
|
y = b'world' + b'!'
|
||||||
|
z = b'\\xff\\x00'
|
||||||
|
",
|
||||||
|
)?;
|
||||||
|
|
||||||
|
assert_public_ty(&db, "src/a.py", "w", "Literal[b\"redknot\"]");
|
||||||
|
assert_public_ty(&db, "src/a.py", "x", "Literal[b\"hello\"]");
|
||||||
|
assert_public_ty(&db, "src/a.py", "y", "Literal[b\"world!\"]");
|
||||||
|
assert_public_ty(&db, "src/a.py", "z", "Literal[b\"\\xff\\x00\"]");
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn resolve_union() -> anyhow::Result<()> {
|
fn resolve_union() -> anyhow::Result<()> {
|
||||||
let mut db = setup_db();
|
let mut db = setup_db();
|
||||||
|
|
|
@ -2152,7 +2152,7 @@ impl BytesLiteralValue {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns an iterator over the bytes of the concatenated bytes.
|
/// Returns an iterator over the bytes of the concatenated bytes.
|
||||||
fn bytes(&self) -> impl Iterator<Item = u8> + '_ {
|
pub fn bytes(&self) -> impl Iterator<Item = u8> + '_ {
|
||||||
self.iter().flat_map(|part| part.as_slice().iter().copied())
|
self.iter().flat_map(|part| part.as_slice().iter().copied())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue