timefold-solver icon indicating copy to clipboard operation
timefold-solver copied to clipboard

Error message for error in constraint provider must mention the line

Open ge0ffrey opened this issue 1 year ago • 1 comments

What line did this happen on?

Traceback (most recent call last):
  File "/home/ge0ffrey/projects/tf/tf-main/timefold-quickstarts/python/employee-scheduling/.venv/bin/run-app", line 5, in <module>
    from employee_scheduling import main
  File "/home/ge0ffrey/projects/tf/tf-main/timefold-quickstarts/python/employee-scheduling/src/employee_scheduling/__init__.py", line 3, in <module>
    from .routes import app
  File "/home/ge0ffrey/projects/tf/tf-main/timefold-quickstarts/python/employee-scheduling/src/employee_scheduling/routes.py", line 23, in <module>
    solver_manager = SolverManager.create(SolverFactory.create(solver_config))
                                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ge0ffrey/projects/tf/tf-main/timefold-quickstarts/python/employee-scheduling/.venv/lib64/python3.11/site-packages/timefold/solver/_solver_factory.py", line 52, in create
    delegate = JavaSolverFactory.create(solver_config)  # noqa
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
NameError: name 'Availability' is not defined

I suspect line 75 in constraints.py:

def unavailable_employee(constraint_factory: ConstraintFactory):
    return (constraint_factory.for_each(Shift)
            .join(Availability,

The error message dosn't mention "line 75" nor the filename.

ge0ffrey avatar Jun 18 '24 13:06 ge0ffrey

I think this is related to https://github.com/jpype-project/jpype/pull/1057

How a ConstraintProvider is constructed go through these steps:

  • Python calls Java SolverFactory to create a Solver
  • Java SolverFactory create an instance of the generated ConstraintProvider class and call its defineConstraints method
  • defineConstraints call into Python to compile the Python functions used in constraints and return to Java

The Python code in defineConstraints throws a name error since Availability is not in globals (missed import or removed class). This exception then goes to Java, which replaces its stack trace with its own.

Christopher-Chianelli avatar Jun 19 '24 17:06 Christopher-Chianelli