PyO3 / PyO3/pyo3

`PyErr::new` has surprising edge cases

Open
#4,412 1 comment 3 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

Continuing from #4400, where we observed that a tuple was being flattened as part of a call to PyErr::new.

The other special case is that PyErr::new(py.None()) counts as no arguments.

#[pyo3::pymodule]
mod pyo3_scratch {
    use pyo3::prelude::*;

    #[pyfunction]
    fn bug(py: Python<'_>) -> PyResult<()> {
        Err(pyo3::exceptions::PyValueError::new_err(py.None()))
    }
}
>>> try:
...     pyo3_scratch.bug()
... except ValueError as e:
...     print(e.args)
... 
()

Again if we wrap the py.None() in a tuple as (py.None(),) then the final e.args will be (None,).

I'm open to debating whether we should change PyErr::new, because this doesn't match Python users' instincts, where there is no special case:

>>> print(ValueError(None).args)
(None,)
>>> print(ValueError((1,)).args)
((1,),)

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 Rust PyErr::new/new_err example and comparing its e.args result with the Python ValueError examples. Review the related discussion in #4400 and determine the intended argument semantics before identifying the behavior and regression coverage the project would accept.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, rust
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.