typevar as module by youknowone · Pull Request #6834 · RustPython/RustPython
impl Constructor for TypeVar {
type Args = FuncArgs;
fn slot_new(cls: PyTypeRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult { let typevar = <Self as Constructor>::py_new(&cls, args, vm)?; let obj = typevar.into_ref_with_type(vm, cls)?; let obj_ref: PyObjectRef = obj.into(); set_module_from_caller(&obj_ref, vm)?; Ok(obj_ref) } fn slot_new(cls: PyTypeRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult { let typevar = <Self as Constructor>::py_new(&cls, args, vm)?; let obj = typevar.into_ref_with_type(vm, cls)?; let obj_ref: PyObjectRef = obj.into(); set_module_from_caller(&obj_ref, vm)?; Ok(obj_ref) }
fn py_new(_cls: &Py<PyType>, args: Self::Args, vm: &VirtualMachine) -> PyResult<Self> { let mut kwargs = args.kwargs; // Parse arguments manually let (name, constraints) = if args.args.is_empty() { // Check if name is provided as keyword argument if let Some(name) = kwargs.swap_remove("name") { (name, vec![]) fn py_new(_cls: &Py<PyType>, args: Self::Args, vm: &VirtualMachine) -> PyResult<Self> { let mut kwargs = args.kwargs; // Parse arguments manually let (name, constraints) = if args.args.is_empty() { // Check if name is provided as keyword argument if let Some(name) = kwargs.swap_remove("name") { (name, vec![]) } else { return Err( vm.new_type_error("TypeVar() missing required argument: 'name' (pos 1)") ); } } else if args.args.len() == 1 { (args.args[0].clone(), vec![]) } else { return Err( vm.new_type_error("TypeVar() missing required argument: 'name' (pos 1)") ); let name = args.args[0].clone(); let constraints = args.args[1..].to_vec(); (name, constraints) };
let bound = kwargs.swap_remove("bound"); let covariant = kwargs .swap_remove("covariant") .map(|v| v.try_to_bool(vm)) .transpose()? .unwrap_or(false); let contravariant = kwargs .swap_remove("contravariant") .map(|v| v.try_to_bool(vm)) .transpose()? .unwrap_or(false); let infer_variance = kwargs .swap_remove("infer_variance") .map(|v| v.try_to_bool(vm)) .transpose()? .unwrap_or(false); let default = kwargs.swap_remove("default");
// Check for unexpected keyword arguments if !kwargs.is_empty() { let unexpected_keys: Vec<String> = kwargs.keys().map(|s| s.to_string()).collect(); return Err(vm.new_type_error(format!( "TypeVar() got unexpected keyword argument(s): {}", unexpected_keys.join(", ") ))); } } else if args.args.len() == 1 { (args.args[0].clone(), vec![]) } else { let name = args.args[0].clone(); let constraints = args.args[1..].to_vec(); (name, constraints) };
let bound = kwargs.swap_remove("bound"); let covariant = kwargs .swap_remove("covariant") .map(|v| v.try_to_bool(vm)) .transpose()? .unwrap_or(false); let contravariant = kwargs .swap_remove("contravariant") .map(|v| v.try_to_bool(vm)) .transpose()? .unwrap_or(false); let infer_variance = kwargs .swap_remove("infer_variance") .map(|v| v.try_to_bool(vm)) .transpose()? .unwrap_or(false); let default = kwargs.swap_remove("default");
// Check for unexpected keyword arguments if !kwargs.is_empty() { let unexpected_keys: Vec<String> = kwargs.keys().map(|s| s.to_string()).collect(); return Err(vm.new_type_error(format!( "TypeVar() got unexpected keyword argument(s): {}", unexpected_keys.join(", ") ))); }
// Check for invalid combinations if covariant && contravariant { return Err(vm.new_value_error("Bivariant type variables are not supported.")); }
if infer_variance && (covariant || contravariant) { return Err(vm.new_value_error("Variance cannot be specified with infer_variance")); }
// Handle constraints and bound let (constraints_obj, evaluate_constraints) = if !constraints.is_empty() { // Check for single constraint if constraints.len() == 1 { return Err(vm.new_type_error("A single constraint is not allowed")); // Check for invalid combinations if covariant && contravariant { return Err(vm.new_value_error("Bivariant type variables are not supported.")); } if bound.is_some() { return Err(vm.new_type_error("Constraints cannot be used with bound")); } let constraints_tuple = vm.ctx.new_tuple(constraints); (constraints_tuple.clone().into(), constraints_tuple.into()) } else { (vm.ctx.none(), vm.ctx.none()) };
// Handle bound let (bound_obj, evaluate_bound) = if let Some(bound) = bound { if vm.is_none(&bound) { (vm.ctx.none(), vm.ctx.none()) } else { // Type check the bound let bound = type_check(bound, "Bound must be a type.", vm)?; (bound, vm.ctx.none()) if infer_variance && (covariant || contravariant) { return Err(vm.new_value_error("Variance cannot be specified with infer_variance")); } } else { (vm.ctx.none(), vm.ctx.none()) };
// Handle default value let (default_value, evaluate_default) = if let Some(default) = default { (default, vm.ctx.none()) } else { // If no default provided, use NoDefault singleton (vm.ctx.typing_no_default.clone().into(), vm.ctx.none()) }; // Handle constraints and bound let (constraints_obj, evaluate_constraints) = if !constraints.is_empty() { // Check for single constraint if constraints.len() == 1 { return Err(vm.new_type_error("A single constraint is not allowed")); } if bound.is_some() { return Err(vm.new_type_error("Constraints cannot be used with bound")); } let constraints_tuple = vm.ctx.new_tuple(constraints); (constraints_tuple.clone().into(), constraints_tuple.into()) } else { (vm.ctx.none(), vm.ctx.none()) };
Ok(Self { name, bound: parking_lot::Mutex::new(bound_obj), evaluate_bound, constraints: parking_lot::Mutex::new(constraints_obj), evaluate_constraints, default_value: parking_lot::Mutex::new(default_value), evaluate_default: PyMutex::new(evaluate_default), covariant, contravariant, infer_variance, }) } } // Handle bound let (bound_obj, evaluate_bound) = if let Some(bound) = bound { if vm.is_none(&bound) { (vm.ctx.none(), vm.ctx.none()) } else { // Type check the bound let bound = type_check(bound, "Bound must be a type.", vm)?; (bound, vm.ctx.none()) } } else { (vm.ctx.none(), vm.ctx.none()) };
impl TypeVar { pub fn new( vm: &VirtualMachine, name: PyObjectRef, evaluate_bound: PyObjectRef, evaluate_constraints: PyObjectRef, ) -> Self { Self { name, bound: parking_lot::Mutex::new(vm.ctx.none()), evaluate_bound, constraints: parking_lot::Mutex::new(vm.ctx.none()), evaluate_constraints, default_value: parking_lot::Mutex::new(vm.ctx.typing_no_default.clone().into()), evaluate_default: PyMutex::new(vm.ctx.none()), covariant: false, contravariant: false, infer_variance: false, // Handle default value let (default_value, evaluate_default) = if let Some(default) = default { (default, vm.ctx.none()) } else { // If no default provided, use NoDefault singleton (vm.ctx.typing_no_default.clone().into(), vm.ctx.none()) };
Ok(Self { name, bound: parking_lot::Mutex::new(bound_obj), evaluate_bound, constraints: parking_lot::Mutex::new(constraints_obj), evaluate_constraints, default_value: parking_lot::Mutex::new(default_value), evaluate_default: PyMutex::new(evaluate_default), covariant, contravariant, infer_variance, }) }
fn slot_new(cls: PyTypeRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult { let typevar = <Self as Constructor>::py_new(&cls, args, vm)?; let obj = typevar.into_ref_with_type(vm, cls)?; let obj_ref: PyObjectRef = obj.into(); set_module_from_caller(&obj_ref, vm)?; Ok(obj_ref) } fn slot_new(cls: PyTypeRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult { let typevar = <Self as Constructor>::py_new(&cls, args, vm)?; let obj = typevar.into_ref_with_type(vm, cls)?; let obj_ref: PyObjectRef = obj.into(); set_module_from_caller(&obj_ref, vm)?; Ok(obj_ref) }
fn py_new(_cls: &Py<PyType>, args: Self::Args, vm: &VirtualMachine) -> PyResult<Self> { let mut kwargs = args.kwargs; // Parse arguments manually let (name, constraints) = if args.args.is_empty() { // Check if name is provided as keyword argument if let Some(name) = kwargs.swap_remove("name") { (name, vec![]) fn py_new(_cls: &Py<PyType>, args: Self::Args, vm: &VirtualMachine) -> PyResult<Self> { let mut kwargs = args.kwargs; // Parse arguments manually let (name, constraints) = if args.args.is_empty() { // Check if name is provided as keyword argument if let Some(name) = kwargs.swap_remove("name") { (name, vec![]) } else { return Err( vm.new_type_error("TypeVar() missing required argument: 'name' (pos 1)") ); } } else if args.args.len() == 1 { (args.args[0].clone(), vec![]) } else { return Err( vm.new_type_error("TypeVar() missing required argument: 'name' (pos 1)") ); let name = args.args[0].clone(); let constraints = args.args[1..].to_vec(); (name, constraints) };
let bound = kwargs.swap_remove("bound"); let covariant = kwargs .swap_remove("covariant") .map(|v| v.try_to_bool(vm)) .transpose()? .unwrap_or(false); let contravariant = kwargs .swap_remove("contravariant") .map(|v| v.try_to_bool(vm)) .transpose()? .unwrap_or(false); let infer_variance = kwargs .swap_remove("infer_variance") .map(|v| v.try_to_bool(vm)) .transpose()? .unwrap_or(false); let default = kwargs.swap_remove("default");
// Check for unexpected keyword arguments if !kwargs.is_empty() { let unexpected_keys: Vec<String> = kwargs.keys().map(|s| s.to_string()).collect(); return Err(vm.new_type_error(format!( "TypeVar() got unexpected keyword argument(s): {}", unexpected_keys.join(", ") ))); } } else if args.args.len() == 1 { (args.args[0].clone(), vec![]) } else { let name = args.args[0].clone(); let constraints = args.args[1..].to_vec(); (name, constraints) };
let bound = kwargs.swap_remove("bound"); let covariant = kwargs .swap_remove("covariant") .map(|v| v.try_to_bool(vm)) .transpose()? .unwrap_or(false); let contravariant = kwargs .swap_remove("contravariant") .map(|v| v.try_to_bool(vm)) .transpose()? .unwrap_or(false); let infer_variance = kwargs .swap_remove("infer_variance") .map(|v| v.try_to_bool(vm)) .transpose()? .unwrap_or(false); let default = kwargs.swap_remove("default");
// Check for unexpected keyword arguments if !kwargs.is_empty() { let unexpected_keys: Vec<String> = kwargs.keys().map(|s| s.to_string()).collect(); return Err(vm.new_type_error(format!( "TypeVar() got unexpected keyword argument(s): {}", unexpected_keys.join(", ") ))); }
// Check for invalid combinations if covariant && contravariant { return Err(vm.new_value_error("Bivariant type variables are not supported.")); }
if infer_variance && (covariant || contravariant) { return Err(vm.new_value_error("Variance cannot be specified with infer_variance")); }
// Handle constraints and bound let (constraints_obj, evaluate_constraints) = if !constraints.is_empty() { // Check for single constraint if constraints.len() == 1 { return Err(vm.new_type_error("A single constraint is not allowed")); // Check for invalid combinations if covariant && contravariant { return Err(vm.new_value_error("Bivariant type variables are not supported.")); } if bound.is_some() { return Err(vm.new_type_error("Constraints cannot be used with bound")); } let constraints_tuple = vm.ctx.new_tuple(constraints); (constraints_tuple.clone().into(), constraints_tuple.into()) } else { (vm.ctx.none(), vm.ctx.none()) };
// Handle bound let (bound_obj, evaluate_bound) = if let Some(bound) = bound { if vm.is_none(&bound) { (vm.ctx.none(), vm.ctx.none()) } else { // Type check the bound let bound = type_check(bound, "Bound must be a type.", vm)?; (bound, vm.ctx.none()) if infer_variance && (covariant || contravariant) { return Err(vm.new_value_error("Variance cannot be specified with infer_variance")); } } else { (vm.ctx.none(), vm.ctx.none()) };
// Handle default value let (default_value, evaluate_default) = if let Some(default) = default { (default, vm.ctx.none()) } else { // If no default provided, use NoDefault singleton (vm.ctx.typing_no_default.clone().into(), vm.ctx.none()) }; // Handle constraints and bound let (constraints_obj, evaluate_constraints) = if !constraints.is_empty() { // Check for single constraint if constraints.len() == 1 { return Err(vm.new_type_error("A single constraint is not allowed")); } if bound.is_some() { return Err(vm.new_type_error("Constraints cannot be used with bound")); } let constraints_tuple = vm.ctx.new_tuple(constraints); (constraints_tuple.clone().into(), constraints_tuple.into()) } else { (vm.ctx.none(), vm.ctx.none()) };
Ok(Self { name, bound: parking_lot::Mutex::new(bound_obj), evaluate_bound, constraints: parking_lot::Mutex::new(constraints_obj), evaluate_constraints, default_value: parking_lot::Mutex::new(default_value), evaluate_default: PyMutex::new(evaluate_default), covariant, contravariant, infer_variance, }) } } // Handle bound let (bound_obj, evaluate_bound) = if let Some(bound) = bound { if vm.is_none(&bound) { (vm.ctx.none(), vm.ctx.none()) } else { // Type check the bound let bound = type_check(bound, "Bound must be a type.", vm)?; (bound, vm.ctx.none()) } } else { (vm.ctx.none(), vm.ctx.none()) };
impl TypeVar { pub fn new( vm: &VirtualMachine, name: PyObjectRef, evaluate_bound: PyObjectRef, evaluate_constraints: PyObjectRef, ) -> Self { Self { name, bound: parking_lot::Mutex::new(vm.ctx.none()), evaluate_bound, constraints: parking_lot::Mutex::new(vm.ctx.none()), evaluate_constraints, default_value: parking_lot::Mutex::new(vm.ctx.typing_no_default.clone().into()), evaluate_default: PyMutex::new(vm.ctx.none()), covariant: false, contravariant: false, infer_variance: false, // Handle default value let (default_value, evaluate_default) = if let Some(default) = default { (default, vm.ctx.none()) } else { // If no default provided, use NoDefault singleton (vm.ctx.typing_no_default.clone().into(), vm.ctx.none()) };
Ok(Self { name, bound: parking_lot::Mutex::new(bound_obj), evaluate_bound, constraints: parking_lot::Mutex::new(constraints_obj), evaluate_constraints, default_value: parking_lot::Mutex::new(default_value), evaluate_default: PyMutex::new(evaluate_default), covariant, contravariant, infer_variance, }) }