PyO3 / PyO3/pyo3

Recursive enum type does not know its name (minor)

Open
#5,420 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

Recursive enum types were introduced in https://github.com/PyO3/pyo3/pull/4694 (thanks!). They work when explicitly referencing the enum's name, but when it's referred as Self PyO3 cannot recognize it.

For example, the works fine:

#[pyclass]
enum TestRecursiveEnum {
    WithRec { my_field: Py<TestRecursiveEnum> },
}

But this leads to a compilation error:

#[pyclass]
enum TestRecursiveEnum {
    WithRec { my_field: Py<Self> },
}
error[E0308]: mismatched types
  --> anise-py/src/astro.rs:37:15
   |
37 |     WithRec { my_field: Py<Self> },
   |               ^^^^^^^^ expected `Py<TestRecursiveEnum>`, found `Py<TestRecursiveEnum_WithRec>`
   |
   = note: expected struct `pyo3::Py<TestRecursiveEnum>`
              found struct `pyo3::Py<TestRecursiveEnum_WithRec>`

error[E0308]: mismatched types
    --> anise-py/src/astro.rs:37:15
     |
35   | #[pyclass]
     | ---------- arguments to this function are incorrect
36   | enum TestRecursiveEnum {
37   |     WithRec { my_field: Py<Self> },
     |               ^^^^^^^^ expected `&Py<TestRecursiveEnum_WithRec>`, found `&Py<TestRecursiveEnum>`
     |
     = note: expected reference `&pyo3::Py<TestRecursiveEnum_WithRec>`
                found reference `&pyo3::Py<TestRecursiveEnum>`
note: associated function defined here
    --> /home/chris/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/pyo3-0.25.1/src/impl_/pyclass.rs:1440:12
     |
1440 |     pub fn convert_field<'a, 'py, T>(obj: &'a T, py: Python<'py>) -> PyResult<Py<PyAny>>
     |            ^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0308`.
error: could not compile `anise-py` (lib) due to 2 previous errors
Steps to Reproduce
  1. Define a recursive type with Self in the field (cf. description)
  2. Build
Your operating system and version

Linux, Opensuse Tumbleweed

Your Python version (python --version)

3.11.12

Your Rust version (rustc --version)

1.88

Your PyO3 version

0.25.1

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

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 with recursive enum support from PR #4694 and the pyclass.rs area named in the compiler trace; reproduce the #[pyclass] enum using Py from the report. Done means the Self form compiles without the shown type mismatch while the explicitly named form remains working.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, rust
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.