Skip to content

Commit 4f2d70f

Browse files
authored
Merge pull request #3475 from hrchu/pep604
Allow writing union types as X | Y
2 parents e740de6 + b0dabd6 commit 4f2d70f

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

vm/src/builtins/pytype.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,15 @@ impl PyType {
381381
fn mro(zelf: PyRef<Self>) -> Vec<PyObjectRef> {
382382
zelf.iter_mro().map(|cls| cls.clone().into()).collect()
383383
}
384+
385+
#[pymethod(name = "__ror__")]
386+
#[pymethod(magic)]
387+
pub fn or(zelf: PyRef<Self>, other: PyObjectRef, vm: &VirtualMachine) -> PyResult {
388+
let union = vm.import("typing", None, 0)?.get_attr("Union", vm)?;
389+
let getitem = vm.get_method(union, "__getitem__").unwrap();
390+
vm.invoke(&getitem?, ((zelf, other),))
391+
}
392+
384393
#[pyslot]
385394
fn slot_new(metatype: PyTypeRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
386395
vm_trace!("type.__new__ {:?}", args);

0 commit comments

Comments
 (0)