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

Code before:
/// Returns `Some(value_type)` if the type is or contains a `nonmember[T]`, otherwise `None`.
pub(crate) fn try_unwrap_nonmember_value<'db>(db: &'db dyn Db, ty: Type<'db>) -> Option<Type<'db>> {
    match ty {
        Type::NominalInstance(instance) if instance.has_known_class(db, KnownClass::Nonmember) => {
            Some(
                ty.member(db, "value")
                    .place
                    .ignore_possibly_undefined()
                    .unwrap_or(Type::unknown()),
            )
        }
        Type::Union(union) => {
            // If the union contains a nonmember, extract its value.
            // Filter out other elements (like Unknown from declarations).
            for elem in union.elements(db) {
                if let Type::NominalInstance(instance) = elem {
                    if instance.has_known_class(db, KnownClass::Nonmember) {
                        return Some(
                            elem.member(db, "value")
                                .place
                                .ignore_possibly_undefined()
                                .unwrap_or(Type::unknown()),
                        );
                    }
                }
            }
            None
        }
        _ => None,
    }
}


Review comment (suggestion):
As commented above, I'm wondering if we should even have this branch at all, given I don't think the nonmember handling in `enum_metadata` handles unions. If we do need it, I think it should be more principled than just arbitrarily grabbing the first nonmember type it sees (more like: union all nonmember types found -- and decide what we should do with mixed types, which is really not clear).

Code after:
        Type::NominalInstance(instance) if instance.has_known_class(db, KnownClass::Nonmember) => {
            Some(
                ty.member(db, "value")
                    .place
                    .ignore_possibly_undefined()
                    .unwrap_or(Type::unknown()),
            )
        }
        Type::Union(union) => {
            // TODO: This is a hack. The proper fix is to avoid unioning Unknown from
            // declarations into Place when we have concrete bindings.
            //
            // For now, we filter out Unknown and expect exactly one nonmember type
            // to remain. If there are other non-Unknown types mixed in, we bail out.
            let mut non_unknown = union.elements(db).iter().filter(|elem| !elem.is_unknown());

            let first = non_unknown.next()?;

            // Ensure there's exactly one non-Unknown element.
            if non_unknown.next().is_some() {
                return None;
            }

            if let Type::NominalInstance(instance) = first {
                if instance.has_known_class(db, KnownClass::Nonmember) {
                    return Some(
                        first
                            .member(db, "value")
                            .place
                            .ignore_possibly_undefined()
                            .unwrap_or(Type::unknown()),
                    );
                }
            }
            None
        }
        _ => None,
    }
}