Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Constructor for TypeAliasType
  • Loading branch information
youknowone committed Jul 20, 2025
commit e326a011cb858a09ca26421f85596338825ac8bb
42 changes: 41 additions & 1 deletion vm/src/stdlib/typing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub(crate) mod decl {
// compute_value: PyObjectRef,
// module: PyObjectRef,
}
#[pyclass(flags(BASETYPE))]
#[pyclass(with(Constructor), flags(BASETYPE))]
impl TypeAliasType {
pub const fn new(name: PyObjectRef, type_params: PyTupleRef, value: PyObjectRef) -> Self {
Self {
Expand Down Expand Up @@ -136,6 +136,46 @@ pub(crate) mod decl {
}
}

impl Constructor for TypeAliasType {
type Args = FuncArgs;

fn py_new(cls: PyTypeRef, args: Self::Args, vm: &VirtualMachine) -> PyResult {
// TypeAliasType(name, value, *, type_params=None)
if args.args.len() < 2 {
return Err(vm.new_type_error(format!(
"TypeAliasType() missing {} required positional argument{}: {}",
2 - args.args.len(),
if 2 - args.args.len() == 1 { "" } else { "s" },
if args.args.is_empty() {
"'name' and 'value'"
} else {
"'value'"
}
)));
}
if args.args.len() > 2 {
return Err(vm.new_type_error(format!(
"TypeAliasType() takes 2 positional arguments but {} were given",
args.args.len()
)));
}

let name = args.args[0].clone();
let value = args.args[1].clone();

let type_params = if let Some(tp) = args.kwargs.get("type_params") {
tp.clone()
.downcast::<crate::builtins::PyTuple>()
.map_err(|_| vm.new_type_error("type_params must be a tuple".to_owned()))?
} else {
vm.ctx.empty_tuple.clone()
};

let ta = TypeAliasType::new(name, type_params, value);
ta.into_ref_with_type(vm, cls).map(Into::into)
}
}

// impl AsMapping for Generic {
// fn as_mapping() -> &'static PyMappingMethods {
// static AS_MAPPING: Lazy<PyMappingMethods> = Lazy::new(|| PyMappingMethods {
Expand Down