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

Use Arbitrary Self Types instead of Py<Type>Methods traits #4885

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

LilyFoote
Copy link
Contributor

@LilyFoote LilyFoote commented Feb 1, 2025

For #4869.

I removed the Deref implementation for Bound<'py, T> because it conflicts with any implementation of std::ops::Receiver we need to use Arbitrary Self Types. For example:

error[E0119]: conflicting implementations of trait `std::ops::Receiver` for type `instance::Bound<'_, PyBool>`
  --> src/types/boolobject.rs:27:1
   |
27 | impl std::ops::Receiver for Bound<'_, PyBool> {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: conflicting implementation in crate `core`:
           - impl<P, T> std::ops::Receiver for P
             where <P as Deref>::Target == T, P: Deref, P: ?Sized, T: ?Sized;

I also added an implementation of PyAnyMethods for Bound<'py, T> to compensate for the removal of Deref. To get this working, I dropped the requirement that PyAnyMethods can only be implemented for sealed types. An alternative approach I think would be to just remove Deref 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:

error[E0599]: no method named `as_type_ptr` found for reference `&pyo3::Bound<'_, PyType>` in the current scope
 --> tests/test_field_cfg.rs:5:1
  |
5 | #[pyclass]
  | ^^^^^^^^^^
  |
  = note: this error originates in the attribute macro `pyclass` (in Nightly builds, run with -Z macro-backtrace for more info)
help: there is a method `as_ptr` with a similar name
  |
5 | as_ptr
  |

I'm confident this is coming from

.as_type_ptr()
but I see no reason why the .as_type_ptr method shouldn't be found, since it's implemented in
#[inline]
pub fn as_type_ptr(self: &Bound<'py, Self>) -> *mut ffi::PyTypeObject {
self.as_ptr() as *mut ffi::PyTypeObject
}

@LilyFoote LilyFoote added refactoring rust Pull requests that update Rust code labels Feb 1, 2025
@LilyFoote LilyFoote self-assigned this Feb 1, 2025
@LilyFoote LilyFoote added the CI-no-fail-fast If one job fails, allow the rest to keep testing label Feb 1, 2025
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.
@LilyFoote LilyFoote force-pushed the arbitrary-self-types-py-any-methods branch 5 times, most recently from adc5264 to 4076c62 Compare February 2, 2025 22:15
@LilyFoote LilyFoote force-pushed the arbitrary-self-types-py-any-methods branch from 4076c62 to 42045ca Compare February 2, 2025 22:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI-no-fail-fast If one job fails, allow the rest to keep testing refactoring rust Pull requests that update Rust code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant