@@ -30,7 +30,7 @@ pub(crate) fn make_module(vm: &VirtualMachine) -> PyRef<PyModule> {
3030#[ pymodule( name = "_typing" ) ]
3131pub ( crate ) mod decl {
3232 use crate :: {
33- PyObjectRef , PyPayload , PyResult , VirtualMachine ,
33+ Py , PyObjectRef , PyPayload , PyResult , VirtualMachine ,
3434 builtins:: { PyTupleRef , PyTypeRef , pystr:: AsPyStr } ,
3535 function:: { FuncArgs , IntoFuncArgs } ,
3636 types:: { Constructor , Representable } ,
@@ -88,7 +88,7 @@ pub(crate) mod decl {
8888
8989 impl Representable for NoDefault {
9090 #[ inline( always) ]
91- fn repr_str ( _zelf : & crate :: Py < Self > , _vm : & VirtualMachine ) -> PyResult < String > {
91+ fn repr_str ( _zelf : & Py < Self > , _vm : & VirtualMachine ) -> PyResult < String > {
9292 Ok ( "typing.NoDefault" . to_owned ( ) )
9393 }
9494 }
@@ -104,7 +104,7 @@ pub(crate) mod decl {
104104 // compute_value: PyObjectRef,
105105 // module: PyObjectRef,
106106 }
107- #[ pyclass( flags( BASETYPE ) ) ]
107+ #[ pyclass( with ( Constructor , Representable ) , flags( BASETYPE ) ) ]
108108 impl TypeAliasType {
109109 pub const fn new ( name : PyObjectRef , type_params : PyTupleRef , value : PyObjectRef ) -> Self {
110110 Self {
@@ -128,10 +128,51 @@ pub(crate) mod decl {
128128 fn __type_params__ ( & self ) -> PyTupleRef {
129129 self . type_params . clone ( )
130130 }
131+ }
132+
133+ impl Constructor for TypeAliasType {
134+ type Args = FuncArgs ;
135+
136+ fn py_new ( cls : PyTypeRef , args : Self :: Args , vm : & VirtualMachine ) -> PyResult {
137+ // TypeAliasType(name, value, *, type_params=None)
138+ if args. args . len ( ) < 2 {
139+ return Err ( vm. new_type_error ( format ! (
140+ "TypeAliasType() missing {} required positional argument{}: {}" ,
141+ 2 - args. args. len( ) ,
142+ if 2 - args. args. len( ) == 1 { "" } else { "s" } ,
143+ if args. args. is_empty( ) {
144+ "'name' and 'value'"
145+ } else {
146+ "'value'"
147+ }
148+ ) ) ) ;
149+ }
150+ if args. args . len ( ) > 2 {
151+ return Err ( vm. new_type_error ( format ! (
152+ "TypeAliasType() takes 2 positional arguments but {} were given" ,
153+ args. args. len( )
154+ ) ) ) ;
155+ }
156+
157+ let name = args. args [ 0 ] . clone ( ) ;
158+ let value = args. args [ 1 ] . clone ( ) ;
159+
160+ let type_params = if let Some ( tp) = args. kwargs . get ( "type_params" ) {
161+ tp. clone ( )
162+ . downcast :: < crate :: builtins:: PyTuple > ( )
163+ . map_err ( |_| vm. new_type_error ( "type_params must be a tuple" . to_owned ( ) ) ) ?
164+ } else {
165+ vm. ctx . empty_tuple . clone ( )
166+ } ;
167+
168+ let ta = TypeAliasType :: new ( name, type_params, value) ;
169+ ta. into_ref_with_type ( vm, cls) . map ( Into :: into)
170+ }
171+ }
131172
132- # [ pymethod ( name = "__repr__" ) ]
133- fn repr ( & self , vm : & VirtualMachine ) -> PyResult < String > {
134- let name = self . name . str ( vm) ?;
173+ impl Representable for TypeAliasType {
174+ fn repr_str ( zelf : & Py < Self > , vm : & VirtualMachine ) -> PyResult < String > {
175+ let name = zelf . name . str ( vm) ?;
135176 Ok ( name. as_str ( ) . to_owned ( ) )
136177 }
137178 }
0 commit comments