Skip to content

Simple enum with a user __repr__ gets two Py_tp_repr slots; Python 3.15 emits a DeprecationWarning #6494

Description

@li-em

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 pytest filterwarnings = ["error"]) cannot import the module. The import fails with RuntimeError: An error occurred while initializing class Color. The DeprecationWarning is 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 in Python/slots.c can also raise SystemError for 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 Color

Your 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 to default_slots and does not check whether #[pymethods] already has __repr__. The macro cannot see #[pymethods] blocks, so a fix in PyTypeBuilder can 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 gives type 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 eq or eq_int. A plain #[pyclass] enum with a user __repr__ gives the same warning.
  • The other generated defaults are not affected. A user __str__ with #[pyclass(str)], __richcmp__ with eq, or __hash__ with hash gives compile error E0592 (duplicate definitions).
  • Workaround: remove the user __repr__ or __int__ and use the generated one.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions