E-SE0008 — Decorated object is not a plain function definition

Error Type

Syntax Error

Phase

defining a problem with the decorator API

Python exception

ValueError (builtin)

Message

The decorator API can only be applied to a plain, non-async function definition.

Possible fix: define the model with `def` (not `async def` or a lambda) and decorate that function.

Cause

The object passed to @problem.update or @jijmodeling.Problem.define is callable, but its source code is not a single, ordinary def function definition. This happens, for example, when decorating a lambda or an async def function.

problem = jm.Problem("My Problem")
problem.update(lambda p: ...)  # a lambda is not a plain `def`

Fix

Write the model-building code as an ordinary, non-async function defined with def, and apply the decorator directly to that definition.

problem = jm.Problem("My Problem")

@problem.update
def build(problem: jm.DecoratedProblem):
    w = problem.Float(ndim=1)
    x = problem.BinaryVar(shape=(w.len_at(0),))
    problem += jm.sum(w * x)