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

[Bug]: Serialization with dictionaries where keys are tuple fails #637

Closed
3 of 4 tasks
Andrew-S-Rosen opened this issue Mar 3, 2024 · 1 comment
Closed
3 of 4 tasks
Labels

Comments

@Andrew-S-Rosen
Copy link
Contributor

Andrew-S-Rosen commented Mar 3, 2024

Email (Optional)

No response

Version

2024.2.26

Which OS(es) are you using?

  • MacOS
  • Windows
  • Linux

What happened?

When trying to JSON serialize (e.g. dumpfn) a dictionary where the keys are of type tuple, an error is raised. This makes sense, because the Python JSON encoder doesn't support this, but perhaps it is worthwhile to consider supporting it in some way through monty.

Potentially related:

Code snippet

from monty.serialization import dumpfn

d = {(0, 0, 0): "hello"}
dumpfn(d, "test.json")

Log output

File ~\miniconda\envs\quacc\Lib\site-packages\monty\serialization.py:127,)    125     if "cls" not in kwargs:
    126         kwargs["cls"] = MontyEncoder
--> 127     fp.write(json.dumps(obj, *args, **kwargs))
    128 else:
    129     raise TypeError(f"Invalid format: {fmt}")

File ~\miniconda\envs\quacc\Lib\json\__init__.py:238, in dumps(obj, skipk)    232 if cls is None:
    233     cls = JSONEncoder
    234 return cls(
    235     skipkeys=skipkeys, ensure_ascii=ensure_ascii,
    236     check_circular=check_circular, allow_nan=allow_nan, indent=in,    237     separators=separators, default=default, sort_keys=sort_keys,
--> 238     **kw).encode(obj)

File ~\miniconda\envs\quacc\Lib\json\encoder.py:200, in JSONEncoder.encode(self, o)
    196         return encode_basestring(o)
    197 # This doesn't pass the iterator directly to ''.join() because the    198 # exceptions aren't as detailed.  The list call should be roughly
    199 # equivalent to the PySequence_Fast that ''.join() would do.
--> 200 chunks = self.iterencode(o, _one_shot=True)
    201 if not isinstance(chunks, (list, tuple)):
    202     chunks = list(chunks)

File ~\miniconda\envs\quacc\Lib\json\encoder.py:258, in JSONEncoder.itere)    253 else:
    254     _iterencode = _make_iterencode(
    255         markers, self.default, _encoder, self.indent, floatstr,
    256         self.key_separator, self.item_separator, self.sort_keys,
    257         self.skipkeys, _one_shot)
--> 258 return _iterencode(o, 0)

TypeError: keys must be str, int, float, bool or None, not tuple

Code of Conduct

  • I agree to follow this project's Code of Conduct
@Andrew-S-Rosen
Copy link
Contributor Author

Probably easier to just jsanitize this one beforehand I suppose.

@Andrew-S-Rosen Andrew-S-Rosen closed this as not planned Won't fix, can't repro, duplicate, stale Mar 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant