Skip to content

check function signature for python endpoints #33

@tom-010

Description

@tom-010

it is fine if you dont want to type python. But tying the fastapi endpoints is important for ts typesafety.
We can add a checking script like this

import inspect
import sys
from fastapi.routing import APIRoute

from app.main import app 

def check_endpoint_signatures():
    violations = []
    
    for route in app.routes:
        # We only care about user-defined API routes, not internal router logic
        if isinstance(route, APIRoute):
            endpoint = route.endpoint
            name = f"{endpoint.__module__}.{endpoint.__name__}"
            sig = inspect.signature(endpoint)

            # 1. Check Return Type
            if sig.return_annotation is inspect.Signature.empty:
                violations.append(f"{name}: Missing return type annotation")

            # 2. Check Arguments
            for param_name, param in sig.parameters.items():
                if param_name == "self": continue # Skip self for class-based views
                if param.annotation is inspect.Signature.empty:
                    violations.append(f"{name}: Argument '{param_name}' missing type annotation")

    if violations:
        print("\n".join(violations))
        sys.exit(1)

if __name__ == "__main__":
    check_endpoint_signatures()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions