-
Notifications
You must be signed in to change notification settings - Fork 795
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
Use Arbitrary Self Types instead of Py<Type>Methods traits #4885
base: main
Are you sure you want to change the base?
Conversation
Arbitrary self types conflicts with the `Deref` implementation for `Bound<'py, T> where T: DerefToPyAny`. This commit replaces that implementation with a blanket implementation of `PyAnyMethods` instead. Unfortunately, this greatly increases the verbosity of using `Py_Methods` when multiple traits match. It also requires many more calls to `as_any()` or `into_any()`.
This largely replaces the need for `Py<Type>Methods` traits, since these methods can now be implemented directly on the type.
adc5264
to
4076c62
Compare
4076c62
to
42045ca
Compare
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.
Thanks very much for running with this! I haven't managed to read in detail, can you summarise how it feels to use? I see there's a fair bit of path syntax change, that's unexpected to me.
Do you think we should be proceeding down this road? Does it seem like it's something fundamental with our design that's wrong? Or something in the self types feature which we should flag upstream before it stabilizes?
@@ -90,7 +90,7 @@ fn sum_as_string(a: usize, b: usize) -> PyResult<String> { | |||
/// import the module. | |||
#[pymodule] | |||
fn string_sum(m: &Bound<'_, PyModule>) -> PyResult<()> { | |||
m.add_function(wrap_pyfunction!(sum_as_string, m)?)?; | |||
PyModule::add_function(m, wrap_pyfunction!(sum_as_string, m)?)?; |
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.
There's a lot of these changes to use path syntax, why?
For #4869.
I removed the
Deref
implementation forBound<'py, T>
because it conflicts with any implementation ofstd::ops::Receiver
we need to use Arbitrary Self Types. For example:I also added an implementation of
PyAnyMethods
forBound<'py, T>
to compensate for the removal ofDeref
. To get this working, I dropped the requirement thatPyAnyMethods
can only be implemented for sealed types. An alternative approach I think would be to just removeDeref
and require.as_any()
or.into_any()
calls in more places.After all of this,
clippy
passes on nightly on my machine (with the exception of one lint, which I think is unrelated to this PR). I can also run much of the test suite, but get an error in#[pyclass]
which I currently don't understand:I'm confident this is coming from
pyo3/pyo3-macros-backend/src/pyclass.rs
Line 1834 in 29c64d8
.as_type_ptr
method shouldn't be found, since it's implemented inpyo3/src/types/typeobject.rs
Lines 51 to 54 in 29c64d8