Skip to content

[Python] generate enums as IntEnum or IntFlag type when requested#8782

Open
jtdavis777 wants to merge 15 commits intogoogle:masterfrom
jtdavis777:feature/python_enum
Open

[Python] generate enums as IntEnum or IntFlag type when requested#8782
jtdavis777 wants to merge 15 commits intogoogle:masterfrom
jtdavis777:feature/python_enum

Conversation

@jtdavis777
Copy link
Collaborator

@jtdavis777 jtdavis777 commented Nov 18, 2025

Fixes #8617 and addresses #5083

This PR has enum types inherit from IntEnum or (IntFlag for bit_flags) directly, instead of just providing this as type hinting, and changes the setter function signatures to require enum types. This code should still be backward and forward compatible on receipt, as ints are still returned by the getters.

This code, chiefly, allows users to go to and from string string values to the enum values, but also provides some runtime type safety (over just type hint safety).

@github-actions github-actions bot added c++ codegen Involving generating code from schema python labels Nov 18, 2025
@fliiiix
Copy link
Contributor

fliiiix commented Nov 18, 2025

Some docs at least in docs/source/flatc.md would be nice for this change

@fliiiix
Copy link
Contributor

fliiiix commented Nov 18, 2025

I played around a bit and it seemed fine, i couldn't integrated it into our main product because this crate was not updated yet rdelfin/flatbuffers-build#30

@jtdavis777 jtdavis777 changed the title [Python] generate enums as IntEnum type when requested [Python] generate enums as IntEnum or IntFlag type when requested Nov 20, 2025
@github-actions github-actions bot added the documentation Documentation label Nov 20, 2025
@jtdavis777 jtdavis777 requested a review from fliiiix November 26, 2025 22:10
Copy link
Contributor

@fliiiix fliiiix left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@jtdavis777 jtdavis777 requested a review from fliiiix December 6, 2025 01:46
@jtdavis777 jtdavis777 requested review from aardappel and fliiiix and removed request for fliiiix December 6, 2025 01:46
aardappel
aardappel previously approved these changes Dec 7, 2025
for (auto import_entry : imports) {
const std::string local_import = "." + mod;

// If we have a file called, say, "MyType.py" and in it we have a
Copy link
Collaborator

Choose a reason for hiding this comment

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

This seems a separate fix? Usually good to keep those in separate PRs, though no biggie keeping it here for now.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

oh I think this is totally unrelated -- just a result of my poor branch management it seems. will remove.

@jtdavis777
Copy link
Collaborator Author

You said it was compatible.. do we need this flag at all? Can't we always do the better thing?
so, I think it depends on what we mean by python 3 support. Now that we have dropped python 2 I think this is safe, but I believe technically this is python 3.4. Though TBF the current implementation is not consistent here so I think just doing this as part of --python-typing is fine. Will change to that!

@fliiiix
Copy link
Contributor

fliiiix commented Dec 23, 2025

Will you still updated this MR @jtdavis777 ?

@jtdavis777
Copy link
Collaborator Author

@fliiix yes it is on my list! Thank you for the reminder.

@fliiiix
Copy link
Contributor

fliiiix commented Dec 23, 2025

No hurry, I just finally got some time to look at changes and im running this right now on my version and am still interested to have this at some point on master :)

@fliiiix
Copy link
Contributor

fliiiix commented Jan 13, 2026

Im integrating the latest version right now any updates on this MR? I would still like to use that at some point :)

@jtdavis777
Copy link
Collaborator Author

No updates yet, I want to just fold it into the typing flag. Apologies for the delays!

GenReceiver(struct_def, code_ptr);
code += namer_.Method(field);
code += "(self):";
code += "(self):"; // TODO: add typing
Copy link
Contributor

Choose a reason for hiding this comment

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

forgotten todo?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not forgotten, just ran out of steam last night. Haha :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ codegen Involving generating code from schema documentation Documentation python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Python] FBS Enums are of underlying type, not of enum type

3 participants