-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Implement mapping trait for GenericAlias
#3374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
vm/src/builtins/genericalias.rs
Outdated
| for idx in 0..tuple.len() { | ||
| if tuple.fast_getitem(idx).is(item) { | ||
| return Some(idx); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah.. I agree with that. Thanks!
vm/src/builtins/genericalias.rs
Outdated
| @@ -1,10 +1,12 @@ | |||
| use crate::{ | |||
| builtins::tuple::IntoPyTuple, | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
vm/src/builtins/genericalias.rs
Outdated
| } | ||
| } | ||
|
|
||
| let newargs = newargs.into_pytuple(vm); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| let newargs = newargs.into_pytuple(vm); | |
| let newargs = newargs.into(); |
Into is enough to convert from PyRef to PyObjectRef
0cf8b3a to
9870276
Compare
vm/src/builtins/genericalias.rs
Outdated
| 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 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)) | |
| }) |
vm/src/builtins/genericalias.rs
Outdated
| 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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