(_cls: &Py<PyType>, args: Self::Args, vm: &VirtualMachine)
| 322 | } |
| 323 | |
| 324 | fn py_new(_cls: &Py<PyType>, args: Self::Args, vm: &VirtualMachine) -> PyResult<Self> { |
| 325 | let mut kwargs = args.kwargs; |
| 326 | // Parse arguments manually |
| 327 | let (name, constraints) = if args.args.is_empty() { |
| 328 | // Check if name is provided as keyword argument |
| 329 | if let Some(name) = kwargs.swap_remove("name") { |
| 330 | (name, vec![]) |
| 331 | } else { |
| 332 | return Err( |
| 333 | vm.new_type_error("TypeVar() missing required argument: 'name' (pos 1)") |
| 334 | ); |
| 335 | } |
| 336 | } else if args.args.len() == 1 { |
| 337 | (args.args[0].clone(), vec![]) |
| 338 | } else { |
| 339 | let name = args.args[0].clone(); |
| 340 | let constraints = args.args[1..].to_vec(); |
| 341 | (name, constraints) |
| 342 | }; |
| 343 | |
| 344 | let bound = kwargs.swap_remove("bound"); |
| 345 | let covariant = kwargs |
| 346 | .swap_remove("covariant") |
| 347 | .map(|v| v.try_to_bool(vm)) |
| 348 | .transpose()? |
| 349 | .unwrap_or(false); |
| 350 | let contravariant = kwargs |
| 351 | .swap_remove("contravariant") |
| 352 | .map(|v| v.try_to_bool(vm)) |
| 353 | .transpose()? |
| 354 | .unwrap_or(false); |
| 355 | let infer_variance = kwargs |
| 356 | .swap_remove("infer_variance") |
| 357 | .map(|v| v.try_to_bool(vm)) |
| 358 | .transpose()? |
| 359 | .unwrap_or(false); |
| 360 | let default = kwargs.swap_remove("default"); |
| 361 | |
| 362 | // Check for unexpected keyword arguments |
| 363 | if !kwargs.is_empty() { |
| 364 | let unexpected_keys: Vec<String> = kwargs.keys().map(|s| s.to_string()).collect(); |
| 365 | return Err(vm.new_type_error(format!( |
| 366 | "TypeVar() got unexpected keyword argument(s): {}", |
| 367 | unexpected_keys.join(", ") |
| 368 | ))); |
| 369 | } |
| 370 | |
| 371 | // Check for invalid combinations |
| 372 | if covariant && contravariant { |
| 373 | return Err(vm.new_value_error("Bivariant type variables are not supported.")); |
| 374 | } |
| 375 | |
| 376 | if infer_variance && (covariant || contravariant) { |
| 377 | return Err(vm.new_value_error("Variance cannot be specified with infer_variance")); |
| 378 | } |
| 379 | |
| 380 | // Handle constraints and bound |
| 381 | let (constraints_obj, evaluate_constraints) = if !constraints.is_empty() { |
nothing calls this directly
no test coverage detected