Optionally generate Python type annotations (#7858)

* optionally generate type prefixes and suffixes for python code

* fix codegen error when qualified name is empty

* WIP: Python typing

* more progress towards python typing

* Further iterate on Python generated code typing

* clang-format

* Regenerate code

* add documentation for Python type annotations option

* generate code with Python type annotations

* handle forward references

* clang-format
This commit is contained in:
Max Burke
2023-04-28 09:38:29 -07:00
committed by GitHub
parent 6eae49a79a
commit a397dd7e8c
35 changed files with 1295 additions and 979 deletions

View File

@@ -4,17 +4,19 @@
import flatbuffers
from flatbuffers.compat import import_numpy
from typing import Any
from .MyGame.Example.NestedStruct import NestedStruct
np = import_numpy()
class ArrayStruct(object):
__slots__ = ['_tab']
@classmethod
def SizeOf(cls):
def SizeOf(cls) -> int:
return 160
# ArrayStruct
def Init(self, buf, pos):
def Init(self, buf: bytes, pos: int):
self._tab = flatbuffers.table.Table(buf, pos)
# ArrayStruct
@@ -33,28 +35,27 @@ class ArrayStruct(object):
return self._tab.GetArrayAsNumpy(flatbuffers.number_types.Int32Flags, self._tab.Pos + 4, self.BLength())
# ArrayStruct
def BLength(self):
def BLength(self) -> int:
return 15
# ArrayStruct
def BIsNone(self):
def BIsNone(self) -> bool:
return False
# ArrayStruct
def C(self): return self._tab.Get(flatbuffers.number_types.Int8Flags, self._tab.Pos + flatbuffers.number_types.UOffsetTFlags.py_type(64))
# ArrayStruct
def D(self, i: int):
from MyGame.Example.NestedStruct import NestedStruct
def D(self, i: int) -> NestedStruct:
obj = NestedStruct()
obj.Init(self._tab.Bytes, self._tab.Pos + 72 + i * 32)
return obj
# ArrayStruct
def DLength(self):
def DLength(self) -> int:
return 2
# ArrayStruct
def DIsNone(self):
def DIsNone(self) -> bool:
return False
# ArrayStruct
@@ -73,11 +74,11 @@ class ArrayStruct(object):
return self._tab.GetArrayAsNumpy(flatbuffers.number_types.Int64Flags, self._tab.Pos + 144, self.FLength())
# ArrayStruct
def FLength(self):
def FLength(self) -> int:
return 2
# ArrayStruct
def FIsNone(self):
def FIsNone(self) -> bool:
return False