Skip to content

Commit b15e537

Browse files
authored
Use PyStrRef for TypeAliasType name (RustPython#6203)
* fix(PyStrRef): fix TODO in typing.rs where PyObjectRef was used * chore(fmt): apply rustfmt to code
1 parent 2faa05d commit b15e537

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-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: 10 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,10 @@ pub(crate) mod decl {
154154
)));
155155
}
156156

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

160163
let type_params = if let Some(tp) = args.kwargs.get("type_params") {
@@ -171,9 +174,8 @@ pub(crate) mod decl {
171174
}
172175

173176
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())
177+
fn repr_str(zelf: &Py<Self>, _vm: &VirtualMachine) -> PyResult<String> {
178+
Ok(zelf.name.as_str().to_owned())
177179
}
178180
}
179181

0 commit comments

Comments
 (0)