-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Python] Render enums as Python IntEnum #8145
Conversation
I got a bit lost on how ImportMapEntry works but as far as i can see its not required to be passed along, but if required i can refactor to pass along the import statement. |
To avoid compatibility issues this should probably only be enabled when |
I thought about that, but this while helping with type checking is in my opinion the correct way to generate an enum in python,
and i could not come up with anyway this would break existing code But if there good arguments against doing this by default im open to move this also behind the |
Flatbuffers currently supports Python 2 (at least it's still advertised in setup.py), while Python enums were added in 3.4. You could question whether Python 2 support should be dropped given that it's EOL, which would be a question for the maintainers, but I'd imagine that breaking changes shouldn't be added until that officially happens.
It's worth noting that the default enums for C++ are the old C-style enums. With the |
75e1226
to
91de368
Compare
@akb825 Fair enough, i missed the Python 2 support its behind the flag for now. |
Thanks! Don't forget to also update the generated code in the tests directory. Another thought is that if you want to get the full benefit of type checking, you will probably also want to adjust the type annotations in the generated code (when Also, full disclosure: I'm not a maintainer and won't be able to approve this PR. The maintainers haven't been very active lately, so it may take some time to get it approved. (I myself noticed this PR when checking to see if there's been any activity lately since I'm waiting on getting a bugfix approved from late May...) |
91de368
to
841f9be
Compare
@dbaileychess what is missing to get this merged? |
841f9be
to
1656ded
Compare
Rebased to latest master, anything else required to get this merged? @dbaileychess |
@dbaileychess Would be wonderful to see this go in |
48fb850
to
dfd66a6
Compare
d64ce3c
to
8eb4508
Compare
f1fcd60
to
5cb1e2f
Compare
Updated and rebased on @anton-bobukh changes - any chance this can be merged? |
I've recently separated the generation of Also, as pointed out above, From what I know about |
good point i kinda assumed that should not be affected but that sounds wrong and i will look into it
It's behind the At least my understanding is none of the typing stuff will work in Python 2.
Can you explain to me how that currently works?
I will look into that 👍 |
My plan was to remove any type annotation from the generated I just realized that you don't change the way enums are read. Currently, we read them as integers (e.g.
ValueError for unknown values (e.g. when the a new enum value is added and the client is updated to send that new value before the server is updated to handle it).
|
This allows enums to be type check with mypy. They will still behave like ints -> > IntEnum is the same as Enum, > but its members are also integers and can be used anywhere > that an integer can be used. > If any integer operation is performed with an IntEnum member, > the resulting value loses its enumeration status. https://docs.python.org/3/library/enum.html#enum.IntEnum Only if the --python-typing flag is set.
5cb1e2f
to
fc4efd6
Compare
I switched it now to put all this info in the |
Hi there, EDIT: Type of the enum is actually "object". |
Fair point with |
This allows enums to be type check with mypy. They will still behave like ints -> > IntEnum is the same as Enum, > but its members are also integers and can be used anywhere > that an integer can be used. > If any integer operation is performed with an IntEnum member, > the resulting value loses its enumeration status. https://docs.python.org/3/library/enum.html#enum.IntEnum Only if the --python-typing flag is set.
This allows enums to be type check with mypy. They will still behave like ints -> > IntEnum is the same as Enum, > but its members are also integers and can be used anywhere > that an integer can be used. > If any integer operation is performed with an IntEnum member, > the resulting value loses its enumeration status. https://docs.python.org/3/library/enum.html#enum.IntEnum Only if the --python-typing flag is set.
This allows enums to be type check with mypy.
They will still behave like ints ->
I think that should be fair enough since an enum is always a int: