Repository navigation
Description
Bug Description
PyO3 generates a default __repr__ for every simple enum #[pyclass]. The guide says that a user can override it in #[pymethods]:
All methods defined by PyO3 can be overridden.
For example here's how you override__repr__:
When a user does this, PyO3 puts two Py_tp_repr entries in the type's slot array. One entry is the generated __pyo3__repr__. The other entry is the user method. PyTypeBuilder::push_slot adds each slot and does not check for duplicates.
Python 3.14 and earlier accept the duplicate without a warning. Python 3.15 implements PEP 820 (PySlot), and PyO3 0.29 creates types through PyType_FromSlots on 3.15. The duplicate now emits this warning when the class is created:
DeprecationWarning: type builtins.Color has multiple Py_tp_repr (66) slots. This is deprecated.
The user __repr__ still wins, so the behavior is correct today. But the warning has these effects:
- A test suite that turns warnings into errors (
-W error, or pytestfilterwarnings = ["error"]) cannot import the module. The import fails withRuntimeError: An error occurred while initializing class Color. TheDeprecationWarningis only the__cause__. - The CPython docs for
PySlot(Doc/c-api/slots.rst) say: "Except when documented otherwise, multiple slots with the same ID may not occur in a single slots array." The duplicate check inPython/slots.ccan also raiseSystemErrorfor slot IDs whose duplicates are rejected. So a later CPython release can make this a hard error.
We found this in icechunk, when we added Python 3.15 to CI. Two simple enums there override __repr__.
Steps to Reproduce
Cargo.toml:
[package]
name = "repr_repro"
version = "0.1.0"
edition = "2024"
[lib]
crate-type = ["cdylib"]
[dependencies]
pyo3 = "=0.29.3"src/lib.rs:
use pyo3::prelude::*;
#[pyclass(eq, eq_int)]
#[derive(PartialEq)]
enum Color {
Red,
Green,
}
#[pymethods]
impl Color {
fn __repr__(&self) -> &'static str {
match self {
Color::Red => "<red>",
Color::Green => "<green>",
}
}
}
#[pymodule]
fn repr_repro(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<Color>()
}check.py:
import sys
import warnings
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
import repr_repro
print(sys.version)
print([str(w.message) for w in caught])
print(repr(repr_repro.Color.Red))Build the module with maturin build -i <python>, install the wheel in a venv, and run python check.py. Then run python -W error -c "import repr_repro".
Results:
| Interpreter | Warning at import | repr(Color.Red) |
-W error import |
|---|---|---|---|
| 3.14.3 | none | <red> |
OK |
| 3.15.0rc1 | type builtins.Color has multiple Py_tp_repr (66) slots. This is deprecated. |
<red> |
RuntimeError: An error occurred while initializing class Color |
| 3.15.0rc3 free-threaded | type builtins.Color has multiple Py_tp_repr (66) slots. This is deprecated. |
<red> |
RuntimeError: An error occurred while initializing class Color |
Backtrace
$ python -W error -c "import repr_repro"
DeprecationWarning: type builtins.Color has multiple Py_tp_repr (66) slots. This is deprecated.
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<string>", line 1, in <module>
import repr_repro
File ".../venv/lib/python3.15/site-packages/repr_repro/__init__.py", line 1, in <module>
from .repr_repro import *
RuntimeError: An error occurred while initializing class ColorYour operating system and version
macOS 26.5.2 (arm64)
Your Python version (python --version)
Python 3.15.0rc1 (uv-managed) and Python 3.15.0rc3 free-threaded (conda-forge python-freethreading). Python 3.14.3 is the control.
Your Rust version (rustc --version)
rustc 1.98.0 (88d9e12ae 2026-08-18)
Your PyO3 version
0.29.3. We also see it with 0.29.2. main at 914665c has the same push_slot code and the same default repr generation.
How did you install python? Did you use a virtualenv?
uv (uv python install 3.15) and conda-forge through pixi (conda-forge/label/python_rc). The repro ran in a uv venv for each interpreter.
Additional Info
- The generated repr comes from
pyo3-macros-backend/src/pyclass.rs(default_repr/__pyo3__repr__). The macro adds it todefault_slotsand does not check whether#[pymethods]already has__repr__. The macro cannot see#[pymethods]blocks, so a fix inPyTypeBuildercan be easier: when a user slot and a default slot have the same ID, keep the user slot. __int__has the same problem. A user__int__on a simple enum givestype builtins.Color has multiple Py_nb_int (26) slots. This is deprecated.The generated__pyo3__repr__and__pyo3__int__are the defaults that a user can override, and both get duplicate slots.- The problem does not need
eqoreq_int. A plain#[pyclass] enumwith a user__repr__gives the same warning. - The other generated defaults are not affected. A user
__str__with#[pyclass(str)],__richcmp__witheq, or__hash__withhashgives compile error E0592 (duplicate definitions). - Workaround: remove the user
__repr__or__int__and use the generated one.
Activity
Use Python 3.15t for free-threaded CI and release wheels
chore(native): bump to libdatadog 45 (#20945)
Use Python 3.15t for free-threaded CI and release wheels
Use Python 3.15t for free-threaded CI and release wheels
Use Python 3.15t for free-threaded CI and add `abi3t` to release whee…