Skip to content

Latest commit

 

History

History
72 lines (54 loc) · 1.56 KB

File metadata and controls

72 lines (54 loc) · 1.56 KB

Type-Less PyPI

Type Less

Find yourself typing less with automatic type inference for Python! Inject function return types at runtime for code generation 🤙.

FastAPI Example

class User:
    id: int
    first: str
    last: str

@app.get("/user/me")
def user_me(user: User):
    return {
        "id": user.id,
        "balance": 13.37,
        "name": {
            "first": user.first,
            "last": user.last,
        },
    }

🚀 Generates Return Types:

class UserMeName(TypedDict):
    first: str
    last: str

class UserMe(TypedDict):
    id: int
    balance: Literal[13.37]
    name: UserMeName

📋 OpenAPI Spec:

OpenAPI Example

Using in FastAPI Project

Inject types before by hooking before setting up routes. Types will be automatically generated when new routes are added.

from type_less.inject import inject_fastapi_route_types
from fastapi import FastAPI

app = FastAPI()
app = inject_fastapi_route_types(app)

@app.get("/test")
def test(request):
    ...

Testing

  • uv run pytest

TODO:

Add Support:

  • Nested class inference
  • Deep function call / return inference

Better Way?:

  • Possibly use pyright, pyre, mypy, or anything else to infer the type?

Cleanup:

  • Possibly refactor - a large portion of the initial work was codegen from claude and has patterns that need rethinking of proper documentation