constraints

exception litestar.middleware.constraints.ConstraintViolationError

Bases: MiddlewareConstraintError

exception litestar.middleware.constraints.CycleError

Bases: MiddlewareConstraintError

exception litestar.middleware.constraints.MiddlewareConstraintError

Bases: LitestarException

class litestar.middleware.constraints.MiddlewareConstraints

Bases: object

Constraints for a middleware.

before: tuple['MiddlewareForwardRef | Middleware | MiddlewareFactory', ...] = ()

Tuple of middlewares that, if present, need to appear before the middleware this constraint is applied to (i.e. closer to the application)

after: tuple['MiddlewareForwardRef | Middleware | MiddlewareFactory', ...] = ()

Tuple of middlewares that, if present, need to appear after the middleware this constraint is applied to (i.e. closer to the handler)

first: bool = False

If True, require the middleware to be the first (i.e. the first middleware on the application).

Mutually exclusive with last=True. Implicitly sets unique=True

last: bool = False

If True, require the middleware to be the last (i.e. the last middleware on the handler).

Mutually exclusive with first=True. Implicitly sets unique=True

unique: bool | None = None

If True, require the middleware to be the only one of its type

require_unique(unique: bool) Self

Return a new constraint with a unique value set

apply_first() Self

Return a new constraint with first=True. Overrides last=True

apply_last() Self

Return a new constraint with first=True. Overrides first=True

apply_before(other: str | Middleware | MiddlewareFactory | MiddlewareForwardRef, ignore_import_error: bool = False) Self

Return new MiddlewareConstraints with other added to existing before constraint.

Parameters:
  • other – Middleware this middleware needs to be applied before. If passed a string, create a MiddlewareForwardRef that resolves to the actual middleware at runtime

  • ignore_import_error – If True and other is a string, ignore the constraint if an ImportError occurs when trying to import it

apply_after(other: str | Middleware | MiddlewareFactory | MiddlewareForwardRef, ignore_import_error: bool = False) Self

Return new MiddlewareConstraints with other added to existing after constraint.

Parameters:
  • other – Middleware this middleware needs to be applied before. If passed a string, create a MiddlewareForwardRef that resolves to the actual middleware at runtime

  • ignore_import_error – If True and other is a string, ignore the constraint if an ImportError occurs when trying to import it

__init__(before: tuple['MiddlewareForwardRef | Middleware | MiddlewareFactory', ...] = (), after: tuple['MiddlewareForwardRef | Middleware | MiddlewareFactory', ...] = (), first: bool = False, last: bool = False, unique: bool | None = None) None
class litestar.middleware.constraints.MiddlewareForwardRef

Bases: object

Forward reference to a middleware

target: str

Absolute path to an importable name of the middleware

ignore_import_error: bool

If ‘True’, ignore ImportErrors will be ignored when resolving the middleware

resolve() Middleware | None

Resolve the reference to a concrete value by importing the target path.

If ignore_import_error=True and an ImportError is raised, ignore the error and return None

__init__(target: str, ignore_import_error: bool) None