Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better error message: KeyError: "'__name__' not in globals" #967

Closed
ge0ffrey opened this issue Jun 25, 2024 · 1 comment · Fixed by #1081
Closed

Better error message: KeyError: "'__name__' not in globals" #967

ge0ffrey opened this issue Jun 25, 2024 · 1 comment · Fixed by #1081
Assignees
Labels
bug Something isn't working process/needs triage Requires initial assessment of validity, priority etc. python Is related to Python code.

Comments

@ge0ffrey
Copy link
Contributor

This constraint on employee scheduling

def minimize_overtime(constraint_factory: ConstraintFactory):
    return (constraint_factory.for_each(Shift)
            .group_by(lambda shift: shift.employee, lambda shift: shift.start.date() - timedelta(days=shift.start.date().weekday()),
                      lambda shift: ConstraintCollectors.sum(get_shift_duration_in_minutes(shift)))
            .filter(lambda employee, week_start_date, duration: duration > 40 * 60)
            .penalize(HardSoftScore.ONE_SOFT,
                    lambda employee, week_start_date, duration: duration - 40 * 60)
            .as_constraint("Minimize overtime")
            )

doesn't give a good error message

INFO:timefold.solver:Solving started: time spent (33), best score (-2850init/0hard/0soft), environment mode (REPRODUCIBLE), move thread count (NONE), random (JDK with seed 0).
INFO:timefold.solver:Problem scale: entity count (2850), variable count (2850), approximate value count (500), approximate problem scale (1.160145 × 10^7692).
Traceback (most recent call last):
...
  File "python/src/employee_scheduling/main.py", line 18, in timefold
    solution = timefold_solve(problem, time_spent_seconds)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "python/src/employee_scheduling/timefold/solve.py", line 25, in solve
    solution = solver.solve(problem)
               ^^^^^^^^^^^^^^^^^^^^^
  File "/python/.venv/lib/python3.12/site-packages/timefold/solver/_solver.py", line 109, in solve
    java_solution = self._delegate.solve(java_problem)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "python/.venv/lib/python3.12/site-packages/_jpyinterpreter/jvm_setup.py", line 256, in apply
    __import__(module_name, python_globals, python_locals, python_from_list, level),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyError: "'__name__' not in globals"

@ge0ffrey ge0ffrey added bug Something isn't working process/needs triage Requires initial assessment of validity, priority etc. labels Jun 25, 2024
@ge0ffrey
Copy link
Contributor Author

The problem lies in

lambda shift: ConstraintCollectors.sum(get_shift_duration_in_minutes(shift))

that should be

ConstraintCollectors.sum(lambda shift: get_shift_duration_in_minutes(shift)))

@triceo triceo transferred this issue from TimefoldAI/timefold-solver-python Jul 17, 2024
@triceo triceo added java Is related to Java code. python Is related to Python code. and removed java Is related to Java code. labels Jul 17, 2024
Christopher-Chianelli added a commit to Christopher-Chianelli/timefold-solver that referenced this issue Sep 9, 2024
- CPython will check globals for `__name__`, which it uses to determine the absolute package name from a relative package name

  - This means that we need to add the `__name__` entry to all Python global maps, even if `__name__` is not used directly by any function in the module

- Fix default loading for one call signature

- Make `ConstraintCollector.__hash__` raise a TypeError, to give an informative error message

- Make PythonInterpreter importModule return a PythonLikeObject, so C modules can be imported

- Use an WrappedExceptionMeta to rename the WrappedException class returned

Fixes TimefoldAI#967
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working process/needs triage Requires initial assessment of validity, priority etc. python Is related to Python code.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants