Skip to content

Commit 49422c5

Browse files
authored
relocate Generic under typevar.rs (#5889)
1 parent d40cbbb commit 49422c5

File tree

2 files changed

+50
-50
lines changed

2 files changed

+50
-50
lines changed

vm/src/stdlib/typevar.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,3 +948,50 @@ impl Comparable for ParamSpecKwargs {
948948
}
949949
}
950950
}
951+
952+
/// Helper function to call typing module functions with cls as first argument
953+
/// Similar to CPython's call_typing_args_kwargs
954+
fn call_typing_args_kwargs(
955+
name: &'static str,
956+
cls: PyTypeRef,
957+
args: FuncArgs,
958+
vm: &VirtualMachine,
959+
) -> PyResult {
960+
let typing = vm.import("typing", 0)?;
961+
let func = typing.get_attr(name, vm)?;
962+
963+
// Prepare arguments: (cls, *args)
964+
let mut call_args = vec![cls.into()];
965+
call_args.extend(args.args);
966+
967+
// Call with prepared args and original kwargs
968+
let func_args = FuncArgs {
969+
args: call_args,
970+
kwargs: args.kwargs,
971+
};
972+
973+
func.call(func_args, vm)
974+
}
975+
976+
#[pyclass(name = "Generic", module = "typing")]
977+
#[derive(Debug, PyPayload)]
978+
#[allow(dead_code)]
979+
pub struct Generic {}
980+
981+
#[pyclass(flags(BASETYPE))]
982+
impl Generic {
983+
#[pyclassmethod]
984+
fn __class_getitem__(cls: PyTypeRef, args: PyObjectRef, vm: &VirtualMachine) -> PyResult {
985+
// Convert single arg to FuncArgs
986+
let func_args = FuncArgs {
987+
args: vec![args],
988+
kwargs: Default::default(),
989+
};
990+
call_typing_args_kwargs("_generic_class_getitem", cls, func_args, vm)
991+
}
992+
993+
#[pyclassmethod]
994+
fn __init_subclass__(cls: PyTypeRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
995+
call_typing_args_kwargs("_generic_init_subclass", cls, args, vm)
996+
}
997+
}

vm/src/stdlib/typing.rs

Lines changed: 3 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use crate::{PyPayload, PyRef, VirtualMachine, class::PyClassImpl, stdlib::PyModule};
33

44
pub use crate::stdlib::typevar::{
5-
ParamSpec, ParamSpecArgs, ParamSpecKwargs, TypeVar, TypeVarTuple,
5+
Generic, ParamSpec, ParamSpecArgs, ParamSpecKwargs, TypeVar, TypeVarTuple,
66
};
77
pub use decl::*;
88

@@ -13,13 +13,15 @@ pub(crate) fn make_module(vm: &VirtualMachine) -> PyRef<PyModule> {
1313
TypeVarTuple::make_class(&vm.ctx);
1414
ParamSpecArgs::make_class(&vm.ctx);
1515
ParamSpecKwargs::make_class(&vm.ctx);
16+
Generic::make_class(&vm.ctx);
1617
extend_module!(vm, &module, {
1718
"NoDefault" => vm.ctx.typing_no_default.clone(),
1819
"TypeVar" => TypeVar::class(&vm.ctx).to_owned(),
1920
"ParamSpec" => ParamSpec::class(&vm.ctx).to_owned(),
2021
"TypeVarTuple" => TypeVarTuple::class(&vm.ctx).to_owned(),
2122
"ParamSpecArgs" => ParamSpecArgs::class(&vm.ctx).to_owned(),
2223
"ParamSpecKwargs" => ParamSpecKwargs::class(&vm.ctx).to_owned(),
24+
"Generic" => Generic::class(&vm.ctx).to_owned(),
2325
});
2426
module
2527
}
@@ -116,55 +118,6 @@ pub(crate) mod decl {
116118
}
117119
}
118120

119-
/// Helper function to call typing module functions with cls as first argument
120-
/// Similar to CPython's call_typing_args_kwargs
121-
fn call_typing_args_kwargs(
122-
name: &'static str,
123-
cls: PyTypeRef,
124-
args: FuncArgs,
125-
vm: &VirtualMachine,
126-
) -> PyResult {
127-
let typing = vm.import("typing", 0)?;
128-
let func = typing.get_attr(name, vm)?;
129-
130-
// Prepare arguments: (cls, *args)
131-
let mut call_args = vec![cls.into()];
132-
call_args.extend(args.args);
133-
134-
// Call with prepared args and original kwargs
135-
let func_args = FuncArgs {
136-
args: call_args,
137-
kwargs: args.kwargs,
138-
};
139-
140-
func.call(func_args, vm)
141-
}
142-
143-
#[pyattr]
144-
#[pyclass(name = "Generic", module = "typing")]
145-
#[derive(Debug, PyPayload)]
146-
#[allow(dead_code)]
147-
pub(crate) struct Generic {}
148-
149-
// #[pyclass(with(AsMapping), flags(BASETYPE))]
150-
#[pyclass(flags(BASETYPE))]
151-
impl Generic {
152-
#[pyclassmethod]
153-
fn __class_getitem__(cls: PyTypeRef, args: PyObjectRef, vm: &VirtualMachine) -> PyResult {
154-
// Convert single arg to FuncArgs
155-
let func_args = FuncArgs {
156-
args: vec![args],
157-
kwargs: Default::default(),
158-
};
159-
call_typing_args_kwargs("_generic_class_getitem", cls, func_args, vm)
160-
}
161-
162-
#[pyclassmethod]
163-
fn __init_subclass__(cls: PyTypeRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
164-
call_typing_args_kwargs("_generic_init_subclass", cls, args, vm)
165-
}
166-
}
167-
168121
// impl AsMapping for Generic {
169122
// fn as_mapping() -> &'static PyMappingMethods {
170123
// static AS_MAPPING: Lazy<PyMappingMethods> = Lazy::new(|| PyMappingMethods {

0 commit comments

Comments
 (0)