diff --git a/vm/src/builtins/genericalias.rs b/vm/src/builtins/genericalias.rs index dbdf5e0b27..a65b8bede0 100644 --- a/vm/src/builtins/genericalias.rs +++ b/vm/src/builtins/genericalias.rs @@ -2,9 +2,9 @@ use crate::{ builtins::{PyList, PyStr, PyStrRef, PyTuple, PyTupleRef, PyType, PyTypeRef}, common::hash, function::{FuncArgs, IntoPyObject}, - types::{Callable, Constructor, GetAttr, Hashable}, - IdProtocol, PyClassImpl, PyContext, PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, - TypeProtocol, VirtualMachine, + types::{Callable, Comparable, Constructor, GetAttr, Hashable, PyComparisonOp}, + IdProtocol, PyClassImpl, PyComparisonValue, PyContext, PyObject, PyObjectRef, PyRef, PyResult, + PyValue, TryFromObject, TypeProtocol, VirtualMachine, }; use std::fmt; @@ -52,7 +52,10 @@ impl Constructor for PyGenericAlias { } } -#[pyimpl(with(Callable, Constructor, GetAttr, Hashable), flags(BASETYPE))] +#[pyimpl( + with(Callable, Comparable, Constructor, GetAttr, Hashable), + flags(BASETYPE) +)] impl PyGenericAlias { pub fn new(origin: PyTypeRef, args: PyObjectRef, vm: &VirtualMachine) -> Self { let args: PyTupleRef = if let Ok(tuple) = PyTupleRef::try_from_object(vm, args.clone()) { @@ -205,6 +208,30 @@ impl Callable for PyGenericAlias { } } +impl Comparable for PyGenericAlias { + fn cmp( + zelf: &crate::PyObjectView, + other: &PyObject, + op: PyComparisonOp, + vm: &VirtualMachine, + ) -> PyResult { + op.eq_only(|| { + let other = class_or_notimplemented!(Self, other); + Ok(PyComparisonValue::Implemented( + if !zelf + .origin() + .rich_compare_bool(&other.origin(), PyComparisonOp::Eq, vm)? + { + false + } else { + zelf.args() + .rich_compare_bool(&other.args(), PyComparisonOp::Eq, vm)? + }, + )) + }) + } +} + impl Hashable for PyGenericAlias { #[inline] fn hash(zelf: &crate::PyObjectView, vm: &VirtualMachine) -> PyResult {