Skip to content

experimental-inspect: dedup union members - #6273

Open
jonasdedden wants to merge 2 commits into
PyO3:mainfrom
jonasdedden:experimental-inspect-dedup-union-members
Open

experimental-inspect: dedup union members#6273
jonasdedden wants to merge 2 commits into
PyO3:mainfrom
jonasdedden:experimental-inspect-dedup-union-members

Conversation

@jonasdedden

Copy link
Copy Markdown
Contributor

#[derive(FromPyObject)] on an enum builds its INPUT_TYPE by reduce-ing the variants' own input types with |. Two ariants overlapping is normal, and the result repeats the overlap:

    enum FileInput { Path(PathBuf), Name(String), FileLike(PyFileLikeObject) }
    // file: str | os.PathLike[str] | str | Incomplete

The macro cannot collapse that - at expansion time the members are unresolved associated constants, and it has no idea PathBuf contributes a str arm. By the time pyo3-introspection renders the union it is a plain expression tree, so this flattens the | chain and drops structurally equal repeats, keeping first-occurrence order.

Additional formatting improvement: The same match arm also wrote buffer.push(' ') before the | and nothing after it, so every union came out as str |None. That is invisible in this repository because nox -s test-introspection pipes the stubs through ruff format before comparing them to the checked-in fixtures, but it is what the raw output looks like to anyone who does not format it.

`#[derive(FromPyObject)]` on an enum builds its `INPUT_TYPE` by `reduce`-ing
the variants' own input types with `|`. Two variants overlapping is normal, and
the result repeats the overlap:

    enum FileInput { Path(PathBuf), Name(String), FileLike(PyFileLikeObject) }
    // file: str | os.PathLike[str] | str | Incomplete

The macro cannot collapse that — at expansion time the members are unresolved
associated constants, and it has no idea `PathBuf` contributes a `str` arm. By
the time `pyo3-introspection` renders the union it is a plain expression tree,
so this flattens the `|` chain and drops structurally equal repeats, keeping
first-occurrence order.

The same match arm also wrote `buffer.push(' ')` before the `|` and nothing
after it, so every union came out as `str |None`. That is invisible in this
repository because `nox -s test-introspection` pipes the stubs through
`ruff format` before comparing them to the checked-in fixtures, but it is what
the raw output looks like to anyone who does not format it.
@jonasdedden jonasdedden changed the title experimental-inspect dedup union members experimental-inspect: dedup union members Jul 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant