mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 05:44:56 +00:00
Remove condensed display type enum (#17902)
## Summary See: https://github.com/astral-sh/ruff/pull/17889#discussion_r2076556002
This commit is contained in:
parent
a2e9a7732a
commit
443f62e98d
1 changed files with 23 additions and 44 deletions
|
@ -15,7 +15,6 @@ use crate::types::{
|
||||||
UnionType, WrapperDescriptorKind,
|
UnionType, WrapperDescriptorKind,
|
||||||
};
|
};
|
||||||
use crate::{Db, FxOrderSet};
|
use crate::{Db, FxOrderSet};
|
||||||
use rustc_hash::FxHashMap;
|
|
||||||
|
|
||||||
impl<'db> Type<'db> {
|
impl<'db> Type<'db> {
|
||||||
pub fn display(&self, db: &'db dyn Db) -> DisplayType {
|
pub fn display(&self, db: &'db dyn Db) -> DisplayType {
|
||||||
|
@ -537,31 +536,35 @@ struct DisplayUnionType<'db> {
|
||||||
|
|
||||||
impl Display for DisplayUnionType<'_> {
|
impl Display for DisplayUnionType<'_> {
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||||
|
fn is_condensable(ty: Type<'_>) -> bool {
|
||||||
|
matches!(
|
||||||
|
ty,
|
||||||
|
Type::IntLiteral(_)
|
||||||
|
| Type::StringLiteral(_)
|
||||||
|
| Type::BytesLiteral(_)
|
||||||
|
| Type::BooleanLiteral(_)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
let elements = self.ty.elements(self.db);
|
let elements = self.ty.elements(self.db);
|
||||||
|
|
||||||
// Group condensed-display types by kind.
|
let condensed_types = elements
|
||||||
let mut grouped_condensed_kinds = FxHashMap::default();
|
.iter()
|
||||||
|
.copied()
|
||||||
for element in elements {
|
.filter(|element| is_condensable(*element))
|
||||||
if let Ok(kind) = CondensedDisplayTypeKind::try_from(*element) {
|
.collect::<Vec<_>>();
|
||||||
grouped_condensed_kinds
|
|
||||||
.entry(kind)
|
|
||||||
.or_insert_with(Vec::new)
|
|
||||||
.push(*element);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut join = f.join(" | ");
|
let mut join = f.join(" | ");
|
||||||
|
|
||||||
|
let mut condensed_types = Some(condensed_types);
|
||||||
for element in elements {
|
for element in elements {
|
||||||
if let Ok(kind) = CondensedDisplayTypeKind::try_from(*element) {
|
if is_condensable(*element) {
|
||||||
let Some(condensed_kind) = grouped_condensed_kinds.remove(&kind) else {
|
if let Some(condensed_types) = condensed_types.take() {
|
||||||
continue;
|
|
||||||
};
|
|
||||||
join.entry(&DisplayLiteralGroup {
|
join.entry(&DisplayLiteralGroup {
|
||||||
literals: condensed_kind,
|
literals: condensed_types,
|
||||||
db: self.db,
|
db: self.db,
|
||||||
});
|
});
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
join.entry(&DisplayMaybeParenthesizedType {
|
join.entry(&DisplayMaybeParenthesizedType {
|
||||||
ty: *element,
|
ty: *element,
|
||||||
|
@ -572,8 +575,6 @@ impl Display for DisplayUnionType<'_> {
|
||||||
|
|
||||||
join.finish()?;
|
join.finish()?;
|
||||||
|
|
||||||
debug_assert!(grouped_condensed_kinds.is_empty());
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -599,28 +600,6 @@ impl Display for DisplayLiteralGroup<'_> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Enumeration of literal types that are displayed in a "condensed way" inside `Literal` slices.
|
|
||||||
///
|
|
||||||
/// For example, `Literal[1] | Literal[2] | Literal["s"]` is displayed as `"Literal[1, 2, "s"]"`.
|
|
||||||
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
|
|
||||||
enum CondensedDisplayTypeKind {
|
|
||||||
LiteralExpression,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TryFrom<Type<'_>> for CondensedDisplayTypeKind {
|
|
||||||
type Error = ();
|
|
||||||
|
|
||||||
fn try_from(value: Type<'_>) -> Result<Self, Self::Error> {
|
|
||||||
match value {
|
|
||||||
Type::IntLiteral(_)
|
|
||||||
| Type::StringLiteral(_)
|
|
||||||
| Type::BytesLiteral(_)
|
|
||||||
| Type::BooleanLiteral(_) => Ok(Self::LiteralExpression),
|
|
||||||
_ => Err(()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'db> IntersectionType<'db> {
|
impl<'db> IntersectionType<'db> {
|
||||||
fn display(&'db self, db: &'db dyn Db) -> DisplayIntersectionType<'db> {
|
fn display(&'db self, db: &'db dyn Db) -> DisplayIntersectionType<'db> {
|
||||||
DisplayIntersectionType { db, ty: self }
|
DisplayIntersectionType { db, ty: self }
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue