foundationdb icon indicating copy to clipboard operation
foundationdb copied to clipboard

Add type annotations to fdb Python bindings (PEP 561)

Open sah-rohan opened this issue 6 months ago • 2 comments

Problem

Addresses issue #11331. The fdb Python bindings ship without type annotations and without a py.typed marker. Downstream users get no help from mypy, pyright, or IDEs when calling the public API, and even if annotations existed they would be ignored by PEP 561-compliant type checkers (which require an explicit py.typed file to trust inline annotations from a third-party package).

Solution

  • Add an empty fdb/py.typed marker file and wire it into the sdist/wheel via [tool.setuptools.package-data] in pyproject.toml (PEP 561).
  • Annotate the public surface of fdb/impl.py, fdb/tuple.py, fdb/subspace_impl.py, and fdb/locality.py.
  • Introduce a TupleElement type alias in fdb/tuple.py covering the values the tuple layer can pack (None | bytes | str | int | float | bool | uuid.UUID | SingleFloat | Versionstamp | nested tuple/list), and use it in pack, pack_with_versionstamp, unpack, range, has_incomplete_versionstamp, compare, and across Subspace.
  • Use from __future__ import annotations so all annotations are strings at runtime — zero runtime cost, no import-order issues.

Where Any is intentionally retained

A few annotations remain Any on purpose:

  • Future.wait() / Future.result() return type — Future is a generic base class; concrete subclasses (FutureInt64, FutureString, FutureKeyValueArray, …) already return narrower types.
  • keyToBytes(k) / valueToBytes(v) parameters — duck-typed via the as_foundationdb_key / as_foundationdb_value protocol. Tightening these would require introducing a typing.Protocol; happy to do that in a follow-up.
  • tpointer / fpointer / dpointer constructor params on TransactionRead, Transaction, Future, Database — the codebase passes a mix of int, Optional[int] (from c_void_p().value), and raw ctypes.c_void_p to these. A single accurate type would require touching call sites, which expands scope.
  • *args: Any, **kwargs: Any in Transaction.get_range_startswith — pure forwarding to get_range.

Testing

Ran mypy against both main and this branch using:

sah-rohan avatar May 19 '26 16:05 sah-rohan

lgtm

xis19 avatar Jun 02 '26 02:06 xis19

Hi @xis19 — thanks for the review! Would you be able to submit a formal approval?

sah-rohan avatar Jun 02 '26 21:06 sah-rohan