MCPcopy Index your code
hub / github.com/RustPython/RustPython / subscript_generic

Function subscript_generic

crates/vm/src/builtins/genericalias.rs:727–741  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

725/// This is used for PEP 695 classes to create Generic[T] from type parameters.
726// _Py_subscript_generic
727pub 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(&params, vm)?;
739
740 generic_alias_class.call((generic_type, args.to_pyobject(vm)), vm)
741}
742
743pub fn init(context: &'static Context) {
744 PyGenericAlias::extend_class(context, context.types.generic_alias_type);

Callers 1

call_intrinsic_1Method · 0.85

Calls 6

unpack_typevartuplesFunction · 0.85
importMethod · 0.45
get_attrMethod · 0.45
to_ownedMethod · 0.45
callMethod · 0.45
to_pyobjectMethod · 0.45

Tested by

no test coverage detected