Skip to content

Commit 6c65565

Browse files
committed
Fix Enum type definition
Using the : syntax leads to non member attributes. > If an attribute is defined in the class body with a type annotation > but with no assigned value, a type checker should assume this is a non-member attribute ``` class Pet(Enum): genus: str # Non-member attribute species: str # Non-member attribute CAT = 1 # Member attribute DOG = 2 # Member attribute ``` https://typing.python.org/en/latest/spec/enums.html#defining-members
1 parent c7b9dc8 commit 6c65565

File tree

4 files changed

+16
-11
lines changed

4 files changed

+16
-11
lines changed

src/idl_gen_python.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,8 @@ class PythonStubGenerator {
549549
stub << "class " << namer_.Type(*enum_def);
550550
imports->Export(ModuleFor(enum_def), namer_.Type(*enum_def));
551551

552+
imports->Import("typing", "cast");
553+
552554
if (version_.major == 3){
553555
imports->Import("enum", "IntEnum");
554556
stub << "(IntEnum)";
@@ -559,8 +561,8 @@ class PythonStubGenerator {
559561

560562
stub << ":\n";
561563
for (const EnumVal *val : enum_def->Vals()) {
562-
stub << " " << namer_.Variant(*val) << ": "
563-
<< ScalarType(enum_def->underlying_type.base_type) << "\n";
564+
stub << " " << namer_.Variant(*val) << " = cast("
565+
<< ScalarType(enum_def->underlying_type.base_type) << ", ...)\n";
564566
}
565567

566568
if (parser_.opts.generate_object_based_api & enum_def->is_union) {

tests/MyGame/Example/NestedUnion/Any.pyi

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ import typing
88
from MyGame.Example.NestedUnion.TestSimpleTableWithEnum import TestSimpleTableWithEnum
99
from MyGame.Example.NestedUnion.Vec3 import Vec3
1010
from flatbuffers import table
11+
from typing import cast
1112

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

1415
class Any(object):
15-
NONE: int
16-
Vec3: int
17-
TestSimpleTableWithEnum: int
16+
NONE = cast(int, ...)
17+
Vec3 = cast(int, ...)
18+
TestSimpleTableWithEnum = cast(int, ...)
1819
def AnyCreator(union_type: typing.Literal[Any.NONE, Any.Vec3, Any.TestSimpleTableWithEnum], table: table.Table) -> typing.Union[None, Vec3, TestSimpleTableWithEnum]: ...
1920

tests/MyGame/Example/NestedUnion/Color.pyi

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import numpy as np
55

66
import flatbuffers
77
import typing
8+
from typing import cast
89

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

1112
class Color(object):
12-
Red: int
13-
Green: int
14-
Blue: int
13+
Red = cast(int, ...)
14+
Green = cast(int, ...)
15+
Blue = cast(int, ...)
1516

tests/MyGame/Example/TestEnum.pyi

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import numpy as np
55

66
import flatbuffers
77
import typing
8+
from typing import cast
89

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

1112
class TestEnum(object):
12-
A: int
13-
B: int
14-
C: int
13+
A = cast(int, ...)
14+
B = cast(int, ...)
15+
C = cast(int, ...)
1516

0 commit comments

Comments
 (0)