PyO3 / pyo3

Rust bindings for the Python interpreter
https://pyo3.rs
Apache License 2.0
12.19k stars 752 forks source link

Cannot implement `__len__` classmethod (0.21+) #4640

Open timkpaine opened 23 hours ago

timkpaine commented 23 hours ago

Bug Description

This used to work in pyo3<0.21:

    #[classmethod]
    fn __len__(cls: &Bound<'_, PyType>) -> PyResult<usize> {
        Ok(0)
    }
error[E0425]: cannot find value `_slf_ref` in this scope
  --> lib1-py/src/lib.rs:15:1
   |
15 | #[pymethods]
   | ^^^^^^^^^^^^ not found in this scope

Steps to Reproduce

Include the above block in any #[pymethods] block

Backtrace

error[E0425]: cannot find value `_slf_ref` in this scope
  --> lib1-py/src/lib.rs:15:1
   |
15 | #[pymethods]
   | ^^^^^^^^^^^^ not found in this scope

Your operating system and version

macOS 15.0.1

Your Python version (python --version)

Python 3.11.10

Your Rust version (rustc --version)

rustc 1.79.0 (129f3b996 2024-06-10)

Your PyO3 version

0.22.5

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

brew install python@3.11, pip install pyo3

Additional Info

No response

LilyFoote commented 12 hours ago

Defining __len__ as a @classmethod seems very unusual to me. What's your use case?