Skip to content

Commit ee3a241

Browse files
committed
Fix instancecheck, subclasscheck of UnionType
1 parent dfc9b6a commit ee3a241

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

vm/src/builtins/union.rs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,33 @@ impl PyUnion {
9090
}
9191

9292
#[pymethod(magic)]
93-
fn instancecheck(_zelf: PyRef<Self>, _obj: PyObjectRef, vm: &VirtualMachine) -> PyResult {
94-
Err(vm
95-
.new_type_error("isinstance() argument 2 cannot be a parameterized generic".to_owned()))
93+
fn instancecheck(zelf: PyRef<Self>, obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<bool> {
94+
if zelf
95+
.args
96+
.iter()
97+
.any(|x| x.class().is(vm.ctx.types.generic_alias_type))
98+
{
99+
Err(vm.new_type_error(
100+
"isinstance() argument 2 cannot be a parameterized generic".to_owned(),
101+
))
102+
} else {
103+
obj.is_instance(zelf.args().as_object(), vm)
104+
}
96105
}
97106

98107
#[pymethod(magic)]
99-
fn subclasscheck(_zelf: PyRef<Self>, _obj: PyObjectRef, vm: &VirtualMachine) -> PyResult {
100-
Err(vm
101-
.new_type_error("issubclass() argument 2 cannot be a parameterized generic".to_owned()))
108+
fn subclasscheck(zelf: PyRef<Self>, obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<bool> {
109+
if zelf
110+
.args
111+
.iter()
112+
.any(|x| x.class().is(vm.ctx.types.generic_alias_type))
113+
{
114+
Err(vm.new_type_error(
115+
"issubclass() argument 2 cannot be a parameterized generic".to_owned(),
116+
))
117+
} else {
118+
obj.is_subclass(zelf.args().as_object(), vm)
119+
}
102120
}
103121

104122
#[pymethod(name = "__ror__")]

vm/src/protocol/object.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ impl PyObject {
398398
})
399399
.and(self.check_cls(cls, vm, || {
400400
format!(
401-
"issubclass() arg 2 must be a class or tuple of classes, not {}",
401+
"issubclass() arg 2 must be a class, a tuple of classes, or a union, not {}",
402402
cls.class()
403403
)
404404
}))

0 commit comments

Comments
 (0)