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

Code before:
                match bound_typevar.typevar(db).bound_or_constraints(db)? {
                    TypeVarBoundOrConstraints::UpperBound(bound) => {
                        self.generate_constraint(db, bound)
                    }
                    TypeVarBoundOrConstraints::Constraints(constraints) => {
                        self.generate_constraint(db, Type::Union(constraints))
                    }
                }
            }

            // It's not valid to use a generic alias as the second argument to `isinstance()` or `issubclass()`,
            // e.g. `isinstance(x, list[int])` fails at runtime.
            Type::GenericAlias(_) => None,

            Type::NominalInstance(nominal) => nominal.tuple_spec(db).and_then(|tuple| {
                UnionType::try_from_elements(
                    db,
                    tuple
                        .all_elements()
                        .map(|element| self.generate_constraint(db, *element)),
                )
            }),

            Type::KnownInstance(KnownInstanceType::UnionType(instance)) => {
                match instance {
                    UnionTypeInstance::Legacy { .. } => None,
                    UnionTypeInstance::PEP604(instance) => {
                        UnionType::try_from_elements(
                            db,
                            instance.value_expr_types(db).iter().map(|element| {
                                // A special case is made for `None` at runtime
                                // (it's implicitly converted to `NoneType` in `int | None`)
                                // which means that `isinstance(x, int | None)` works even though
                                // `None` is not a class literal.
                                if element.is_none(db) {
                                    self.generate_constraint(
                                        db,
                                        KnownClass::NoneType.to_class_literal(db),
                                    )
                                } else {
                                    self.generate_constraint(db, *element)
                                }
                            }),
                        )
                    }
                }
            }

            // We don't have a good meta-type for `Callable`s right now,
            // so only apply `isinstance()` narrowing, not `issubclass()`
            Type::SpecialForm(SpecialFormType::Callable)

Review comment (suggestion):
Will look into this tomorrow, but I guess a legacy `Union[int, str]` is not allowed in `isinstance`/`issubclass`. If so, we were definitely not handling it correctly before.

Code after:
                match bound_typevar.typevar(db).bound_or_constraints(db)? {
                    TypeVarBoundOrConstraints::UpperBound(bound) => {
                        self.generate_constraint(db, bound)
                    }
                    TypeVarBoundOrConstraints::Constraints(constraints) => {
                        self.generate_constraint(db, Type::Union(constraints))
                    }
                }
            }

            // It's not valid to use a generic alias as the second argument to `isinstance()` or `issubclass()`,
            // e.g. `isinstance(x, list[int])` fails at runtime.
            Type::GenericAlias(_) => None,

            Type::NominalInstance(nominal) => nominal.tuple_spec(db).and_then(|tuple| {
                UnionType::try_from_elements(
                    db,
                    tuple
                        .all_elements()
                        .map(|element| self.generate_constraint(db, *element)),
                )
            }),

            Type::KnownInstance(KnownInstanceType::UnionType(instance)) => {
                UnionType::try_from_elements(
                    db,
                    instance.value_expression_types(db).ok()?.map(|element| {
                        // A special case is made for `None` at runtime
                        // (it's implicitly converted to `NoneType` in `int | None`)
                        // which means that `isinstance(x, int | None)` works even though
                        // `None` is not a class literal.
                        if element.is_none(db) {
                            self.generate_constraint(db, KnownClass::NoneType.to_class_literal(db))
                        } else {
                            self.generate_constraint(db, element)
                        }
                    }),
                )
            }

            // We don't have a good meta-type for `Callable`s right now,
            // so only apply `isinstance()` narrowing, not `issubclass()`
            Type::SpecialForm(SpecialFormType::Callable)
                if self == ClassInfoConstraintFunction::IsInstance =>
            {
                Some(CallableType::unknown(db).top_materialization(db))
            }

            Type::SpecialForm(special_form) => special_form
                .aliased_stdlib_class()
                .and_then(|class| self.generate_constraint(db, class.to_class_literal(db))),