Skip to content

fix: handle mixed-type sets in best-effort JSON - #80

Open
rioyu123 wants to merge 1 commit into
google:masterfrom
rioyu123:fix/best-effort-json-mixed-sets
Open

rioyu123 wants to merge 1 commit into
google:masterfrom
rioyu123:fix/best-effort-json-mixed-sets

Conversation

@rioyu123

@rioyu123 rioyu123 commented Oct 3, 2026

Copy link
Copy Markdown

ConfigDict.to_json_best_effort() is intended to serialize what it can without raising. However, a set such as {1, 'one'} or {None, 3} raises TypeError because its elements cannot be sorted against each other. This can prevent an otherwise valid experiment configuration from being exported.

This change keeps the existing sort for comparable elements and falls back to sorting by repr when that raises TypeError. The output still contains the original element values, not their string representations. No stable ordering is promised for arbitrary custom objects, and strict to_json() conversion is unchanged.

Tests

  • Added cases for int/string and None/int sets, a nested mixed set, and unchanged numeric sorting. Before the fix, the three mixed-set cases fail and the numeric control passes; all four pass with the fix.
  • Linux, Python 3.12.3: pytest -q -n 2 ml_collections/ --ignore=ml_collections/config_dict/examples/examples_test.py — 393 passed, 1 skipped.
  • The separately run example tests also pass: 7 passed.
  • Changed-line Pyink check and git diff --check pass.

The Windows core run retains one testFlagfile failure that also occurs on the unmodified base; the Linux runs above pass. The other Linux Python versions in CI were not run locally.

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