-
Notifications
You must be signed in to change notification settings - Fork 796
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
Support all receiver types for all protocol methods #1206
Comments
So what would the trait definition would be? |
I think I see a way to make a |
Quick update from me: over the weekend I had a little time to play with a trait along the lines of:
It almost works, but there's some lifetime challenges about making sure the receiver lifetime is scoped correctly and safely. For the
I'm hopeful that if I continue experimenting with designs in this space I'll be able to come up with a definition which is sound and gets us what we want. |
So you mean you're extending the current |
I think that the most important thing is that we support
So for |
I'm also very interested in this, for another use case: |
I started to implemented this type then quickly found myself blocked on PyO3/pyo3#1205 / PyO3/pyo3#1206 due to not being able to return `Self` from `__enter__`. We'll have to wait for a future pyo3 release before we can finish the Rust port.
I started to implemented this type then quickly found myself blocked on PyO3/pyo3#1205 / PyO3/pyo3#1206 due to not being able to return `Self` from `__enter__`. We'll have to wait for a future pyo3 release before we can finish the Rust port.
I took another look at this today, with a hope that after #1328 it should be possible to do further refactoring to support all of the receiver types listed above. The answer is that if we change the traits to have
... then for these traits it's not possible to call As a consequence, supporting
Then I would expect I should be able to call This problem might eventually be fixed with the
|
Without knowing too much about the implementation specifics, idea #2 sounds good to me. It will feel natural to Python users, since it mirrors how special methods are defined there. On the Rust side though, the "trait-ness" of these interfaces is lost. Do people use the traits on the Rust side to handle different objects with common traits - even if the traits only contain Python related functionality? (If desperately needed this could still be mitigated by still having the traits and implementing them automatically from the pymethods macro, with its methods just forwarding to the related pymethod.) |
It's possible, but I haven't seen this in practice. Note that a Rust extension module will only have these protocol traits defined for its own pyclasses, and not for any builtin or thirdparty Python object. So most consumption of the Python protocols will still need to go via Python's dynamic type system / attribute lookup at runtime. Having drafted the implementation for option 1 in #1561, I also now strongly prefer option 2. I agree about just having In either way, I think release 0.14 is due soon as we've got a lot of changes piling up, so I'm regrettably going to shift this to the 0.15 milestone. In my eyes this is one of the top things to sort for 0.15, along with #1056. |
I think idea 2 could be better for those who already know Python well, but for Rust users who don't know Python very well, the trait is much better. I actually didn't know Python very well when I had to write some Rust extensions for Python. I'm not sure it's a major case, though. Also, even if we remove protocol traits, I think we have to leave some traits (e.g., buffer). So, if we are going to remove protocol traits, we need to clarify what traits should be remained. |
This is exactly what I've drafted in #1561. As well as being a bit ugly it's also a big migration for all existing code to have to change.
What I think I'm hearing from this is that the traits provide useful grouping and documentation. I think with good documentation in the guide we can have most of the same benefit with |
I meant rewriting trait methods by proc macro. |
Ahh I see! Yes, it's true The downsides of this approach:
If people think that option 1 is the better final design than option 2, then I could support migrating to option 1 via this. However I still think that option 2 is probably nicer overall. I need to try and draft an implementation of option 2 and see what it looks like in reality! |
Another interesting point for the discussion: a user this week tried to implement This further makes me think just having the one macro would help avoid confusion. |
OK, now I'm also inclined to use pymethod for everything. Raising compile errors and preparing documentation would be big stuff, though. |
Yes, I'm willing to put in the effort to build all of the necessary implementation for 0.15! |
I've had this problem too.
I'm on board to do this. I know my way around the magic methods (in python) pretty well. |
Help with the documentation when we're ready would be really amazing. I'll probably start experimenting with this in about a month's time once 0.14 release is done and any relevant bugfixes out the way. I imagine that what this will end up being like is that the For example, we might need to document constraints like:
I imagine that it might need a couple iterations to make this all easy to read. |
The initial implementation for |
At the moment the protocol methods are in an inconsistent state: some of them can take
PyRef
orPyRefMut
, and some of them take&self
or&mut self
.This is confusing to users and also gets in the way in certain cases like needing to access
Python
inside a protocol method (which can be obtained fromPyRef::py()
for example) or wanting to returnPyRef<Self>
.The simple solution is to just change all protocol methods to use
TryFromPyCell
trait. This is however a breaking change.The better solution is to change all
#[pyproto]
methods to support any of the five of&PyCell
,PyRef<Self>
,PyRefMut<Self>
,&self
and&mut self
, just like we support for#[pymethods]
.I've had some ideas how to approach this second point so would like to take a shot at it soon.
The text was updated successfully, but these errors were encountered: