Creates a GenericAlias from type parameters, equivalent to _Py_subscript_generic. This is used for PEP 695 classes to create Generic[T] from type parameters. _Py_subscript_generic
(type_params: PyObjectRef, vm: &VirtualMachine)
| 725 | /// This is used for PEP 695 classes to create Generic[T] from type parameters. |
| 726 | // _Py_subscript_generic |
| 727 | pub fn subscript_generic(type_params: PyObjectRef, vm: &VirtualMachine) -> PyResult { |
| 728 | let typing_module = vm.import("typing", 0)?; |
| 729 | let generic_type = typing_module.get_attr("Generic", vm)?; |
| 730 | let generic_alias_class = typing_module.get_attr("_GenericAlias", vm)?; |
| 731 | |
| 732 | let params = if let Ok(tuple) = type_params.try_to_ref::<PyTuple>(vm) { |
| 733 | tuple.to_owned() |
| 734 | } else { |
| 735 | PyTuple::new_ref(vec![type_params], &vm.ctx) |
| 736 | }; |
| 737 | |
| 738 | let args = crate::stdlib::_typing::unpack_typevartuples(¶ms, vm)?; |
| 739 | |
| 740 | generic_alias_class.call((generic_type, args.to_pyobject(vm)), vm) |
| 741 | } |
| 742 | |
| 743 | pub fn init(context: &'static Context) { |
| 744 | PyGenericAlias::extend_class(context, context.types.generic_alias_type); |
no test coverage detected