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

Missing __init_subclass__ and __set_name__ magic methods in the user guide #4180

Closed
Divy1211 opened this issue May 12, 2024 · 2 comments · Fixed by #4190
Closed

Missing __init_subclass__ and __set_name__ magic methods in the user guide #4180

Divy1211 opened this issue May 12, 2024 · 2 comments · Fixed by #4190

Comments

@Divy1211
Copy link

Divy1211 commented May 12, 2024

Bug Description

(Details for both methods: https://peps.python.org/pep-0487/#proposal)

Both magic methods seem to work in PyO3, so it looks like it's just the documentation for them that is missing from the user guide. Here's the code that I got to work for reference:

__set_name__:

#[pymethods]
impl MapValidate {
    fn __set_name__(&mut self, _owner: &Bound<'_, PyType>, name: &str) {
        self.name = name.to_string()
    }
}

__init_subclass__:

#[pymethods]
impl BaseStruct {
    #[classmethod]
    fn __init_subclass__(cls: &Bound<'_, PyType>) -> PyResult<()> {
        // code here
    }
}

Your operating system and version

Windows 10 Home 22H2 Build 19045.4291

Your Python version (python --version)

3.10.11

Your Rust version (rustc --version)

1.71.1

Your PyO3 version

0.21.2

How did you install python? Did you use a virtualenv?

  • python.org
  • created a venv using python -m venv venv
@davidhewitt
Copy link
Member

This is deliberate, though the docs don't necessarily make it super clear. If it's not in the list in the docs then PyO3 doesn't need to do anything special with it and Python will handle it fine anyway.

@Divy1211
Copy link
Author

This is deliberate, though the docs don't necessarily make it super clear. If it's not in the list in the docs then PyO3 doesn't need to do anything special with it and Python will handle it fine anyway.

Ahh, I see I see, I was confused because I (incorrectly) believed that PyO3 needs to deliberately support each magic method for them to work. Glad to hear that's not the case!

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

Successfully merging a pull request may close this issue.

2 participants