foundationdb
foundationdb copied to clipboard
Add type annotations to fdb Python bindings (PEP 561)
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.typedmarker file and wire it into the sdist/wheel via[tool.setuptools.package-data]inpyproject.toml(PEP 561). - Annotate the public surface of
fdb/impl.py,fdb/tuple.py,fdb/subspace_impl.py, andfdb/locality.py. - Introduce a
TupleElementtype alias infdb/tuple.pycovering the values the tuple layer can pack (None | bytes | str | int | float | bool | uuid.UUID | SingleFloat | Versionstamp | nested tuple/list), and use it inpack,pack_with_versionstamp,unpack,range,has_incomplete_versionstamp,compare, and acrossSubspace. - Use
from __future__ import annotationsso 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 —Futureis a generic base class; concrete subclasses (FutureInt64,FutureString,FutureKeyValueArray, …) already return narrower types.keyToBytes(k)/valueToBytes(v)parameters — duck-typed via theas_foundationdb_key/as_foundationdb_valueprotocol. Tightening these would require introducing atyping.Protocol; happy to do that in a follow-up.tpointer/fpointer/dpointerconstructor params onTransactionRead,Transaction,Future,Database— the codebase passes a mix ofint,Optional[int](fromc_void_p().value), and rawctypes.c_void_pto these. A single accurate type would require touching call sites, which expands scope.*args: Any, **kwargs: AnyinTransaction.get_range_startswith— pure forwarding toget_range.
Testing
Ran mypy against both main and this branch using:
lgtm
Hi @xis19 — thanks for the review! Would you be able to submit a formal approval?