Skip to content

Commit 91481a8

Browse files
authored
[red-knot] Minor simplifications to types/display.rs (astral-sh#17813)
1 parent 097af06 commit 91481a8

File tree

1 file changed

+13
-19
lines changed
  • crates/red_knot_python_semantic/src/types

1 file changed

+13
-19
lines changed

crates/red_knot_python_semantic/src/types/display.rs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl Display for DisplayRepresentation<'_> {
7676
(_, Some(KnownClass::NoneType)) => f.write_str("None"),
7777
(_, Some(KnownClass::NoDefaultType)) => f.write_str("NoDefault"),
7878
(ClassType::NonGeneric(class), _) => f.write_str(class.name(self.db)),
79-
(ClassType::Generic(alias), _) => write!(f, "{}", alias.display(self.db)),
79+
(ClassType::Generic(alias), _) => alias.display(self.db).fmt(f),
8080
}
8181
}
8282
Type::ProtocolInstance(protocol) => match protocol.inner() {
@@ -104,16 +104,14 @@ impl Display for DisplayRepresentation<'_> {
104104
}
105105
// TODO functions and classes should display using a fully qualified name
106106
Type::ClassLiteral(class) => f.write_str(class.name(self.db)),
107-
Type::GenericAlias(generic) => {
108-
write!(f, "{}", generic.display(self.db))
109-
}
107+
Type::GenericAlias(generic) => generic.display(self.db).fmt(f),
110108
Type::SubclassOf(subclass_of_ty) => match subclass_of_ty.subclass_of() {
111109
// Only show the bare class name here; ClassBase::display would render this as
112110
// type[<class 'Foo'>] instead of type[Foo].
113111
SubclassOfInner::Class(class) => write!(f, "type[{}]", class.name(self.db)),
114112
SubclassOfInner::Dynamic(dynamic) => write!(f, "type[{dynamic}]"),
115113
},
116-
Type::KnownInstance(known_instance) => write!(f, "{}", known_instance.repr(self.db)),
114+
Type::KnownInstance(known_instance) => known_instance.repr(self.db).fmt(f),
117115
Type::FunctionLiteral(function) => {
118116
let signature = function.signature(self.db);
119117

@@ -263,9 +261,7 @@ impl Display for DisplayRepresentation<'_> {
263261
}
264262
f.write_str("]")
265263
}
266-
Type::TypeVar(typevar) => {
267-
write!(f, "{}", typevar.name(self.db))
268-
}
264+
Type::TypeVar(typevar) => f.write_str(typevar.name(self.db)),
269265
Type::AlwaysTruthy => f.write_str("AlwaysTruthy"),
270266
Type::AlwaysFalsy => f.write_str("AlwaysFalsy"),
271267
Type::BoundSuper(bound_super) => {
@@ -328,7 +324,7 @@ impl Display for DisplayGenericContext<'_> {
328324
if idx > 0 {
329325
f.write_str(", ")?;
330326
}
331-
write!(f, "{}", var.name(self.db))?;
327+
f.write_str(var.name(self.db))?;
332328
match var.bound_or_constraints(self.db) {
333329
Some(TypeVarBoundOrConstraints::UpperBound(bound)) => {
334330
write!(f, ": {}", bound.display(self.db))?;
@@ -339,7 +335,7 @@ impl Display for DisplayGenericContext<'_> {
339335
if idx > 0 {
340336
f.write_str(", ")?;
341337
}
342-
write!(f, "{}", constraint.display(self.db))?;
338+
constraint.display(self.db).fmt(f)?;
343339
}
344340
f.write_char(')')?;
345341
}
@@ -399,7 +395,7 @@ impl Display for DisplaySpecialization<'_> {
399395
if idx > 0 {
400396
f.write_str(", ")?;
401397
}
402-
write!(f, "{}", ty.display(self.db))?;
398+
ty.display(self.db).fmt(f)?;
403399
}
404400
f.write_char(']')
405401
}
@@ -423,7 +419,7 @@ pub(crate) struct DisplayCallableType<'db> {
423419
impl Display for DisplayCallableType<'_> {
424420
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
425421
match self.signatures {
426-
[signature] => write!(f, "{}", signature.display(self.db)),
422+
[signature] => signature.display(self.db).fmt(f),
427423
signatures => {
428424
// TODO: How to display overloads?
429425
f.write_str("Overload[")?;
@@ -490,9 +486,7 @@ impl Display for DisplaySignature<'_> {
490486
f,
491487
") -> {}",
492488
self.return_ty.unwrap_or(Type::unknown()).display(self.db)
493-
)?;
494-
495-
Ok(())
489+
)
496490
}
497491
}
498492

@@ -510,7 +504,7 @@ struct DisplayParameter<'db> {
510504
impl Display for DisplayParameter<'_> {
511505
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
512506
if let Some(name) = self.param.display_name() {
513-
write!(f, "{name}")?;
507+
f.write_str(&name)?;
514508
if let Some(annotated_type) = self.param.annotated_type() {
515509
write!(f, ": {}", annotated_type.display(self.db))?;
516510
}
@@ -767,9 +761,9 @@ impl Display for DisplayStringLiteralType<'_> {
767761
match ch {
768762
// `escape_debug` will escape even single quotes, which is not necessary for our
769763
// use case as we are already using double quotes to wrap the string.
770-
'\'' => f.write_char('\'')?,
771-
_ => write!(f, "{}", ch.escape_debug())?,
772-
}
764+
'\'' => f.write_char('\''),
765+
_ => ch.escape_debug().fmt(f),
766+
}?;
773767
}
774768
f.write_char('"')
775769
}

0 commit comments

Comments
 (0)