PyO3 / pyo3

Rust bindings for the Python interpreter
https://pyo3.rs
Other
11.89k stars 729 forks source link

non-local `impl` definition, they should be avoided as they go against expectation #4094

Open waynexia opened 4 months ago

waynexia commented 4 months ago

Bug Description

In our code https://github.com/GreptimeTeam/greptimedb/blob/main/src/script/src/python/pyo3/vector_impl.rs#L94 like

#[pymethods]
impl PyVector {
    /// convert from numpy array to [`PyVector`]
    #[classmethod]
    fn from_numpy(cls: &PyType, py: Python<'_>, obj: PyObject) -> PyResult<PyObject> {
        let pa = py.import("pyarrow")?;
        let obj = pa.call_method1("array", (obj,))?;
        let zelf = Self::from_pyarrow(cls, py, obj.into())?;
        Ok(zelf.into_py(py))
    }
    ...
}

Compile with toolchain nightly-2024-04-18 would get the following error:

error: non-local `impl` definition, they should be avoided as they go against expectation
  --> src/script/src/python/pyo3/vector_impl.rs:94:1
   |
94 | #[pymethods]
   | ^^^^^^^^^^^^
   |
   = help: move this `impl` block outside the of the current method `__or__` and up 2 bodies
   = note: an `impl` definition is non-local if it is nested inside an item and may impact type checking outside of that item. This can be the case if neither the trait or the self type are at the same nesting level as the `impl`
   = note: one exception to the rule are anon-const (`const _: () = { ... }`) at top-level module and anon-const at the same nesting as the trait or type
   = note: this lint may become deny-by-default in the edition 2024 and higher, see the tracking issue <https://github.com/rust-lang/rust/issues/120363>
   = note: the attribute macro `pymethods` may come from an old version of the `pyo3_macros` crate, try updating your dependency with `cargo update -p pyo3_macros`
   = note: this error originates in the attribute macro `pymethods` (in Nightly builds, run with -Z macro-backtrace for more info)

Steps to Reproduce

git clone https://github.com/waynexia/greptimedb.git
cd greptimedb
git checkout update-toolchain-3
make clippy

Source

Backtrace

N/A

Your operating system and version

Arch Linux x86_64 6.8.6-arch1-1

Your Python version (python --version)

Python 3.11.8

Your Rust version (rustc --version)

rustc 1.79.0-nightly (becebb315 2024-04-17)

Your PyO3 version

0.20

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

pacman

Additional Info

To workaround it, add this to Cargo.toml

rust.non_local_definitions = "allow"
adamreichold commented 4 months ago

Please update to the current version 0.21.2 which resolves this.

tisonkun commented 4 months ago

@adamreichold Thanks for your information!

In GreptimeDB we can't upgrade pyo3 now due to our deps (arrow, datafusion) are using pyo3 0.20:

But once these deps upgrade, we'll try this out.

Cross ref -