`PyInt` cannot be subclassed
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 16.2k
- Forks
- 1k
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 66
Description
Bug Description
PyInt does not implement PyClassBaseType, because it does not use pyobject_native_type!() but pyobject_native_type_core!(). As such, it cannot be subclassed.
Fixing this is not trivial because:
error[E0277]: the trait bound `pyo3_ffi::PyLongObject: PySizedLayout<types::num::PyInt>` is not satisfied
--> src\types\mod.rs:200:33
|
200 | type LayoutAsBase = $crate::impl_::pycell::PyClassObjectBase<$layout>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `PySizedLayout<types::num::PyInt>` is not implemented for `pyo3_ffi::PyLongObject`, which is required by `PyClassObjectBase<pyo3_ffi::PyLongObject>: PyClassObjectLayout<types::num::PyInt>`
|
::: src\types\num.rs:17:1
|
17 | pyobject_subclassable_native_type!(PyInt, ffi::PyLongObject);
| ------------------------------------------------------------ in this macro invocation
|
= help: the following other types implement trait `PySizedLayout<T>`:
<PyClassObject<T> as PySizedLayout<T>>
<_PyWeakReference as PySizedLayout<PyWeakrefReference>>
<pyo3_ffi::PyBaseExceptionObject as PySizedLayout<PyArithmeticError>>
<pyo3_ffi::PyBaseExceptionObject as PySizedLayout<PyAssertionError>>
<pyo3_ffi::PyBaseExceptionObject as PySizedLayout<PyAttributeError>>
<pyo3_ffi::PyBaseExceptionObject as PySizedLayout<PyBaseException>>
<pyo3_ffi::PyBaseExceptionObject as PySizedLayout<PyBaseExceptionGroup>>
<pyo3_ffi::PyBaseExceptionObject as PySizedLayout<PyBlockingIOError>>
and 74 others
note: required for `PyClassObjectBase<pyo3_ffi::PyLongObject>` to implement `PyClassObjectLayout<types::num::PyInt>`
--> src\pycell\impl_.rs:195:12
|
195 | impl<T, U> PyClassObjectLayout<T> for PyClassObjectBase<U>
| ^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^
196 | where
197 | U: PySizedLayout<T>,
| ---------------- unsatisfied trait bound introduced here
note: required by a bound in `PyClassBaseType::LayoutAsBase`
--> src\impl_\pyclass.rs:1131:24
|
1131 | type LayoutAsBase: PyClassObjectLayout<Self>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `PyClassBaseType::LayoutAsBase`
= note: this error originates in the macro `pyobject_subclassable_native_type` (in Nightly builds, run with -Z macro-backtrace for more info)
Steps to Reproduce
The following code:
use pyo3::prelude::*;
use pyo3::types::PyInt;
#[pyclass(extends=PyInt)]
pub struct IntSubClass {}
Triggers the error:
error[E0277]: the trait bound `PyInt: PyClass` is not satisfied
--> src\lib.rs:4:1
|
4 | #[pyclass(extends=PyInt)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `PyClass` is not implemented for `PyInt`, which is required by `PyInt: PyClassBaseType`
|
= help: the trait `PyClass` is implemented for `IntSubClass`
= note: required for `PyInt` to implement `PyClassBaseType`
= note: this error originates in the attribute macro `pyclass` (in Nightly builds, run with -Z macro-backtrace for more info)
Backtrace
No response
Your operating system and version
Windows 11
Your Python version (python --version)
3.12
Your Rust version (rustc --version)
rustc 1.80.1 (3f5fd8dd4 2024-08-06)
Your PyO3 version
0.22.0
How did you install python? Did you use a virtualenv?
The official website.
Additional Info
Noticed while working on https://github.com/PyO3/pyo3/pull/4453.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing the native-type macros in src/types/num.rs and src/types/mod.rs, then inspect the PyClassObjectLayout and PyClassBaseType bounds in src/pycell/impl_.rs and src/impl_/pyclass.rs. Reproduce the #[pyclass(extends=PyInt)] example and review the related work in pull request 4453; done means the example compiles with PyInt as a base type and regression coverage verifies subclassing.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, rust
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100