Skip to content

FIX load dict bool keys as saved - #573

Open
kevincommits wants to merge 1 commit into
skops-dev:mainfrom
kevincommits:fix/dict-bool-keys
Open

kevincommits wants to merge 1 commit into
skops-dev:mainfrom
kevincommits:fix/dict-bool-keys

Conversation

@kevincommits

Copy link
Copy Markdown

Reference Issues/PRs

No existing issue. The new test in #570 notes this as a separate issue.

What does this implement/fix? Explain your changes.

A dict with bool keys does not round trip. The keys are written as the JSON keys "true" and "false", and on load each key is rebuilt as bool("false"), which is True:

from skops.io import dumps, loads

loads(dumps({False: "no", True: "yes"}))
# today:    {True: 'yes'}
# expected: {False: 'no', True: 'yes'}

This silently drops an entry, for example from a class_weight={False: 2, True: 1} hyperparameter. The same happens with numpy.bool_ keys and with files saved under protocols 0 to 2.

The fix rebuilds a bool or numpy.bool_ key by comparing the saved string with "true", in DictNode and in the protocol 2 DictNode. Other key types are unchanged. Since only loading changes, existing files load correctly too.

test_dictionary_bool_keys covers bool and numpy.bool_ keys, and a new case in test_plain_container_v2 covers protocols 0 to 2. I added a changelog entry.

What I ran: the full test suite with pytest in a virtual environment (equivalent of pixi run tests; the only errors are the CatBoost tests, which fail before this change too under a newer pytest), and the pre-commit hooks (ruff check, ruff format, pyrefly), all passing.

What I did not run: the pixi environments for other scikit-learn versions.

Any other comments?

None.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant