Skip to content

Fix Enum type definition #8624

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

Merged
merged 1 commit into from
Jul 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/idl_gen_python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,8 @@ class PythonStubGenerator {
stub << "class " << namer_.Type(*enum_def);
imports->Export(ModuleFor(enum_def), namer_.Type(*enum_def));

imports->Import("typing", "cast");

if (version_.major == 3){
imports->Import("enum", "IntEnum");
stub << "(IntEnum)";
Expand All @@ -559,8 +561,8 @@ class PythonStubGenerator {

stub << ":\n";
for (const EnumVal *val : enum_def->Vals()) {
stub << " " << namer_.Variant(*val) << ": "
<< ScalarType(enum_def->underlying_type.base_type) << "\n";
stub << " " << namer_.Variant(*val) << " = cast("
<< ScalarType(enum_def->underlying_type.base_type) << ", ...)\n";
}

if (parser_.opts.generate_object_based_api & enum_def->is_union) {
Expand Down
7 changes: 4 additions & 3 deletions tests/MyGame/Example/NestedUnion/Any.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import typing
from MyGame.Example.NestedUnion.TestSimpleTableWithEnum import TestSimpleTableWithEnum
from MyGame.Example.NestedUnion.Vec3 import Vec3
from flatbuffers import table
from typing import cast

uoffset: typing.TypeAlias = flatbuffers.number_types.UOffsetTFlags.py_type

class Any(object):
NONE: int
Vec3: int
TestSimpleTableWithEnum: int
NONE = cast(int, ...)
Vec3 = cast(int, ...)
TestSimpleTableWithEnum = cast(int, ...)
def AnyCreator(union_type: typing.Literal[Any.NONE, Any.Vec3, Any.TestSimpleTableWithEnum], table: table.Table) -> typing.Union[None, Vec3, TestSimpleTableWithEnum]: ...

7 changes: 4 additions & 3 deletions tests/MyGame/Example/NestedUnion/Color.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import numpy as np

import flatbuffers
import typing
from typing import cast

uoffset: typing.TypeAlias = flatbuffers.number_types.UOffsetTFlags.py_type

class Color(object):
Red: int
Green: int
Blue: int
Red = cast(int, ...)
Green = cast(int, ...)
Blue = cast(int, ...)

7 changes: 4 additions & 3 deletions tests/MyGame/Example/TestEnum.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import numpy as np

import flatbuffers
import typing
from typing import cast

uoffset: typing.TypeAlias = flatbuffers.number_types.UOffsetTFlags.py_type

class TestEnum(object):
A: int
B: int
C: int
A = cast(int, ...)
B = cast(int, ...)
C = cast(int, ...)

Loading