Skip to content

Commit f30ec34

Browse files
committed
cleanup | implementation
Signed-off-by: Ashwin Naren <arihant2math@gmail.com>
1 parent 1b3ac3f commit f30ec34

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

vm/src/builtins/genericalias.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::sync::LazyLock;
22

33
use super::type_;
4+
use crate::builtins::union_::UNION_OR;
45
use crate::{
56
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject,
67
VirtualMachine, atomic_func,
@@ -337,11 +338,7 @@ impl AsMapping for PyGenericAlias {
337338

338339
impl AsNumber for PyGenericAlias {
339340
fn as_number() -> &'static PyNumberMethods {
340-
static AS_NUMBER: PyNumberMethods = PyNumberMethods {
341-
or: Some(|a, b, vm| Ok(PyGenericAlias::or(a.to_owned(), b.to_owned(), vm))),
342-
..PyNumberMethods::NOT_IMPLEMENTED
343-
};
344-
&AS_NUMBER
341+
&UNION_OR
345342
}
346343
}
347344

vm/src/builtins/union.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use super::{genericalias, type_};
2+
use crate::class::StaticType;
23
use crate::{
34
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
45
atomic_func,
56
builtins::{PyFrozenSet, PyStr, PyTuple, PyTupleRef, PyType},
67
class::PyClassImpl,
78
common::hash,
8-
convert::{ToPyObject, ToPyResult},
9+
convert::ToPyObject,
910
function::PyComparisonValue,
1011
protocol::{PyMappingMethods, PyNumberMethods},
1112
types::{AsMapping, AsNumber, Comparable, GetAttr, Hashable, PyComparisonOp, Representable},
@@ -135,6 +136,9 @@ pub fn is_unionable(obj: PyObjectRef, vm: &VirtualMachine) -> bool {
135136
obj.class().is(vm.ctx.types.none_type)
136137
|| obj.payload_if_subclass::<PyType>(vm).is_some()
137138
|| obj.class().is(vm.ctx.types.generic_alias_type)
139+
|| obj
140+
.class()
141+
.is(crate::stdlib::typing::_typing::TypeAliasType::static_type())
138142
|| obj.class().is(vm.ctx.types.union_type)
139143
}
140144

@@ -229,13 +233,14 @@ impl AsMapping for PyUnion {
229233
}
230234
}
231235

236+
pub static UNION_OR: PyNumberMethods = PyNumberMethods {
237+
or: Some(|a, b, vm| Ok(type_::or_(a.to_pyobject(vm), b.to_pyobject(vm), vm))),
238+
..PyNumberMethods::NOT_IMPLEMENTED
239+
};
240+
232241
impl AsNumber for PyUnion {
233242
fn as_number() -> &'static PyNumberMethods {
234-
static AS_NUMBER: PyNumberMethods = PyNumberMethods {
235-
or: Some(|a, b, vm| PyUnion::or(a.to_owned(), b.to_owned(), vm).to_pyresult(vm)),
236-
..PyNumberMethods::NOT_IMPLEMENTED
237-
};
238-
&AS_NUMBER
243+
&UNION_OR
239244
}
240245
}
241246

vm/src/stdlib/typing.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ pub(crate) use _typing::make_module;
22

33
#[pymodule]
44
pub(crate) mod _typing {
5+
use crate::builtins::union_::UNION_OR;
6+
use crate::protocol::PyNumberMethods;
7+
use crate::types::AsNumber;
58
use crate::{
69
PyObjectRef, PyPayload, PyResult, VirtualMachine,
710
builtins::{PyGenericAlias, PyTupleRef, PyTypeRef, pystr::AsPyStr},
811
function::IntoFuncArgs,
912
};
13+
use crate::builtins::type_;
1014

1115
pub(crate) fn _call_typing_func_object<'a>(
1216
_vm: &VirtualMachine,
@@ -37,6 +41,7 @@ pub(crate) mod _typing {
3741
contravariant: bool,
3842
infer_variance: bool,
3943
}
44+
4045
#[pyclass(flags(BASETYPE))]
4146
impl TypeVar {
4247
pub(crate) fn _bound(&self, vm: &VirtualMachine) -> PyResult {
@@ -223,7 +228,14 @@ pub(crate) mod _typing {
223228
// compute_value: PyObjectRef,
224229
// module: PyObjectRef,
225230
}
226-
#[pyclass(flags(BASETYPE))]
231+
232+
impl AsNumber for TypeAliasType {
233+
fn as_number() -> &'static PyNumberMethods {
234+
&UNION_OR
235+
}
236+
}
237+
238+
#[pyclass(flags(BASETYPE), with(AsNumber))]
227239
impl TypeAliasType {
228240
pub fn new(
229241
name: PyObjectRef,
@@ -236,6 +248,16 @@ pub(crate) mod _typing {
236248
value,
237249
}
238250
}
251+
252+
#[pymethod(magic)]
253+
fn ror(zelf: PyObjectRef, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
254+
type_::or_(other, zelf, vm)
255+
}
256+
257+
#[pymethod(magic)]
258+
fn or(zelf: PyObjectRef, other: PyObjectRef, vm: &VirtualMachine) -> PyObjectRef {
259+
type_::or_(zelf, other, vm)
260+
}
239261
}
240262

241263
#[pyattr]

0 commit comments

Comments
 (0)