From 78601f30d12cc3892214d31c606b016defcb9a93 Mon Sep 17 00:00:00 2001 From: winlogon Date: Tue, 21 Oct 2025 01:37:40 +0200 Subject: [PATCH 1/2] fix(PyStrRef): fix TODO in typing.rs where PyObjectRef was used --- vm/src/frame.rs | 3 +++ vm/src/stdlib/typing.rs | 17 +++++++++-------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/vm/src/frame.rs b/vm/src/frame.rs index fa5860244f1..89d19f15c8a 100644 --- a/vm/src/frame.rs +++ b/vm/src/frame.rs @@ -2453,6 +2453,9 @@ impl ExecutingFrame<'_> { .map_err(|_| vm.new_type_error("Type params must be a tuple."))? }; + let name = name.downcast::().map_err(|_| { + vm.new_type_error("TypeAliasType name must be a string".to_owned()) + })?; let type_alias = typing::TypeAliasType::new(name, type_params, value); Ok(type_alias.into_ref(&vm.ctx).into()) } diff --git a/vm/src/stdlib/typing.rs b/vm/src/stdlib/typing.rs index 2c4517fc7b6..276afccfe9a 100644 --- a/vm/src/stdlib/typing.rs +++ b/vm/src/stdlib/typing.rs @@ -31,7 +31,7 @@ pub(crate) fn make_module(vm: &VirtualMachine) -> PyRef { pub(crate) mod decl { use crate::{ Py, PyObjectRef, PyPayload, PyResult, VirtualMachine, - builtins::{PyTupleRef, PyTypeRef, pystr::AsPyStr}, + builtins::{PyStrRef, PyTupleRef, PyTypeRef, pystr::AsPyStr}, function::{FuncArgs, IntoFuncArgs}, types::{Constructor, Representable}, }; @@ -98,7 +98,7 @@ pub(crate) mod decl { #[derive(Debug, PyPayload)] #[allow(dead_code)] pub(crate) struct TypeAliasType { - name: PyObjectRef, // TODO PyStrRef? + name: PyStrRef, type_params: PyTupleRef, value: PyObjectRef, // compute_value: PyObjectRef, @@ -106,7 +106,7 @@ pub(crate) mod decl { } #[pyclass(with(Constructor, Representable), flags(BASETYPE))] impl TypeAliasType { - pub const fn new(name: PyObjectRef, type_params: PyTupleRef, value: PyObjectRef) -> Self { + pub const fn new(name: PyStrRef, type_params: PyTupleRef, value: PyObjectRef) -> Self { Self { name, type_params, @@ -116,7 +116,7 @@ pub(crate) mod decl { #[pygetset] fn __name__(&self) -> PyObjectRef { - self.name.clone() + self.name.clone().into() } #[pygetset] @@ -154,7 +154,9 @@ pub(crate) mod decl { ))); } - let name = args.args[0].clone(); + let name = args.args[0].clone().downcast::().map_err(|_| { + vm.new_type_error("TypeAliasType name must be a string".to_owned()) + })?; let value = args.args[1].clone(); let type_params = if let Some(tp) = args.kwargs.get("type_params") { @@ -171,9 +173,8 @@ pub(crate) mod decl { } impl Representable for TypeAliasType { - fn repr_str(zelf: &Py, vm: &VirtualMachine) -> PyResult { - let name = zelf.name.str(vm)?; - Ok(name.as_str().to_owned()) + fn repr_str(zelf: &Py, _vm: &VirtualMachine) -> PyResult { + Ok(zelf.name.as_str().to_owned()) } } From 1e6dbc65fd76d04356823ec208c575752b54b4da Mon Sep 17 00:00:00 2001 From: winlogon Date: Tue, 21 Oct 2025 03:29:15 +0200 Subject: [PATCH 2/2] chore(fmt): apply rustfmt to code --- vm/src/stdlib/typing.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/vm/src/stdlib/typing.rs b/vm/src/stdlib/typing.rs index 276afccfe9a..c014266935c 100644 --- a/vm/src/stdlib/typing.rs +++ b/vm/src/stdlib/typing.rs @@ -154,9 +154,10 @@ pub(crate) mod decl { ))); } - let name = args.args[0].clone().downcast::().map_err(|_| { - vm.new_type_error("TypeAliasType name must be a string".to_owned()) - })?; + let name = args.args[0] + .clone() + .downcast::() + .map_err(|_| vm.new_type_error("TypeAliasType name must be a string".to_owned()))?; let value = args.args[1].clone(); let type_params = if let Some(tp) = args.kwargs.get("type_params") {