Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Implementmapping trait forGenericAlias#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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
youknowone merged 3 commits intoRustPython:mainfromSnowapril:ga_mapping_protocol
Oct 26, 2021

Conversation

Snowapril
Copy link
Contributor

@SnowaprilSnowapril commentedOct 25, 2021
edited
Loading

This revision implementsmapping trait forGenericAlias

Although this is not included in#3244, actuallyGenericAlias provide mapping protocol interface.
(https://github.com/python/cpython/blob/main/Objects/genericaliasobject.c#L376)

Reference

Comment on lines 198 to 202
for idx in 0..tuple.len() {
if tuple.fast_getitem(idx).is(item) {
return Some(idx);
}
}
Copy link
Member

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.

Suggested change
for idxin0..tuple.len(){
if tuple.fast_getitem(idx).is(item){
returnSome(idx);
}
}
params.as_slice().position(|p| p.is(arg))

Copy link
ContributorAuthor

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!

@@ -1,10 +1,12 @@
use crate::{
builtins::tuple::IntoPyTuple,
Copy link
Member

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 orPyTuple::new_ref will suggest more consistent way like other types.

Snowapril reacted with thumbs up emoji
}
}

let newargs = newargs.into_pytuple(vm);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggested change
let newargs = newargs.into_pytuple(vm);
let newargs = newargs.into();

Into is enough to convert from PyRef to PyObjectRef

Snowapril reacted with thumbs up emoji
@SnowaprilSnowaprilforce-pushed thega_mapping_protocol branch 2 times, most recently from0cf8b3a to9870276CompareOctober 25, 2021 14:04
Comment on lines 210 to 226
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
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggested change
ifletOk(sub_params) =PyTupleRef::try_from_object(vm, sub_params){
let sub_args = sub_params
.as_slice()
.iter()
.map(|arg|{
ifletSome(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|{
ifletSome(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))
})

Comment on lines 275 to 283
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)?);
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggested change
letmut new_args:Vec<PyObjectRef> =Vec::with_capacity(zelf.args.len());
for argin zelf.args.as_slice().iter(){
ifis_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|{
ifis_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

Snowapril reacted with thumbs up emoji
Copy link
ContributorAuthor

@SnowaprilSnowaprilOct 26, 2021
edited
Loading

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 thatVec<Result> can be converted like that

Signed-off-by: snowapril <sinjihng@gmail.com>
Signed-off-by: snowapril <sinjihng@gmail.com>
Signed-off-by: snowapril <sinjihng@gmail.com>
@Snowapril
Copy link
ContributorAuthor

I add extra test that didn't caught by unittest

fromtypingimportTypeVarY=TypeVar('Y')assertdict[str,Y][int]==dict[str,int]

@youknowoneyouknowone merged commitc414da1 intoRustPython:mainOct 26, 2021
@youknowone
Copy link
Member

Thanks!

Snowapril reacted with thumbs up emoji

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@youknowoneyouknowoneyouknowone approved these changes

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

2 participants
@Snowapril@youknowone

[8]ページ先頭

©2009-2025 Movatter.jp