Fix instancecheck, subclasscheck of UnionType

This commit is contained in:
Hyunmin Shin
2022-07-30 03:36:21 +09:00
parent dfc9b6abf4
commit ee3a241884
2 changed files with 25 additions and 7 deletions

View File

@@ -90,15 +90,33 @@ impl PyUnion {
}
#[pymethod(magic)]
fn instancecheck(_zelf: PyRef<Self>, _obj: PyObjectRef, vm: &VirtualMachine) -> PyResult {
Err(vm
.new_type_error("isinstance() argument 2 cannot be a parameterized generic".to_owned()))
fn instancecheck(zelf: PyRef<Self>, obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<bool> {
if zelf
.args
.iter()
.any(|x| x.class().is(vm.ctx.types.generic_alias_type))
{
Err(vm.new_type_error(
"isinstance() argument 2 cannot be a parameterized generic".to_owned(),
))
} else {
obj.is_instance(zelf.args().as_object(), vm)
}
}
#[pymethod(magic)]
fn subclasscheck(_zelf: PyRef<Self>, _obj: PyObjectRef, vm: &VirtualMachine) -> PyResult {
Err(vm
.new_type_error("issubclass() argument 2 cannot be a parameterized generic".to_owned()))
fn subclasscheck(zelf: PyRef<Self>, obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<bool> {
if zelf
.args
.iter()
.any(|x| x.class().is(vm.ctx.types.generic_alias_type))
{
Err(vm.new_type_error(
"issubclass() argument 2 cannot be a parameterized generic".to_owned(),
))
} else {
obj.is_subclass(zelf.args().as_object(), vm)
}
}
#[pymethod(name = "__ror__")]

View File

@@ -398,7 +398,7 @@ impl PyObject {
})
.and(self.check_cls(cls, vm, || {
format!(
"issubclass() arg 2 must be a class or tuple of classes, not {}",
"issubclass() arg 2 must be a class, a tuple of classes, or a union, not {}",
cls.class()
)
}))