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

PyInt cannot be subclassed #4455

Open
ChayimFriedman2 opened this issue Aug 19, 2024 · 1 comment
Open

PyInt cannot be subclassed #4455

ChayimFriedman2 opened this issue Aug 19, 2024 · 1 comment
Labels

Comments

@ChayimFriedman2
Copy link
Contributor

ChayimFriedman2 commented Aug 19, 2024

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 #4453.

@davidhewitt
Copy link
Member

See also #1647, I think int is a PyVarObject like tuple. If we were fixing this, I think it would also likely help with #1344.

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

No branches or pull requests

2 participants