Skip to content

dill.dump() warning and dill.load() error - which dill version is needed? #3

Description

@shortydutchie

When I build a regression model I see following output at the end. It's got some difficulties with pickling a few variable types it appears:

...
...
2026-04-02 13:44:04,667.667 INFO three_step_opt_build_merge - log_scores: All test scores: {'explained_variance': 0.94, 'max_error': -1.249, 'neg_mean_absolute_error': -0.299, 'neg_mean_squared_error': -0.163, 'neg_median_absolute_error': -0.211, 'r2': 0.94, 'corrcoef': 0.971, 'mpe': 1.467, 'mbe': 0.034}
<my_path_to_venv>/lib/python3.12/site-packages/dill/_dill.py:422: PicklingWarning: Cannot locate reference to <enum 'ModelMode'>.
  StockPickler.save(self, obj, save_persistent_id)
<my_path_to_venv>/lib/python3.12/site-packages/dill/_dill.py:422: PicklingWarning: Cannot pickle <enum 'ModelMode'>: optunaz.config.ModelMode has recursive self-references that trigger a RecursionError.
  StockPickler.save(self, obj, save_persistent_id)
<my_path_to_venv>/lib/python3.12/site-packages/dill/_dill.py:422: PicklingWarning: Cannot locate reference to <enum 'OptimizationDirection'>.
  StockPickler.save(self, obj, save_persistent_id)
<my_path_to_venv>/lib/python3.12/site-packages/dill/_dill.py:422: PicklingWarning: Cannot pickle <enum 'OptimizationDirection'>: optunaz.config.OptimizationDirection has recursive self-references that trigger a RecursionError.
  StockPickler.save(self, obj, save_persistent_id)
2026-04-02 13:44:08,779.779 INFO three_step_opt_build_merge - build_merged: Model: <mydir>/bestmodelmerged_Combi_ScldPC+UnscECFP2048r3+Counts_RF_SVR_Lasso_XGB.pkl
...
...

It does write the model pkl file, but loading it results in another error:

with open(fnm,"rb") as f:
    obj = pickle.load(f)  #same error with dill.load()

which results in following error:

Traceback (most recent call last):
  File "<ipython-input-16-09160c4bd099>", line 3, in <module>
    pickle.load(f)
  File "<myvenvpath>/lib/python3.12/site-packages/dill/_dill.py", line 601, in _create_type
    return typeobj(*args)
           ^^^^^^^^^^^^^^
  File "<myhome>/.pyenv/versions/3.12.6/lib/python3.12/enum.py", line 544, in __new__
    member_names = classdict._member_names
                   ^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'dict' object has no attribute '_member_names'

This seems to result from having a dict when expecting an enum (latter has _member_names), so think it is a result of the first pickling errors (I tried dill's dill.settings['recurse'] = True and also the by_ref option - no joy).

It may well be a dill bug - if it all works for you, what dill version do you use?

If I define the enums ModelMode and OptimizationDirection explicitly at the top of optunaz/modelwriter.py, the RecursionError is resolved, but 'PicklingWarning: Cannot locate reference' remains, and I still cannot load the .pkl.
It may be that having the original definitions in an _ init _.py is problematic?

Any thoughts appreciated.
Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions