Skip to content

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 👍

if (version_.major == 3) {
imports->Import("enum", "IntEnum");
stub << "(IntEnum)";
if (parser_.opts.python_enum) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Ahh i finally got it to work on my project and i spotted an other bug if you just limit to python_enum here you will break for people who enabled just --python-typing

enum Symbology : uint { DEFAULT = 0x00, NO_READ = 0x02, } table Barcode { symbology: Symbology; value: string; } 

will generate

def Symbology(self) -> typing.Literal[Symbology.DEFAULT, Symbology.NO_READ] 

which only works if Symbology is a enum in python.

This should be addressed before we merge this that without the python_enum the old typing code is generated, and with the new code with better types.

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

2 participants