Skip to content
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

Make it enable to take &PyClass as arguments as pyfunctions/methods #791

Merged
merged 3 commits into from
Mar 4, 2020

Conversation

kngwyu
Copy link
Member

@kngwyu kngwyu commented Mar 4, 2020

Resolves #786.
For C: PyClass, this PR enables the use of &C, &mut C, Option<&C> and Option<&mut C> as function/method arguments.
The hack I used is

  1. Set ExtractExt::Target for &C, like
impl<'a> pyo3::derive_utils::ExtractExt<'a> for &'a C
{
    type Target = pyo3::PyRef<'a, C>;
}
  1. If the argument is one of &C/&mut C/Option<&C>/Option<&mut C>, extract the argument to <type as ExtractExt::Target> and insert deref.

But, for object protocols, still we need to use PyRef<C> or PyRefMut<C> due to its trait requirements.

//name,
//ty);
}
pub(crate) fn check_ty_optional<'a>(ty: &'a syn::Type) -> Option<&'a syn::Type> {
Copy link
Member Author

Choose a reason for hiding this comment

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

This is just a refactoring.
Not related to the logic so much.

@kngwyu
Copy link
Member Author

kngwyu commented Mar 4, 2020

Somehow tests/test_dunder.rs was mode 100755 so I executed chmod -x for that file.

@kngwyu kngwyu added this to the 0.9.0 milestone Mar 4, 2020
Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

LGTM. One small nitpick on the test.

@kngwyu kngwyu mentioned this pull request Mar 4, 2020
@kngwyu
Copy link
Member Author

kngwyu commented Mar 4, 2020

Thank you for the review 😉

@kngwyu kngwyu merged commit 74b22eb into PyO3:master Mar 4, 2020
@kngwyu kngwyu deleted the pyclass-arg branch March 4, 2020 12:31
@1tgr
Copy link
Contributor

1tgr commented Jul 27, 2020

FYI, I think the syntax x: &Self no longer works for method parameters as a result of this change. A method taking a parameter of type &Self did work on pyo3 v0.8.5.

It's because of this usage of the #tref token outside of the class's impl block:
https://github.com/kngwyu/pyo3/blob/96115eaaaad6df8039fb0dc07521137a89ce15b4/pyo3-derive-backend/src/pymethod.rs#L567

@davidhewitt
Copy link
Member

Thanks, have opened #1070 to remember this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Restore support for &Class and &mut Class as arguments
3 participants