Repository: astral-sh/ruff
File: crates/ty_python_semantic/src/types/display.rs (Rust)

Code before:
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        fn is_condensable(ty: Type<'_>) -> bool {
            matches!(
                ty,
                Type::IntLiteral(_)
                    | Type::StringLiteral(_)
                    | Type::BytesLiteral(_)
                    | Type::BooleanLiteral(_)
                    | Type::EnumLiteral(_)
            )
        }

        let elements = self.ty.elements(self.db);

        let condensed_types = elements
            .iter()
            .copied()
            .filter(|element| is_condensable(*element))
            .collect::<Vec<_>>();

        let total_entries =
            usize::from(!condensed_types.is_empty()) + elements.len() - condensed_types.len();

        if total_entries == 0 {
            return Ok(());
        }

        let mut join = f.join(" | ");

        let display_limit = (if total_entries > MAX_DISPLAYED_UNION_ITEMS {
            MAX_DISPLAYED_UNION_ITEMS_WHEN_ELIDED
        } else {
            MAX_DISPLAYED_UNION_ITEMS
        })
        .min(total_entries);

        let mut condensed_types = Some(condensed_types);
        let mut displayed_entries = 0usize;

        for element in elements {
            if displayed_entries >= display_limit {
                break;
            }

            if is_condensable(*element) {
                if let Some(condensed_types) = condensed_types.take() {
                    displayed_entries += 1;
                    join.entry(&DisplayLiteralGroup {
                        literals: condensed_types,
                        db: self.db,
                        settings: self.settings.singleline(),

Review comment (suggestion):
I'm not sure this is possible? If a union has 0 elements, we should eagerly normalize the union into `Type::Never` when constructing the type

```suggestion
        assert_ne!(total_entries, 0);
```

Code after:
#[derive(Debug, Clone)]
pub(crate) struct SignatureDisplayDetails {
    /// The full signature string
    pub label: String,
    /// Ranges for each parameter within the label
    pub parameter_ranges: Vec<TextRange>,
    /// Names of the parameters in order
    pub parameter_names: Vec<String>,
}

impl<'db> Parameter<'db> {
    fn display_with(
        &'db self,
        db: &'db dyn Db,
        settings: DisplaySettings<'db>,
    ) -> DisplayParameter<'db> {
        DisplayParameter {
            param: self,
            db,
            settings,
        }
    }
}

struct DisplayParameter<'db> {
    param: &'db Parameter<'db>,
    db: &'db dyn Db,
    settings: DisplaySettings<'db>,
}

impl Display for DisplayParameter<'_> {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        if let Some(name) = self.param.display_name() {
            f.write_str(&name)?;
            if let Some(annotated_type) = self.param.annotated_type() {
                if self.param.should_annotation_be_displayed() {
                    write!(
                        f,
                        ": {}",
                        annotated_type.display_with(self.db, self.settings.clone())
                    )?;
                }
            }
            // Default value can only be specified if `name` is given.
            if let Some(default_ty) = self.param.default_type() {
                if self.param.annotated_type().is_some() {
                    write!(
                        f,
                        " = {}",
                        default_ty.display_with(self.db, self.settings.clone())
                    )?;