Skip to content

Commit 78601f3

Browse files
committed
fix(PyStrRef): fix TODO in typing.rs where PyObjectRef was used
1 parent fcf1969 commit 78601f3

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

vm/src/frame.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2453,6 +2453,9 @@ impl ExecutingFrame<'_> {
24532453
.map_err(|_| vm.new_type_error("Type params must be a tuple."))?
24542454
};
24552455

2456+
let name = name.downcast::<crate::builtins::PyStr>().map_err(|_| {
2457+
vm.new_type_error("TypeAliasType name must be a string".to_owned())
2458+
})?;
24562459
let type_alias = typing::TypeAliasType::new(name, type_params, value);
24572460
Ok(type_alias.into_ref(&vm.ctx).into())
24582461
}

vm/src/stdlib/typing.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub(crate) fn make_module(vm: &VirtualMachine) -> PyRef<PyModule> {
3131
pub(crate) mod decl {
3232
use crate::{
3333
Py, PyObjectRef, PyPayload, PyResult, VirtualMachine,
34-
builtins::{PyTupleRef, PyTypeRef, pystr::AsPyStr},
34+
builtins::{PyStrRef, PyTupleRef, PyTypeRef, pystr::AsPyStr},
3535
function::{FuncArgs, IntoFuncArgs},
3636
types::{Constructor, Representable},
3737
};
@@ -98,15 +98,15 @@ pub(crate) mod decl {
9898
#[derive(Debug, PyPayload)]
9999
#[allow(dead_code)]
100100
pub(crate) struct TypeAliasType {
101-
name: PyObjectRef, // TODO PyStrRef?
101+
name: PyStrRef,
102102
type_params: PyTupleRef,
103103
value: PyObjectRef,
104104
// compute_value: PyObjectRef,
105105
// module: PyObjectRef,
106106
}
107107
#[pyclass(with(Constructor, Representable), flags(BASETYPE))]
108108
impl TypeAliasType {
109-
pub const fn new(name: PyObjectRef, type_params: PyTupleRef, value: PyObjectRef) -> Self {
109+
pub const fn new(name: PyStrRef, type_params: PyTupleRef, value: PyObjectRef) -> Self {
110110
Self {
111111
name,
112112
type_params,
@@ -116,7 +116,7 @@ pub(crate) mod decl {
116116

117117
#[pygetset]
118118
fn __name__(&self) -> PyObjectRef {
119-
self.name.clone()
119+
self.name.clone().into()
120120
}
121121

122122
#[pygetset]
@@ -154,7 +154,9 @@ pub(crate) mod decl {
154154
)));
155155
}
156156

157-
let name = args.args[0].clone();
157+
let name = args.args[0].clone().downcast::<crate::builtins::PyStr>().map_err(|_| {
158+
vm.new_type_error("TypeAliasType name must be a string".to_owned())
159+
})?;
158160
let value = args.args[1].clone();
159161

160162
let type_params = if let Some(tp) = args.kwargs.get("type_params") {
@@ -171,9 +173,8 @@ pub(crate) mod decl {
171173
}
172174

173175
impl Representable for TypeAliasType {
174-
fn repr_str(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<String> {
175-
let name = zelf.name.str(vm)?;
176-
Ok(name.as_str().to_owned())
176+
fn repr_str(zelf: &Py<Self>, _vm: &VirtualMachine) -> PyResult<String> {
177+
Ok(zelf.name.as_str().to_owned())
177178
}
178179
}
179180

0 commit comments

Comments
 (0)