Skip to content

[Python] Generated type stubs don't work #9224

Description

@MKuranowski

Given a very simple FlatBuffer definition:

table Foo {
    bar: string;
    baz: int64;
}

root_type Foo;

The generated type stubs (flatc --python --python-typing foo.fbs) include an incorrect alias to uoffset:

from __future__ import annotations

import flatbuffers
import numpy as np

import typing

uoffset: typing.TypeAlias = flatbuffers.number_types.UOffsetTFlags.py_type
  1. The corresponding Foo.py file does not define an uoffset alias.
    >>> from Foo import uoffset
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
        from Foo import uoffset
    ImportError: cannot import name 'uoffset' from 'Foo' (/.../Foo.py)
  2. flatbuffers.number_types is not accessible with a simple import flatbuffers, as flatbuffers/__init__.py doesn't re-export number_types. An explicit import flatbuffers.number_types is needed.
  3. The stub adds an unconditional import numpy as np, which breaks type checking when numpy is not installed.

Tested with Python 3.14.7, Pyright 1.1.411 and flatbuffers/flatc 25.12.19.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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