Files
flatbuffers/tests/MyGame/MonsterExtra.pyi
Łukasz Kurowski c526cb640b [Python] Enhance object API __init__ with typed keyword arguments (#8615)
This commit significantly improves the developer experience for the Python Object-Based API by overhauling the generated `__init__` method for `T`-suffixed classes.

Previously, `T` objects had to be instantiated with an empty constructor, and their fields had to be populated manually one by one. This was verbose and not idiomatic Python.

This change modifies the Python code generator (`GenInitialize`) to produce `__init__` methods that are:

1.  **Keyword-Argument-Friendly**: The constructor now accepts all table/struct fields as keyword arguments, allowing for concise, single-line object creation.

2.  **Fully Typed**: The signature of the `__init__` method is now annotated with Python type hints. This provides immediate benefits for static analysis tools (like Mypy) and IDEs, enabling better autocompletion and type checking.

3.  **Correctly Optional**: The generator now correctly wraps types in `Optional[...]` if their default value is `None`. This applies to strings, vectors, and other nullable fields, ensuring strict type safety.

The new approach remains **fully backward-compatible**, as all arguments have default values. Existing code that uses the empty constructor will continue to work without modification.

#### Example of a Generated `__init__`

**Before:**

```python
class KeyValueT(object):
    def __init__(self):
        self.key = None  # type: str
        self.value = None  # type: str
```

**After:**

```python
class KeyValueT(object):
    def __init__(self, key: Optional[str] = None, value: Optional[str] = None):
        self.key = key
        self.value = value
```

#### Example of User Code

**Before:**

```python
# Old, verbose way
kv = KeyValueT()
kv.key = "instrument"
kv.value = "EUR/USD"
```

**After:**

```python
# New, Pythonic way
kv = KeyValueT(key="instrument", value="EUR/USD")
```
2025-07-22 23:57:39 -07:00

86 lines
3.3 KiB
Python

from __future__ import annotations
import flatbuffers
import numpy as np
import typing
uoffset: typing.TypeAlias = flatbuffers.number_types.UOffsetTFlags.py_type
class MonsterExtra(object):
@classmethod
def GetRootAs(cls, buf: bytes, offset: int) -> MonsterExtra: ...
@classmethod
def GetRootAsMonsterExtra(cls, buf: bytes, offset: int) -> MonsterExtra: ...
@classmethod
def MonsterExtraBufferHasIdentifier(cls, buf: bytes, offset: int, size_prefixed: bool) -> bool: ...
def Init(self, buf: bytes, pos: int) -> None: ...
def D0(self) -> float: ...
def D1(self) -> float: ...
def D2(self) -> float: ...
def D3(self) -> float: ...
def F0(self) -> float: ...
def F1(self) -> float: ...
def F2(self) -> float: ...
def F3(self) -> float: ...
def Dvec(self, i: int) -> typing.List[float]: ...
def DvecAsNumpy(self) -> np.ndarray: ...
def DvecLength(self) -> int: ...
def DvecIsNone(self) -> bool: ...
def Fvec(self, i: int) -> typing.List[float]: ...
def FvecAsNumpy(self) -> np.ndarray: ...
def FvecLength(self) -> int: ...
def FvecIsNone(self) -> bool: ...
class MonsterExtraT(object):
d0: float
d1: float
d2: float
d3: float
f0: float
f1: float
f2: float
f3: float
dvec: typing.List[float]
fvec: typing.List[float]
def __init__(
self,
d0: float = ...,
d1: float = ...,
d2: float = ...,
d3: float = ...,
f0: float = ...,
f1: float = ...,
f2: float = ...,
f3: float = ...,
dvec: typing.List[float] | None = ...,
fvec: typing.List[float] | None = ...,
) -> None: ...
@classmethod
def InitFromBuf(cls, buf: bytes, pos: int) -> MonsterExtraT: ...
@classmethod
def InitFromPackedBuf(cls, buf: bytes, pos: int = 0) -> MonsterExtraT: ...
@classmethod
def InitFromObj(cls, monsterExtra: MonsterExtra) -> MonsterExtraT: ...
def _UnPack(self, monsterExtra: MonsterExtra) -> None: ...
def Pack(self, builder: flatbuffers.Builder) -> None: ...
def __eq__(self, other: MonsterExtraT) -> bool: ...
def MonsterExtraStart(builder: flatbuffers.Builder) -> None: ...
def Start(builder: flatbuffers.Builder) -> None: ...
def MonsterExtraAddD0(builder: flatbuffers.Builder, d0: float) -> None: ...
def MonsterExtraAddD1(builder: flatbuffers.Builder, d1: float) -> None: ...
def MonsterExtraAddD2(builder: flatbuffers.Builder, d2: float) -> None: ...
def MonsterExtraAddD3(builder: flatbuffers.Builder, d3: float) -> None: ...
def MonsterExtraAddF0(builder: flatbuffers.Builder, f0: float) -> None: ...
def MonsterExtraAddF1(builder: flatbuffers.Builder, f1: float) -> None: ...
def MonsterExtraAddF2(builder: flatbuffers.Builder, f2: float) -> None: ...
def MonsterExtraAddF3(builder: flatbuffers.Builder, f3: float) -> None: ...
def MonsterExtraAddDvec(builder: flatbuffers.Builder, dvec: uoffset) -> None: ...
def MonsterExtraStartDvecVector(builder: flatbuffers.Builder, num_elems: int) -> uoffset: ...
def StartDvecVector(builder: flatbuffers.Builder, num_elems: int) -> uoffset: ...
def MonsterExtraAddFvec(builder: flatbuffers.Builder, fvec: uoffset) -> None: ...
def MonsterExtraStartFvecVector(builder: flatbuffers.Builder, num_elems: int) -> uoffset: ...
def StartFvecVector(builder: flatbuffers.Builder, num_elems: int) -> uoffset: ...
def MonsterExtraEnd(builder: flatbuffers.Builder) -> uoffset: ...
def End(builder: flatbuffers.Builder) -> uoffset: ...