Skip to content

Commit f26054a

Browse files
committed
isinstance
1 parent c8e5e89 commit f26054a

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

vm/src/builtins/union.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ impl PyUnion {
4141
}
4242

4343
/// Direct access to args field, matching CPython's _Py_union_args
44-
pub(crate) fn get_args(&self) -> &PyTupleRef {
44+
#[inline]
45+
pub fn args(&self) -> &PyTupleRef {
4546
&self.args
4647
}
4748

vm/src/protocol/object.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ impl PyObject {
513513
let union = cls
514514
.downcast_ref::<crate::builtins::PyUnion>()
515515
.expect("union is already checked");
516-
union.get_args().as_object()
516+
union.args().as_object()
517517
} else {
518518
cls
519519
};
@@ -602,16 +602,14 @@ impl PyObject {
602602

603603
// Check for Union type (e.g., int | str) - CPython checks this before tuple
604604
if cls.class().is(vm.ctx.types.union_type) {
605-
if let Ok(args) = cls.get_attr(identifier!(vm, __args__), vm) {
606-
if let Ok(tuple) = args.try_to_ref::<PyTuple>(vm) {
607-
for typ in tuple {
608-
if vm
609-
.with_recursion("in __instancecheck__", || self.is_instance(typ, vm))?
610-
{
611-
return Ok(true);
612-
}
613-
}
614-
return Ok(false);
605+
// Match CPython's _Py_union_args which directly accesses the args field
606+
let union = cls
607+
.try_to_ref::<crate::builtins::PyUnion>(vm)
608+
.expect("checked by is");
609+
let tuple = union.args();
610+
for typ in tuple.iter() {
611+
if vm.with_recursion("in __instancecheck__", || self.is_instance(typ, vm))? {
612+
return Ok(true);
615613
}
616614
}
617615
}

0 commit comments

Comments
 (0)