Skip to content

23.5.8's Python code generator creates import statements that break existing projects using 23.3.3 or earlier #7951

@stewartmiles

Description

@stewartmiles

#7858 changes the way Python imports are generated from, for example:

from reflection.EnumVal import EnumVal

to:

from .reflection.EnumVal import EnumVal

This breaks packages where the root is the generated flatbuffers package name. For example, we have code that generates Python FlatBuffers code from TensorFlow Lite's schema that places all code into a top-level Python package called tflite i.e given the file structure:

tensorflow_lite_model_schema/pyproject.toml
tensorflow_lite_model_schema/setup.py
tensorflow_lite_model_schema/tflite/  # Generated FlatBuffers code.
tensorflow_lite_model_schema/tflite_model_schema/ # mirror of https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/schema

Where the code is generated using a command line like this:

cd tensorflow_lite_model_schema
flatc --python -o . --gen-object-api -I . tflite_model_schema/schema.fbs

This yields a load of generated files:

ls tensorflow_lite_model_schema/tflite | sort | head -n 5
AbsOptions.py
ActivationFunctionType.py
AddNOptions.py
AddOptions.py
ArgMaxOptions.py

Finally, when importing one of these modules that transitively imports another module the problem pops up:

python -c '
import sys;
sys.path.append(".");
import flatbuffers;
from tflite import Model;
from tflite import OperatorCode;
builder = flatbuffers.builder.Builder();
model_object = Model.ModelT();
model_object.operatorCodes = [OperatorCode.OperatorCodeT()];
builder.Finish(model_object.Pack(builder));
model_flatbuffer = builder.Output();
model = Model.Model.GetRootAs(model_flatbuffer); print(model.OperatorCodes(0))'

produces:

Traceback (most recent call last):
  File "<string>", line 12, in <module>
  File "/home/stewart/src/agentic/service_clean/tensorflow_lite_model_schema/tflite/Model.py", line 45, in OperatorCodes
    from .tflite.OperatorCode import OperatorCode
ModuleNotFoundError: No module named 'tflite.tflite'

@maxburke and @dbaileychess thoughts on a fix?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions