PyO3 / PyO3/pyo3

Bad error message when using incorrect signature for some dunder methods

Open
#4,507 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
16.2k
Forks
1k
Avg merge
2d 6h
Merged PRs (30d)
66

Description

Bug Description

When implementing __iadd__, I assumed that I'd need to return a reference to self since that's generally the behavior expected in python. Following that logic, I wrote it with the signature:

fn __iadd__<'a>(self_: PyRefMut<'a, Self>, other: &Self) -> PyRefMut<'a, Self>.

This lead to a compiler error message about PyRefMut not implementing the correct conversion trait, which lead me down a bit of a rabbit hole until I found the correct section in the readme.

Ideally a dunder method with an incorrect signature could be detected, and the error message note the expected signature or just point users to the relevant section of the readme.

Steps to Reproduce

Define a pyclass with
fn __iadd__<'a>(self_: PyRefMut<'a, Self>, other: &Self) -> PyRefMut<'a, Self>{...} in the impl block, observe the error message

Backtrace

No response

Your operating system and version

Windows 11

Your Python version (python --version)

Python 3.12.4

Your Rust version (rustc --version)

rustc 1.80.1 (3f5fd8dd4 2024-08-06)

Your PyO3 version

0.22.1

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

Basic installer for python, UV 0.3 used for virtualenv

Additional Info

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the reported iadd signature in a PyO3 pyclass impl and compare the compiler diagnostic with the relevant dunder-method section of the README. Trace the pyclass or dunder-method validation entry point that handles the signature, then verify that an incorrect signature produces an actionable expected-signature or documentation diagnostic.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, rust
Domain
developer-experience
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.