Skip to content

introspect types built by create_exception!#6209

Open
fee1-dead wants to merge 1 commit into
PyO3:mainfrom
fee1-dead-contrib:exception-introspection
Open

introspect types built by create_exception!#6209
fee1-dead wants to merge 1 commit into
PyO3:mainfrom
fee1-dead-contrib:exception-introspection

Conversation

@fee1-dead

Copy link
Copy Markdown

Looks like piping the data to a doc(hidden) procedural macro is good enough and didn't require much implementation effort.

see pytests/stubs/exception.pyi for how this changed stub generation.

newsfragments: pending.

tested locally using nox -s test

@fee1-dead
fee1-dead force-pushed the exception-introspection branch from 4b4b0a4 to 0b7d5ff Compare July 17, 2026 23:10
@bschoenmaeckers
bschoenmaeckers requested a review from Tpt July 18, 2026 07:32
@Tpt

Tpt commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

Thank you for this! Sadly @davidhewitt (the main pyo3 maintainer) is not very keen to add more complexity in the create_exception! macro. We plan to deprecate this macro in favor of just plain #[pyclass]. See this comment for more details. But there might be a chance to change his mind.

@fee1-dead

Copy link
Copy Markdown
Author

Thanks for the reply and pointer! I wasnt aware of it and I saw an unticked checkbox on the tracking issue, so i figured it would be nice to support it. Is there a rough design in how to make pyclasses support exceptions, or any way I can help out with implementing that?

@mejrs

mejrs commented Jul 18, 2026

Copy link
Copy Markdown
Member

I think API wise I think we'd prefer #[pyclass(extends = PyException)].

There is #295 which is a pretty old issue though.

There is also #4186 (see #4186 (comment) as well)

Both of those PRs ran into some issues related to subclassing on abi3 builds, but we have evolved the subclassing machinery since and have also dropped support for older python versions.

@mejrs

mejrs commented Jul 18, 2026

Copy link
Copy Markdown
Member

Bit of a brainfart obviously; #[pyclass(extends = PyException)] is a thing that works today to define exceptions (since #1591)

@Icxolu

Icxolu commented Jul 18, 2026

Copy link
Copy Markdown
Member

It's also possible since #5733 to do this on abi3 (for 3.12+). One point that is still missing to make this more usable is adding the ability to pass down arguments to the native superclass during construction, so that an error message can be set for example (I plan to have a look at that again). Otherwise I think the parts are there, PyClassInitializer to create the Python exception object and PyErr::from_value to create a PyErr from it. There are probably quite some opportunities to make this more ergonomic in the long run, but I don't think we are too far off from making it somewhat workable.

@fee1-dead

Copy link
Copy Markdown
Author

It's also possible since #5733 to do this on abi3 (for 3.12+).

Does that mean it is currently not supported for python 3.10?

@Icxolu

Icxolu commented Jul 20, 2026

Copy link
Copy Markdown
Member

Yes, when using abi3 #[pyclass(extends = PyException)] is only possible for 3.12+ (very unlikely to change), on the unlimited it's possible for all versions I think.

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.

4 participants