Skip to content

Commit 3335fbc

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 31beb0f commit 3335fbc

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
@@ -546,6 +546,8 @@ class PythonStubGenerator {
546546
Imports *imports) const {
547547
stub << "class " << namer_.Type(*enum_def);
548548

549+
imports->Import("typing", "cast");
550+
549551
if (version_.major == 3){
550552
imports->Import("enum", "IntEnum");
551553
stub << "(IntEnum)";
@@ -556,8 +558,8 @@ class PythonStubGenerator {
556558

557559
stub << ":\n";
558560
for (const EnumVal *val : enum_def->Vals()) {
559-
stub << " " << namer_.Variant(*val) << ": "
560-
<< ScalarType(enum_def->underlying_type.base_type) << "\n";
561+
stub << " " << namer_.Variant(*val) << " = cast("
562+
<< ScalarType(enum_def->underlying_type.base_type) << ", ...)\n";
561563
}
562564

563565
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
@@ -9,12 +9,13 @@ from MyGame.Example.NestedUnion.Any import Any
99
from MyGame.Example.NestedUnion.TestSimpleTableWithEnum import TestSimpleTableWithEnum
1010
from MyGame.Example.NestedUnion.Vec3 import Vec3
1111
from flatbuffers import table
12+
from typing import cast
1213

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

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

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)