Implement mapping trait for GenericAlias#3374
Conversation
| for idx in 0..tuple.len() { | ||
| if tuple.fast_getitem(idx).is(item) { | ||
| return Some(idx); | ||
| } | ||
| } |
There was a problem hiding this comment.
PyTuple is immutable so it doesn't have a lock.
| for idx in 0..tuple.len() { | |
| if tuple.fast_getitem(idx).is(item) { | |
| return Some(idx); | |
| } | |
| } | |
| params.as_slice().position(|p| p.is(arg)) |
There was a problem hiding this comment.
Ah.. I agree with that. Thanks!
| @@ -1,10 +1,12 @@ | |||
| use crate::{ | |||
| builtins::tuple::IntoPyTuple, | |||
There was a problem hiding this comment.
This is not intended to be used as an API. I'd better to hide this better than now. vm.ctx.new_tuple or PyTuple::new_ref will suggest more consistent way like other types.
| } | ||
| } | ||
|
|
||
| let newargs = newargs.into_pytuple(vm); |
There was a problem hiding this comment.
| let newargs = newargs.into_pytuple(vm); | |
| let newargs = newargs.into(); |
Into is enough to convert from PyRef to PyObjectRef
0cf8b3a to
9870276
Compare
| if let Ok(sub_params) = PyTupleRef::try_from_object(vm, sub_params) { | ||
| let sub_args = sub_params | ||
| .as_slice() | ||
| .iter() | ||
| .map(|arg| { | ||
| if let Some(idx) = tuple_index(params, arg) { | ||
| argitems[idx].clone() | ||
| } else { | ||
| arg.clone() | ||
| } | ||
| }) | ||
| .collect::<Vec<_>>(); | ||
| let sub_args: PyObjectRef = PyTuple::new_ref(sub_args, &vm.ctx).into(); | ||
| Some(obj.get_item(sub_args, vm)) | ||
| } else { | ||
| None | ||
| } |
There was a problem hiding this comment.
| if let Ok(sub_params) = PyTupleRef::try_from_object(vm, sub_params) { | |
| let sub_args = sub_params | |
| .as_slice() | |
| .iter() | |
| .map(|arg| { | |
| if let Some(idx) = tuple_index(params, arg) { | |
| argitems[idx].clone() | |
| } else { | |
| arg.clone() | |
| } | |
| }) | |
| .collect::<Vec<_>>(); | |
| let sub_args: PyObjectRef = PyTuple::new_ref(sub_args, &vm.ctx).into(); | |
| Some(obj.get_item(sub_args, vm)) | |
| } else { | |
| None | |
| } | |
| PyTupleRef::try_from_object(vm, sub_params).ok().map(|sub_params| { | |
| let sub_args = sub_params | |
| .as_slice() | |
| .iter() | |
| .map(|arg| { | |
| if let Some(idx) = tuple_index(params, arg) { | |
| argitems[idx].clone() | |
| } else { | |
| arg.clone() | |
| } | |
| }) | |
| .collect::<Vec<_>>(); | |
| let sub_args: PyObjectRef = PyTuple::new_ref(sub_args, &vm.ctx).into(); | |
| Some(obj.get_item(sub_args, vm)) | |
| }) |
| let mut new_args: Vec<PyObjectRef> = Vec::with_capacity(zelf.args.len()); | ||
| for arg in zelf.args.as_slice().iter() { | ||
| if is_typevar(arg) { | ||
| let idx = tuple_index(&zelf.parameters, arg).unwrap(); | ||
| new_args.push(arg_items[idx].clone()); | ||
| } else { | ||
| new_args.push(subs_tvars(arg.clone(), &zelf.parameters, arg_items, vm)?); | ||
| } | ||
| } |
There was a problem hiding this comment.
| let mut new_args: Vec<PyObjectRef> = Vec::with_capacity(zelf.args.len()); | |
| for arg in zelf.args.as_slice().iter() { | |
| if is_typevar(arg) { | |
| let idx = tuple_index(&zelf.parameters, arg).unwrap(); | |
| new_args.push(arg_items[idx].clone()); | |
| } else { | |
| new_args.push(subs_tvars(arg.clone(), &zelf.parameters, arg_items, vm)?); | |
| } | |
| } | |
| let new_args = zelf.args.as_slice().iter().map(|arg| { | |
| if is_typevar(arg) { | |
| let idx = tuple_index(&zelf.parameters, arg).unwrap(); | |
| Ok(arg_items[idx].clone()) | |
| } else { | |
| subs_tvars(arg.clone(), &zelf.parameters, arg_items, vm) | |
| } | |
| }).collect::<Result<Vec<_>, _>>()?; |
with_capacity is inferred through iterator
There was a problem hiding this comment.
wow.. collect::<Result<Vec<_>, _>> seems great. I didn't know that Vec<Result> can be converted like that
9870276 to
4705b04
Compare
Signed-off-by: snowapril <sinjihng@gmail.com>
Signed-off-by: snowapril <sinjihng@gmail.com>
Signed-off-by: snowapril <sinjihng@gmail.com>
4705b04 to
795bfeb
Compare
|
I add extra test that didn't caught by unittest from typing import TypeVar
Y = TypeVar('Y')
assert dict[str,Y][int] == dict[str, int] |
|
Thanks! |
This revision implements
mappingtrait forGenericAliasAlthough this is not included in #3244, actually
GenericAliasprovide mapping protocol interface.(https://github.com/python/cpython/blob/main/Objects/genericaliasobject.c#L376)
Reference