[red-knot] Minor simplifications to types/display.rs (#17813)

This commit is contained in:
Alex Waygood 2025-05-03 15:29:05 +01:00 committed by GitHub
parent 097af060c9
commit 91481a8be7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -76,7 +76,7 @@ impl Display for DisplayRepresentation<'_> {
(_, Some(KnownClass::NoneType)) => f.write_str("None"), (_, Some(KnownClass::NoneType)) => f.write_str("None"),
(_, Some(KnownClass::NoDefaultType)) => f.write_str("NoDefault"), (_, Some(KnownClass::NoDefaultType)) => f.write_str("NoDefault"),
(ClassType::NonGeneric(class), _) => f.write_str(class.name(self.db)), (ClassType::NonGeneric(class), _) => f.write_str(class.name(self.db)),
(ClassType::Generic(alias), _) => write!(f, "{}", alias.display(self.db)), (ClassType::Generic(alias), _) => alias.display(self.db).fmt(f),
} }
} }
Type::ProtocolInstance(protocol) => match protocol.inner() { Type::ProtocolInstance(protocol) => match protocol.inner() {
@ -104,16 +104,14 @@ impl Display for DisplayRepresentation<'_> {
} }
// TODO functions and classes should display using a fully qualified name // TODO functions and classes should display using a fully qualified name
Type::ClassLiteral(class) => f.write_str(class.name(self.db)), Type::ClassLiteral(class) => f.write_str(class.name(self.db)),
Type::GenericAlias(generic) => { Type::GenericAlias(generic) => generic.display(self.db).fmt(f),
write!(f, "{}", generic.display(self.db))
}
Type::SubclassOf(subclass_of_ty) => match subclass_of_ty.subclass_of() { Type::SubclassOf(subclass_of_ty) => match subclass_of_ty.subclass_of() {
// Only show the bare class name here; ClassBase::display would render this as // Only show the bare class name here; ClassBase::display would render this as
// type[<class 'Foo'>] instead of type[Foo]. // type[<class 'Foo'>] instead of type[Foo].
SubclassOfInner::Class(class) => write!(f, "type[{}]", class.name(self.db)), SubclassOfInner::Class(class) => write!(f, "type[{}]", class.name(self.db)),
SubclassOfInner::Dynamic(dynamic) => write!(f, "type[{dynamic}]"), SubclassOfInner::Dynamic(dynamic) => write!(f, "type[{dynamic}]"),
}, },
Type::KnownInstance(known_instance) => write!(f, "{}", known_instance.repr(self.db)), Type::KnownInstance(known_instance) => known_instance.repr(self.db).fmt(f),
Type::FunctionLiteral(function) => { Type::FunctionLiteral(function) => {
let signature = function.signature(self.db); let signature = function.signature(self.db);
@ -263,9 +261,7 @@ impl Display for DisplayRepresentation<'_> {
} }
f.write_str("]") f.write_str("]")
} }
Type::TypeVar(typevar) => { Type::TypeVar(typevar) => f.write_str(typevar.name(self.db)),
write!(f, "{}", typevar.name(self.db))
}
Type::AlwaysTruthy => f.write_str("AlwaysTruthy"), Type::AlwaysTruthy => f.write_str("AlwaysTruthy"),
Type::AlwaysFalsy => f.write_str("AlwaysFalsy"), Type::AlwaysFalsy => f.write_str("AlwaysFalsy"),
Type::BoundSuper(bound_super) => { Type::BoundSuper(bound_super) => {
@ -328,7 +324,7 @@ impl Display for DisplayGenericContext<'_> {
if idx > 0 { if idx > 0 {
f.write_str(", ")?; f.write_str(", ")?;
} }
write!(f, "{}", var.name(self.db))?; f.write_str(var.name(self.db))?;
match var.bound_or_constraints(self.db) { match var.bound_or_constraints(self.db) {
Some(TypeVarBoundOrConstraints::UpperBound(bound)) => { Some(TypeVarBoundOrConstraints::UpperBound(bound)) => {
write!(f, ": {}", bound.display(self.db))?; write!(f, ": {}", bound.display(self.db))?;
@ -339,7 +335,7 @@ impl Display for DisplayGenericContext<'_> {
if idx > 0 { if idx > 0 {
f.write_str(", ")?; f.write_str(", ")?;
} }
write!(f, "{}", constraint.display(self.db))?; constraint.display(self.db).fmt(f)?;
} }
f.write_char(')')?; f.write_char(')')?;
} }
@ -399,7 +395,7 @@ impl Display for DisplaySpecialization<'_> {
if idx > 0 { if idx > 0 {
f.write_str(", ")?; f.write_str(", ")?;
} }
write!(f, "{}", ty.display(self.db))?; ty.display(self.db).fmt(f)?;
} }
f.write_char(']') f.write_char(']')
} }
@ -423,7 +419,7 @@ pub(crate) struct DisplayCallableType<'db> {
impl Display for DisplayCallableType<'_> { impl Display for DisplayCallableType<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self.signatures { match self.signatures {
[signature] => write!(f, "{}", signature.display(self.db)), [signature] => signature.display(self.db).fmt(f),
signatures => { signatures => {
// TODO: How to display overloads? // TODO: How to display overloads?
f.write_str("Overload[")?; f.write_str("Overload[")?;
@ -490,9 +486,7 @@ impl Display for DisplaySignature<'_> {
f, f,
") -> {}", ") -> {}",
self.return_ty.unwrap_or(Type::unknown()).display(self.db) self.return_ty.unwrap_or(Type::unknown()).display(self.db)
)?; )
Ok(())
} }
} }
@ -510,7 +504,7 @@ struct DisplayParameter<'db> {
impl Display for DisplayParameter<'_> { impl Display for DisplayParameter<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
if let Some(name) = self.param.display_name() { if let Some(name) = self.param.display_name() {
write!(f, "{name}")?; f.write_str(&name)?;
if let Some(annotated_type) = self.param.annotated_type() { if let Some(annotated_type) = self.param.annotated_type() {
write!(f, ": {}", annotated_type.display(self.db))?; write!(f, ": {}", annotated_type.display(self.db))?;
} }
@ -767,9 +761,9 @@ impl Display for DisplayStringLiteralType<'_> {
match ch { match ch {
// `escape_debug` will escape even single quotes, which is not necessary for our // `escape_debug` will escape even single quotes, which is not necessary for our
// use case as we are already using double quotes to wrap the string. // use case as we are already using double quotes to wrap the string.
'\'' => f.write_char('\'')?, '\'' => f.write_char('\''),
_ => write!(f, "{}", ch.escape_debug())?, _ => ch.escape_debug().fmt(f),
} }?;
} }
f.write_char('"') f.write_char('"')
} }