Skip to content

Introspection: generate nested classes for complex enums#5708

Merged
davidhewitt merged 2 commits intoPyO3:mainfrom
Tpt:tpt/inspect-enums
Jan 29, 2026
Merged

Introspection: generate nested classes for complex enums#5708
davidhewitt merged 2 commits intoPyO3:mainfrom
Tpt:tpt/inspect-enums

Conversation

@Tpt
Copy link
Contributor

@Tpt Tpt commented Dec 18, 2025

No description provided.

/// Identifier of the class Rust struct
cls_ident: &'a Ident,
/// Name of the class in Python
cls_name: &'a Ident,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need the two "flavors" because for complex enum variants the first one will be Enum_Variant and the second one just Variant

@Tpt Tpt force-pushed the tpt/inspect-enums branch 3 times, most recently from 2b185a2 to d49ade6 Compare December 18, 2025 15:54
@Tpt Tpt marked this pull request as ready for review December 18, 2025 16:46
@Tpt
Copy link
Contributor Author

Tpt commented Dec 19, 2025

Note the the PyTypeInfo::TYPE_HINT value for variants is currently wrong. Will fix it in a follow-up.

Comment on lines 22 to 27
class MixedComplexEnum:
@final
class Empty(MixedComplexEnum): ...

@final
class Nothing(MixedComplexEnum): ...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This syntax technically isn't valid:

class MixedComplexEnum:
    @final
    class Empty(MixedComplexEnum): ...

    @final
    class Nothing(MixedComplexEnum): ...

gives

Traceback (most recent call last):
  File "<python-input-2>", line 1, in <module>
    class MixedComplexEnum:
    ...<4 lines>...
        class Nothing(MixedComplexEnum): ...
  File "<python-input-2>", line 3, in MixedComplexEnum
    class Empty(MixedComplexEnum): ...
                ^^^^^^^^^^^^^^^^
NameError: name 'MixedComplexEnum' is not defined

... I wonder, is something like the below better?

class MixedComplexEnum:
    Empty: Final[type[Empty]] = ...
    Nothing: Final[type[Nothing]] = ...
    
@final
class Empty(MixedComplexEnum): ...

@final
class Nothing(MixedComplexEnum): ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. Missed that. Thanks!

... I wonder, is something like the below better?

We would need to tweak it a bit to make sure Empty is not exposed as one of the member of the module. Happily there is a hack for that. I will experiment around it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just tested with Mypy and this syntax error does not prevent it to parse the stubs.

I added the implementation of enums method to this MR.

If it's fine with you I would leave this issue for later (adding it to the introspection TODO issue) and focus on having mypy green on the pytests module (we are nearly there, with this MR and #5758 I only get misc and override errors that should be not too hard to fix). WDYT?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me, happy to defer until when we know it's actually a problem (if it ever is).

@Tpt Tpt marked this pull request as draft December 19, 2025 14:47
@Tpt Tpt force-pushed the tpt/inspect-enums branch from d49ade6 to 0386177 Compare January 28, 2026 08:45
@Tpt Tpt marked this pull request as ready for review January 28, 2026 09:24
@davidhewitt davidhewitt added this pull request to the merge queue Jan 29, 2026
Merged via the queue into PyO3:main with commit e8701ab Jan 29, 2026
44 checks passed
@Tpt Tpt deleted the tpt/inspect-enums branch January 30, 2026 15:39
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.

2 participants