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

pyclass: switch from immutable to frozen #2448

Merged
merged 2 commits into from
Jun 20, 2022
Merged

Conversation

davidhewitt
Copy link
Member

Switches the WIP #[pyclass(immutable)] to #[pyclass(frozen)], as suggested in #2325 (comment)

What should we name the Mutability/Mutable/Immutable traits and structs then? Those are user-visible.

@mejrs - I had an interesting idea this morning and introduced a "boolean trait" setup which allows us to write PyClass<Frozen = False> etc. I think it works pretty nicely!

(It would have been cool to use an associated const and be able to constrain on PyClass<FROZEN = false> instead, but associated const equality is unstable. The boolean trait system above seems fine as a replacement.)

The only thing remaining with "mutability" in the name is the PyClassMutability trait, which I consider an implementation detail anyway. I think it should be possible to clean it up later. Just wanted to start by throwing this idea out there.

@davidhewitt davidhewitt requested a review from mejrs June 11, 2022 11:27
@davidhewitt davidhewitt force-pushed the frozen branch 4 times, most recently from 1f7037a to 20324ee Compare June 12, 2022 07:59
Copy link
Member

@mejrs mejrs left a comment

Choose a reason for hiding this comment

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

Thanks, I agree this approach is cleaner overall 🙂

src/lib.rs Outdated
@@ -357,6 +357,7 @@ pub use inventory; // Re-exported for `#[pyclass]` and `#[pymethods]` with `mult
#[macro_use]
mod internal_tricks;

pub mod boolean_traits;
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this deserves to be visible as its own file/module. It seems more appropriate to have this (appear to be) a part of src/pyclass.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've gone for pyclass::boolean_struct::{True, False} instead.

Comment on lines +1 to +23
error[E0277]: the trait bound `PyDict: PyClass` is not satisfied
--> tests/ui/abi3_nativetype_inheritance.rs:5:1
|
5 | #[pyclass(extends=PyDict)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `PyClass` is not implemented for `PyDict`
|
= note: required because of the requirements on the impl of `PyClassBaseType` for `PyDict`
note: required by a bound in `PyRefMut`
--> src/pycell.rs
|
| pub struct PyRefMut<'p, T: PyClass<Frozen = False>> {
| ^^^^^^^^^^^^^^ required by this bound in `PyRefMut`
= note: this error originates in the attribute macro `pyclass` (in Nightly builds, run with -Z macro-backtrace for more info)

Copy link
Member

Choose a reason for hiding this comment

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

This error is pretty bad 😬

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I fully agree. I'd like to call it out-of-scope of this PR to fix; ultimately I'd like to remove this error completely in #1344

src/pycell.rs Outdated Show resolved Hide resolved
Comment on lines +27 to +28
/// Frozen or not
type Frozen: Frozen;
Copy link
Member Author

Choose a reason for hiding this comment

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

Given that the True / False nature of this field exactly mirrors the macro attribute, I felt comfortable exposing this in the PyClass trait rather than the PyClassImpl trait. Also seems sensible given that users see this type in the public api docs for e.g. PyRefMut.

Copy link
Member Author

@davidhewitt davidhewitt Jun 20, 2022

Choose a reason for hiding this comment

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

(will merge this branch now, we can always change this before 0.17 if you disagree)

@davidhewitt davidhewitt merged commit 920fa93 into PyO3:main Jun 20, 2022
@davidhewitt davidhewitt deleted the frozen branch June 20, 2022 06:27
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.

None yet

2 participants