rustpython_vm/stdlib/
typing.rs

1pub(crate) use _typing::make_module;
2
3#[pymodule]
4pub(crate) mod _typing {
5    use crate::{
6        builtins::{pystr::AsPyStr, PyGenericAlias, PyTupleRef, PyTypeRef},
7        function::IntoFuncArgs,
8        PyObjectRef, PyPayload, PyResult, VirtualMachine,
9    };
10
11    pub(crate) fn _call_typing_func_object<'a>(
12        _vm: &VirtualMachine,
13        _func_name: impl AsPyStr<'a>,
14        _args: impl IntoFuncArgs,
15    ) -> PyResult {
16        todo!("does this work????");
17        // let module = vm.import("typing", 0)?;
18        // let module = vm.import("_pycodecs", None, 0)?;
19        // let func = module.get_attr(func_name, vm)?;
20        // func.call(args, vm)
21    }
22
23    #[pyattr]
24    pub(crate) fn _idfunc(_vm: &VirtualMachine) {}
25
26    #[pyattr]
27    #[pyclass(name = "TypeVar")]
28    #[derive(Debug, PyPayload)]
29    #[allow(dead_code)]
30    pub(crate) struct TypeVar {
31        name: PyObjectRef, // TODO PyStrRef?
32        bound: parking_lot::Mutex<PyObjectRef>,
33        evaluate_bound: PyObjectRef,
34        constraints: parking_lot::Mutex<PyObjectRef>,
35        evaluate_constraints: PyObjectRef,
36        covariant: bool,
37        contravariant: bool,
38        infer_variance: bool,
39    }
40    #[pyclass(flags(BASETYPE))]
41    impl TypeVar {
42        pub(crate) fn _bound(&self, vm: &VirtualMachine) -> PyResult {
43            let mut bound = self.bound.lock();
44            if !vm.is_none(&bound) {
45                return Ok(bound.clone());
46            }
47            if !vm.is_none(&self.evaluate_bound) {
48                *bound = self.evaluate_bound.call((), vm)?;
49                Ok(bound.clone())
50            } else {
51                Ok(vm.ctx.none())
52            }
53        }
54    }
55
56    pub(crate) fn make_typevar(
57        vm: &VirtualMachine,
58        name: PyObjectRef,
59        evaluate_bound: PyObjectRef,
60        evaluate_constraints: PyObjectRef,
61    ) -> TypeVar {
62        TypeVar {
63            name,
64            bound: parking_lot::Mutex::new(vm.ctx.none()),
65            evaluate_bound,
66            constraints: parking_lot::Mutex::new(vm.ctx.none()),
67            evaluate_constraints,
68            covariant: false,
69            contravariant: false,
70            infer_variance: true,
71        }
72    }
73
74    #[pyattr]
75    #[pyclass(name = "ParamSpec")]
76    #[derive(Debug, PyPayload)]
77    #[allow(dead_code)]
78    struct ParamSpec {}
79    #[pyclass(flags(BASETYPE))]
80    impl ParamSpec {}
81
82    #[pyattr]
83    #[pyclass(name = "TypeVarTuple")]
84    #[derive(Debug, PyPayload)]
85    #[allow(dead_code)]
86    pub(crate) struct TypeVarTuple {}
87    #[pyclass(flags(BASETYPE))]
88    impl TypeVarTuple {}
89
90    #[pyattr]
91    #[pyclass(name = "ParamSpecArgs")]
92    #[derive(Debug, PyPayload)]
93    #[allow(dead_code)]
94    pub(crate) struct ParamSpecArgs {}
95    #[pyclass(flags(BASETYPE))]
96    impl ParamSpecArgs {}
97
98    #[pyattr]
99    #[pyclass(name = "ParamSpecKwargs")]
100    #[derive(Debug, PyPayload)]
101    #[allow(dead_code)]
102    pub(crate) struct ParamSpecKwargs {}
103    #[pyclass(flags(BASETYPE))]
104    impl ParamSpecKwargs {}
105
106    #[pyattr]
107    #[pyclass(name)]
108    #[derive(Debug, PyPayload)]
109    #[allow(dead_code)]
110    pub(crate) struct TypeAliasType {
111        name: PyObjectRef, // TODO PyStrRef?
112        type_params: PyTupleRef,
113        value: PyObjectRef,
114        // compute_value: PyObjectRef,
115        // module: PyObjectRef,
116    }
117    #[pyclass(flags(BASETYPE))]
118    impl TypeAliasType {
119        pub fn new(
120            name: PyObjectRef,
121            type_params: PyTupleRef,
122            value: PyObjectRef,
123        ) -> TypeAliasType {
124            TypeAliasType {
125                name,
126                type_params,
127                value,
128            }
129        }
130    }
131
132    #[pyattr]
133    #[pyclass(name)]
134    #[derive(Debug, PyPayload)]
135    #[allow(dead_code)]
136    pub(crate) struct Generic {}
137
138    // #[pyclass(with(AsMapping), flags(BASETYPE))]
139    #[pyclass(flags(BASETYPE))]
140    impl Generic {
141        #[pyclassmethod(magic)]
142        fn class_getitem(cls: PyTypeRef, args: PyObjectRef, vm: &VirtualMachine) -> PyGenericAlias {
143            PyGenericAlias::new(cls, args, vm)
144        }
145    }
146
147    // impl AsMapping for Generic {
148    //     fn as_mapping() -> &'static PyMappingMethods {
149    //         static AS_MAPPING: Lazy<PyMappingMethods> = Lazy::new(|| PyMappingMethods {
150    //             subscript: atomic_func!(|mapping, needle, vm| {
151    //                 println!("gigity");
152    //                 call_typing_func_object(vm, "_GenericAlias", (mapping.obj, needle))
153    //             }),
154    //             ..PyMappingMethods::NOT_IMPLEMENTED
155    //         });
156    //         &AS_MAPPING
157    //     }
158    // }
159}