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

Code before:
            Type::LiteralValue(literal)
                if literal.as_enum().is_some_and(|enum_literal| {
                    matches!(name_str, "value" | "_value_")
                        && Type::ClassLiteral(enum_literal.enum_class(db))
                            .is_subtype_of(db, KnownClass::Enum.to_subclass_of(db))
                }) =>
            {
                let enum_literal = literal.as_enum().unwrap();
                enum_metadata(db, enum_literal.enum_class(db))
                    .and_then(|metadata| metadata.value_type(enum_literal.name(db)))
                    .map_or_else(|| Place::Undefined, Place::bound)
                    .into()
            }

            Type::TypeVar(typevar) if name_str == "args" && typevar.is_paramspec(db) => {
                Place::declared(Type::TypeVar(
                    typevar.with_paramspec_attr(db, ParamSpecAttrKind::Args),
                ))
                .into()
            }
            Type::TypeVar(typevar) if name_str == "kwargs" && typevar.is_paramspec(db) => {
                Place::declared(Type::TypeVar(
                    typevar.with_paramspec_attr(db, ParamSpecAttrKind::Kwargs),
                ))
                .into()
            }

            Type::NominalInstance(instance)
                if matches!(name_str, "name" | "_name_")
                    && enum_metadata(db, instance.class_literal(db)).is_some() =>
            {
                enum_metadata(db, instance.class_literal(db))
                    .and_then(|metadata| metadata.instance_name_type(db))
                    .map_or_else(Place::default, Place::bound)
                    .into()
            }

            Type::NominalInstance(instance)
                if matches!(name_str, "value" | "_value_")
                    && enum_metadata(db, instance.class_literal(db)).is_some() =>
            {
                enum_metadata(db, instance.class_literal(db))
                    .and_then(|metadata| metadata.instance_value_type(db))
                    .map_or_else(Place::default, Place::bound)
                    .into()
            }

            Type::NominalInstance(..)
            | Type::ProtocolInstance(..)
            | Type::NewTypeInstance(..)
            | Type::LiteralValue(..)

Review comment (suggestion):
There's a subtlety here where classes using `EnumMeta/EnumType` without inheriting `Enum` should not have `.name` or `.value` available.

Code after:
                        "name" if is_enum_subclass => metadata.name_type(db, enum_literal.name(db)),
                        "_name_" => metadata.name_type(db, enum_literal.name(db)),
                        "value" if is_enum_subclass => metadata.value_type(enum_literal.name(db)),
                        "_value_" => metadata.value_type(enum_literal.name(db)),
                        _ => None,
                    })
                    .map_or_else(|| Place::Undefined, Place::bound)
                    .into()
            }

            Type::TypeVar(typevar) if name_str == "args" && typevar.is_paramspec(db) => {
                Place::declared(Type::TypeVar(
                    typevar.with_paramspec_attr(db, ParamSpecAttrKind::Args),
                ))
                .into()
            }
            Type::TypeVar(typevar) if name_str == "kwargs" && typevar.is_paramspec(db) => {
                Place::declared(Type::TypeVar(
                    typevar.with_paramspec_attr(db, ParamSpecAttrKind::Kwargs),
                ))
                .into()
            }

            Type::NominalInstance(instance)
                if matches!(name_str, "name" | "_name_" | "value" | "_value_")
                    && enum_metadata(db, instance.class_literal(db)).is_some() =>
            {
                let class_literal = instance.class_literal(db);
                let is_enum_subclass = Type::ClassLiteral(class_literal)
                    .is_subtype_of(db, KnownClass::Enum.to_subclass_of(db));

                enum_metadata(db, class_literal)
                    .and_then(|metadata| match name_str {
                        "name" if is_enum_subclass => metadata.instance_name_type(db),
                        "_name_" => metadata.instance_name_type(db),
                        "value" if is_enum_subclass => metadata.instance_value_type(db),
                        "_value_" => metadata.instance_value_type(db),
                        _ => None,
                    })
                    .map_or_else(Place::default, Place::bound)
                    .into()
            }

            Type::NominalInstance(..)
            | Type::ProtocolInstance(..)
            | Type::NewTypeInstance(..)
            | Type::LiteralValue(..)
            | Type::TypeVar(..)
            | Type::SpecialForm(..)
            | Type::KnownInstance(..)
            | Type::PropertyInstance(..)