Skip to content

Type Definitions

The typing module contains type definitions and type variables used throughout FastAPI Shield for better type safety and IDE support.

Module Reference

Type definitions and type variables for FastAPI Shield.

This module contains type definitions used throughout the FastAPI Shield library to provide better type safety and documentation.

EndPointFunc = Callable[..., Any] module-attribute

Type alias for FastAPI endpoint functions.

Represents any callable that can serve as a FastAPI endpoint function. This includes both synchronous and asynchronous functions with arbitrary parameters and return types.

Examples:

# Both of these match EndPointFunc
async def async_endpoint(user_id: int) -> dict:
    return {"user_id": user_id}

def sync_endpoint() -> str:
    return "Hello World"

U = TypeVar('U') module-attribute

Generic type variable for shield function signatures.

This type variable is used to preserve the type signature of shield functions when they are wrapped by the Shield decorator, enabling proper type checking and IDE support.

Examples:

def my_shield_func(request: Request) -> Optional[User]:
    # Shield logic here
    pass

# U will be bound to the type of my_shield_func
shield_instance: Shield[U] = Shield(my_shield_func)

Usage

from fastapi_shield.typing import U, EndPointFunc

# U preserves function type signatures
def my_shield_func(request: Request) -> Optional[dict]:
    return {"user": "data"}

# EndPointFunc represents any FastAPI endpoint
def my_endpoint(user_id: int) -> dict:
    return {"user_id": user_id}