@@ -63,7 +63,12 @@ impl Constructor for PyGenericAlias {
6363 return Err ( vm. new_type_error ( "GenericAlias() takes no keyword arguments" ) ) ;
6464 }
6565 let ( origin, arguments) : ( _ , PyObjectRef ) = args. bind ( vm) ?;
66- PyGenericAlias :: new ( origin, arguments, vm)
66+ let args = if let Ok ( tuple) = arguments. try_to_ref :: < PyTuple > ( vm) {
67+ tuple. to_owned ( )
68+ } else {
69+ PyTuple :: new_ref ( vec ! [ arguments] , & vm. ctx )
70+ } ;
71+ PyGenericAlias :: new ( origin, args, false , vm)
6772 . into_ref_with_type ( vm, cls)
6873 . map ( Into :: into)
6974 }
@@ -84,35 +89,24 @@ impl Constructor for PyGenericAlias {
8489 flags( BASETYPE )
8590) ]
8691impl PyGenericAlias {
87- pub fn new ( origin : PyTypeRef , args : PyObjectRef , vm : & VirtualMachine ) -> Self {
88- let args = if let Ok ( tuple) = args. try_to_ref :: < PyTuple > ( vm) {
89- tuple. to_owned ( )
90- } else {
91- PyTuple :: new_ref ( vec ! [ args] , & vm. ctx )
92- } ;
93-
92+ pub fn new ( origin : PyTypeRef , args : PyTupleRef , starred : bool , vm : & VirtualMachine ) -> Self {
9493 let parameters = make_parameters ( & args, vm) ;
9594 Self {
9695 origin,
9796 args,
9897 parameters,
99- starred : false , // default to false
98+ starred,
10099 }
101100 }
102101
103- fn with_tuple_args (
104- origin : PyTypeRef ,
105- args : PyTupleRef ,
106- starred : bool ,
107- vm : & VirtualMachine ,
108- ) -> Self {
109- let parameters = make_parameters ( & args, vm) ;
110- Self {
111- origin,
112- args,
113- parameters,
114- starred,
115- }
102+ /// Create a GenericAlias from an origin and PyObjectRef arguments (helper for compatibility)
103+ pub fn from_args ( origin : PyTypeRef , args : PyObjectRef , vm : & VirtualMachine ) -> Self {
104+ let args = if let Ok ( tuple) = args. try_to_ref :: < PyTuple > ( vm) {
105+ tuple. to_owned ( )
106+ } else {
107+ PyTuple :: new_ref ( vec ! [ args] , & vm. ctx )
108+ } ;
109+ Self :: new ( origin, args, false , vm)
116110 }
117111
118112 fn repr ( & self , vm : & VirtualMachine ) -> PyResult < String > {
@@ -146,7 +140,7 @@ impl PyGenericAlias {
146140 }
147141 }
148142
149- Ok ( format ! (
143+ let repr_str = format ! (
150144 "{}[{}]" ,
151145 repr_item( self . origin. clone( ) . into( ) , vm) ?,
152146 if self . args. is_empty( ) {
@@ -158,7 +152,14 @@ impl PyGenericAlias {
158152 . collect:: <PyResult <Vec <_>>>( ) ?
159153 . join( ", " )
160154 }
161- ) )
155+ ) ;
156+
157+ // Add * prefix if this is a starred GenericAlias
158+ Ok ( if self . starred {
159+ format ! ( "*{repr_str}" )
160+ } else {
161+ repr_str
162+ } )
162163 }
163164
164165 #[ pygetset]
@@ -200,10 +201,7 @@ impl PyGenericAlias {
200201 vm,
201202 ) ?;
202203
203- Ok (
204- PyGenericAlias :: new ( zelf. origin . clone ( ) , new_args. to_pyobject ( vm) , vm)
205- . into_pyobject ( vm) ,
206- )
204+ Ok ( PyGenericAlias :: new ( zelf. origin . clone ( ) , new_args, false , vm) . into_pyobject ( vm) )
207205 }
208206
209207 #[ pymethod]
@@ -598,7 +596,7 @@ impl Iterable for PyGenericAlias {
598596 // CPython's ga_iter creates an iterator that yields one starred GenericAlias
599597 // we don't have gaiterobject yet
600598
601- let starred_alias = PyGenericAlias :: with_tuple_args (
599+ let starred_alias = PyGenericAlias :: new (
602600 zelf. origin . clone ( ) ,
603601 zelf. args . clone ( ) ,
604602 true , // starred
0 commit comments