lidatong / dataclasses-json

Easily serialize Data Classes to and from JSON
MIT License
1.34k stars 150 forks source link

[minimal-repro-testcase] from v0.5.12 on, tuple fields get truncated #491

Closed simlei closed 7 months ago

simlei commented 9 months ago

Description

From v0.5.12 on, tuples get (sometimes?) truncated when deserializing. I think this is a major issue since there is no exception thrown; your data is just garbled. This has huge potential to make make whole data recording sessions useless as you only notice it when your data looks funny afterwards.

Everything works fine in v0.5.9; from v0.5.12, not.

This project is one of my absolute favorites by the way and thank you for it.

Serializing from tuples instead of lists is IMO a very common use case, as dataclass instances with lists cannot be hashable, i.e. cannot be used as a key in dictionaries for efficient lookup.

Code snippet that reproduces the issue

from dataclasses import dataclass
from dataclasses_json import dataclass_json, LetterCase, Undefined
from typing import Tuple

@dataclass_json(letter_case=LetterCase.CAMEL, undefined=Undefined.EXCLUDE)
@dataclass(frozen=True, eq=True)
class MyClass:
    path: Tuple[int]

instance = MyClass((0, 1, 0))
asJson = instance.to_json()
fromJson = MyClass.from_json(asJson)
assert fromJson.path == (0,1,0)

See also my pip environment which is not huge.

Describe the results you expected

That it just works...

Python version you are using

3.11.4

Environment description

boltons @ file:///work/ci_py311/boltons_1677685195580/work brotlipy==0.7.0 certifi @ file:///croot/certifi_1683875369620/work/certifi cffi @ file:///work/ci_py311/cffi_1676822533496/work charset-normalizer @ file:///tmp/build/80754af9/charset-normalizer_1630003229654/work conda @ file:///croot/conda_1689269889729/work conda-content-trust @ file:///work/ci_py311/conda-content-trust_1676851327497/work conda-libmamba-solver @ file:///croot/conda-libmamba-solver_1685032319139/work/src conda-package-handling @ file:///croot/conda-package-handling_1685024767917/work conda_package_streaming @ file:///croot/conda-package-streaming_1685019673878/work cryptography @ file:///croot/cryptography_1686613057838/work dataclasses-json==0.6.1 idna @ file:///work/ci_py311/idna_1676822698822/work jsonpatch @ file:///tmp/build/80754af9/jsonpatch_1615747632069/work jsonpointer==2.1 libmambapy @ file:///croot/mamba-split_1685993156657/work/libmambapy marshmallow==3.20.1 marshmallow-enum==1.5.1 mypy-extensions==1.0.0 packaging @ file:///croot/packaging_1678965309396/work pluggy @ file:///work/ci_py311/pluggy_1676822818071/work pycosat @ file:///work/ci_py311/pycosat_1676838522308/work pycparser @ file:///tmp/build/80754af9/pycparser_1636541352034/work pyOpenSSL @ file:///croot/pyopenssl_1678965284384/work PySocks @ file:///work/ci_py311/pysocks_1676822712504/work requests @ file:///croot/requests_1682607517574/work ruamel.yaml @ file:///work/ci_py311/ruamel.yaml_1676838772170/work six @ file:///tmp/build/80754af9/six_1644875935023/work tabulate==0.9.0 toolz @ file:///work/ci_py311/toolz_1676827522705/work tqdm @ file:///croot/tqdm_1679561862951/work typing-inspect==0.9.0 typing_extensions==4.8.0 urllib3 @ file:///croot/urllib3_1686163155763/work zstandard @ file:///work/ci_py311_2/zstandard_1679339489613/work

george-zubrienko commented 8 months ago

@matt035343 I'll try to run the repro case tomorrow, but maybe you have an idea why this happens since you worked with tuple ser recently?

george-zubrienko commented 8 months ago

Confirmed - linked a PR that should fix this.

george-zubrienko commented 8 months ago

Circling back here - had a break from OSS due to high load at work. Will resume next week. Really sorry folks

simlei commented 7 months ago

Great !! 💯 really appreciate it

On Thu, 9 Nov 2023, 19:55 George Zubrienko @.***> wrote:

Closed #491 https://github.com/lidatong/dataclasses-json/issues/491 as completed via #493 https://github.com/lidatong/dataclasses-json/pull/493 .

— Reply to this email directly, view it on GitHub https://github.com/lidatong/dataclasses-json/issues/491#event-10916889849, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAHP22ZZD565YYZA6TVLT6DYDURJTAVCNFSM6AAAAAA5W36HKWVHI2DSMVQWIX3LMV45UABCJFZXG5LFIV3GK3TUJZXXI2LGNFRWC5DJN5XDWMJQHEYTMOBYHE4DIOI . You are receiving this because you authored the thread.Message ID: @.***>

simlei commented 7 months ago

Btw, looking over the PR, I realize that Tuple[int] actually is ambiguous. In Scala e.g. you couldn't do this. But it's really useful still for hinting IDEs to the correct member type, and as stated, tuple is hashable which I think is a common requirement

On Thu, 9 Nov 2023, 20:59 Simon Leischnig @.***> wrote:

Great !! 💯 really appreciate it

On Thu, 9 Nov 2023, 19:55 George Zubrienko @.***> wrote:

Closed #491 https://github.com/lidatong/dataclasses-json/issues/491 as completed via #493 https://github.com/lidatong/dataclasses-json/pull/493.

— Reply to this email directly, view it on GitHub https://github.com/lidatong/dataclasses-json/issues/491#event-10916889849, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAHP22ZZD565YYZA6TVLT6DYDURJTAVCNFSM6AAAAAA5W36HKWVHI2DSMVQWIX3LMV45UABCJFZXG5LFIV3GK3TUJZXXI2LGNFRWC5DJN5XDWMJQHEYTMOBYHE4DIOI . You are receiving this because you authored the thread.Message ID: @.***>