@@ -237,81 +237,68 @@ fn subs_tvars(
237237 . unwrap_or ( Ok ( obj) )
238238}
239239
240- impl AsMapping for PyGenericAlias {
241- fn as_mapping ( _zelf : & PyObjectView < Self > , _vm : & VirtualMachine ) -> PyMappingMethods {
242- PyMappingMethods {
243- length : None ,
244- subscript : Some ( Self :: subscript) ,
245- ass_subscript : None ,
240+ impl PyGenericAlias {
241+ fn getitem ( & self , needle : & PyObject , vm : & VirtualMachine ) -> PyResult {
242+ let num_params = self . parameters . len ( ) ;
243+ if num_params == 0 {
244+ return Err ( vm. new_type_error ( format ! (
245+ "There are no type variables left in {}" ,
246+ self . repr( vm) ?
247+ ) ) ) ;
246248 }
247- }
248-
249- #[ cold]
250- fn length ( zelf : PyObjectRef , _vm : & VirtualMachine ) -> PyResult < usize > {
251- unreachable ! ( "length not implemented for {}" , zelf. class( ) )
252- }
253249
254- fn subscript ( zelf : PyObjectRef , needle : PyObjectRef , vm : & VirtualMachine ) -> PyResult {
255- Self :: downcast ( zelf, vm) . map ( |zelf| {
256- let num_params = zelf. parameters . len ( ) ;
257- if num_params == 0 {
258- return Err ( vm. new_type_error ( format ! (
259- "There are no type variables left in {}" ,
260- zelf. repr( vm) ?
261- ) ) ) ;
262- }
250+ let items = PyTupleRef :: try_from_object ( vm, needle. clone ( ) ) ;
251+ let arg_items = match items {
252+ Ok ( ref tuple) => tuple. as_slice ( ) ,
253+ Err ( _) => std:: slice:: from_ref ( & needle) ,
254+ } ;
263255
264- let items = PyTupleRef :: try_from_object ( vm, needle. clone ( ) ) ;
265- let arg_items = match items {
266- Ok ( ref tuple) => tuple. as_slice ( ) ,
267- Err ( _) => std:: slice:: from_ref ( & needle) ,
256+ let num_items = arg_items. len ( ) ;
257+ if num_params != num_items {
258+ let plural = if num_items > num_params {
259+ "many"
260+ } else {
261+ "few"
268262 } ;
263+ return Err ( vm. new_type_error ( format ! (
264+ "Too {} arguments for {}" ,
265+ plural,
266+ self . repr( vm) ?
267+ ) ) ) ;
268+ }
269269
270- let num_items = arg_items. len ( ) ;
271- if num_params != num_items {
272- let plural = if num_items > num_params {
273- "many"
270+ let new_args = self
271+ . args
272+ . as_slice ( )
273+ . iter ( )
274+ . map ( |arg| {
275+ if is_typevar ( arg) {
276+ let idx = tuple_index ( & self . parameters , arg) . unwrap ( ) ;
277+ Ok ( arg_items[ idx] . clone ( ) )
274278 } else {
275- "few"
276- } ;
277- return Err ( vm. new_type_error ( format ! (
278- "Too {} arguments for {}" ,
279- plural,
280- zelf. repr( vm) ?
281- ) ) ) ;
282- }
279+ subs_tvars ( arg. clone ( ) , & self . parameters , arg_items, vm)
280+ }
281+ } )
282+ . collect :: < PyResult < Vec < _ > > > ( ) ?;
283283
284- let new_args = zelf
285- . args
286- . as_slice ( )
287- . iter ( )
288- . map ( |arg| {
289- if is_typevar ( arg) {
290- let idx = tuple_index ( & zelf. parameters , arg) . unwrap ( ) ;
291- Ok ( arg_items[ idx] . clone ( ) )
292- } else {
293- subs_tvars ( arg. clone ( ) , & zelf. parameters , arg_items, vm)
294- }
295- } )
296- . collect :: < PyResult < Vec < _ > > > ( ) ?;
297-
298- Ok ( PyGenericAlias :: new (
299- zelf. origin . clone ( ) ,
300- PyTuple :: new_ref ( new_args, & vm. ctx ) . into ( ) ,
301- vm,
302- )
303- . into_object ( vm) )
304- } ) ?
284+ Ok ( PyGenericAlias :: new (
285+ self . origin . clone ( ) ,
286+ PyTuple :: new_ref ( new_args, & vm. ctx ) . into ( ) ,
287+ vm,
288+ )
289+ . into_object ( vm) )
305290 }
306291
307- #[ cold]
308- fn ass_subscript (
309- zelf : PyObjectRef ,
310- _needle : PyObjectRef ,
311- _value : Option < PyObjectRef > ,
312- _vm : & VirtualMachine ,
313- ) -> PyResult < ( ) > {
314- unreachable ! ( "ass_subscript not implemented for {}" , zelf. class( ) )
292+ const MAPPING_METHODS : PyMappingMethods = PyMappingMethods {
293+ length : None ,
294+ subscript : Some ( |mapping, needle, vm| mapping. obj_as :: < Self > ( ) . getitem ( needle, vm) ) ,
295+ ass_subscript : None ,
296+ } ;
297+ }
298+
299+ impl AsMapping for PyGenericAlias {
300+ fn as_mapping ( _zelf : & PyObjectView < Self > , _vm : & VirtualMachine ) -> PyMappingMethods {
301+ Self :: MAPPING_METHODS
315302 }
316303}
317304
0 commit comments