Create type hints dynamically with ease
from dyntypes import Codegen
import typing as t
codegen = Codegen()
query_func = codegen.func()
@query_func.bind
def query(statement: str, args: tuple) -> tuple: ...
def generate_types():
query_func.overload(
filename="SELECT * FROM posts WHERE ID = ",
args=tuple[int],
return_type=list[str | int],
)
query_func.overload(filename=str, return_type=t.Never)
codegen.save()
if __name__ == "__main__":
generate_types()Type stubs are a way of adding type hints to files in python, intended for C extensions.
However, there is nothing stopping code from using this to annotate dynamic types on files.
Create a codegen object, this stores all the type information you'll bind to functions
from dyntypes import Codegen
codegen = Codegen()Create a function type object
example_func = codegen.func()
@example_func.bind
def example(bar: str) -> int | None: ...Next we apply overloads to this function, we use the function binding to do this.
I recommend creating a seperate function for type generation so that it can be run conditionally.
def generate_types():
query_func.overload(
filename="SELECT * FROM posts WHERE ID = ",
args=tuple[int],
return_type=list[str | int],
)
query_func.overload(filename=str, return_type=t.Never)
codegen.generate() # Writes out the type stub filesFor example implementations, see the examples folder on GitHub.