Fix Enum type definition (#8624)

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
This commit is contained in:
Felix
2025-07-16 21:22:45 +02:00
committed by GitHub
parent 07c2eb5fe7
commit 1047d7ec13
4 changed files with 16 additions and 11 deletions

View File

@@ -549,6 +549,8 @@ class PythonStubGenerator {
stub << "class " << namer_.Type(*enum_def); stub << "class " << namer_.Type(*enum_def);
imports->Export(ModuleFor(enum_def), namer_.Type(*enum_def)); imports->Export(ModuleFor(enum_def), namer_.Type(*enum_def));
imports->Import("typing", "cast");
if (version_.major == 3){ if (version_.major == 3){
imports->Import("enum", "IntEnum"); imports->Import("enum", "IntEnum");
stub << "(IntEnum)"; stub << "(IntEnum)";
@@ -559,8 +561,8 @@ class PythonStubGenerator {
stub << ":\n"; stub << ":\n";
for (const EnumVal *val : enum_def->Vals()) { for (const EnumVal *val : enum_def->Vals()) {
stub << " " << namer_.Variant(*val) << ": " stub << " " << namer_.Variant(*val) << " = cast("
<< ScalarType(enum_def->underlying_type.base_type) << "\n"; << ScalarType(enum_def->underlying_type.base_type) << ", ...)\n";
} }
if (parser_.opts.generate_object_based_api & enum_def->is_union) { if (parser_.opts.generate_object_based_api & enum_def->is_union) {

View File

@@ -8,12 +8,13 @@ import typing
from MyGame.Example.NestedUnion.TestSimpleTableWithEnum import TestSimpleTableWithEnum from MyGame.Example.NestedUnion.TestSimpleTableWithEnum import TestSimpleTableWithEnum
from MyGame.Example.NestedUnion.Vec3 import Vec3 from MyGame.Example.NestedUnion.Vec3 import Vec3
from flatbuffers import table from flatbuffers import table
from typing import cast
uoffset: typing.TypeAlias = flatbuffers.number_types.UOffsetTFlags.py_type uoffset: typing.TypeAlias = flatbuffers.number_types.UOffsetTFlags.py_type
class Any(object): class Any(object):
NONE: int NONE = cast(int, ...)
Vec3: int Vec3 = cast(int, ...)
TestSimpleTableWithEnum: int TestSimpleTableWithEnum = cast(int, ...)
def AnyCreator(union_type: typing.Literal[Any.NONE, Any.Vec3, Any.TestSimpleTableWithEnum], table: table.Table) -> typing.Union[None, Vec3, TestSimpleTableWithEnum]: ... def AnyCreator(union_type: typing.Literal[Any.NONE, Any.Vec3, Any.TestSimpleTableWithEnum], table: table.Table) -> typing.Union[None, Vec3, TestSimpleTableWithEnum]: ...

View File

@@ -5,11 +5,12 @@ import numpy as np
import flatbuffers import flatbuffers
import typing import typing
from typing import cast
uoffset: typing.TypeAlias = flatbuffers.number_types.UOffsetTFlags.py_type uoffset: typing.TypeAlias = flatbuffers.number_types.UOffsetTFlags.py_type
class Color(object): class Color(object):
Red: int Red = cast(int, ...)
Green: int Green = cast(int, ...)
Blue: int Blue = cast(int, ...)

View File

@@ -5,11 +5,12 @@ import numpy as np
import flatbuffers import flatbuffers
import typing import typing
from typing import cast
uoffset: typing.TypeAlias = flatbuffers.number_types.UOffsetTFlags.py_type uoffset: typing.TypeAlias = flatbuffers.number_types.UOffsetTFlags.py_type
class TestEnum(object): class TestEnum(object):
A: int A = cast(int, ...)
B: int B = cast(int, ...)
C: int C = cast(int, ...)