lidatong/dataclasses-json

[BUG] Using `HexBytes` results in a bug when deserialising

Opened this issue · 0 comments

Description

When using HexBytes as part of the dataclass, the code in the example is failing to deserialise with the following error:

raise TypeError(f"Cannot convert {val!r} of type {type(val)} to bytes")

TypeError: Cannot convert [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] of type <class 'list'> to bytes

Code snippet that reproduces the issue

from dataclasses_json import dataclass_json
from dataclasses import dataclass

from hexbytes import HexBytes


@dataclass_json
@dataclass
class Example:
    name: str
    hex_bytes: HexBytes = HexBytes("0x0000000000000000000000000000000000000000000000000000000000000000")
    
    
Example.from_dict({"name": "Example"})

Describe the results you expected

Deserialisation works

Python version you are using

Python 3.11.7

Environment description

aiohttp==3.9.5
aiosignal==1.3.1
alabaster==0.7.16
anyio==4.4.0
appnope==0.1.4
argon2-cffi==23.1.0
argon2-cffi-bindings==21.2.0
arrow==1.3.0
asn1crypto==1.5.1
asttokens==2.4.1
async-lru==2.0.4
attrs==23.2.0
Babel==2.15.0
beautifulsoup4==4.12.3
bitarray==2.9.2
black==22.12.0
bleach==6.1.0
blinker==1.8.2
cachetools==5.4.0
certifi==2024.7.4
cffi==1.16.0
cfgv==3.4.0
charset-normalizer==3.3.2
ckzg==1.0.2
click==8.1.7
cloud-sql-python-connector==1.11.0
comm==0.2.2
contourpy==1.2.0
coverage==5.5
cryptography==43.0.0
cycler==0.12.1
Cython==3.0.10
cytoolz==0.12.3
darglint==1.8.1
dash==2.17.1
dash-bootstrap-components==1.6.0
dash-core-components==2.0.0
dash-html-components==2.0.0
dash-mantine-components==0.12.1
dash-table==5.0.0
dataclasses-json==0.6.7
debugpy==1.8.1
decorator==5.1.1
defusedxml==0.7.1
distlib==0.3.8
docformatter==1.7.5
eth-account==0.11.2
eth-hash==0.7.0
eth-keyfile==0.8.1
eth-keys==0.5.1
eth-rlp==1.0.1
eth-typing==4.4.0
eth-utils==4.1.1
eth_abi==5.1.0
execnet==2.1.1
executing==2.0.1
fastjsonschema==2.20.0
filelock==3.15.4
flake8==7.1.0
Flask==3.0.3
fonttools==4.49.0
fqdn==1.5.1
frozendict==2.4.4
frozenlist==1.4.1
furo==2024.7.18
google-api-core==2.19.1
google-auth==2.32.0
google-cloud-appengine-logging==1.4.3
google-cloud-audit-log==0.2.5
google-cloud-bigquery==3.25.0
google-cloud-core==2.4.1
google-cloud-logging==3.10.0
google-cloud-monitoring==2.22.1
google-cloud-storage==2.18.0
google-crc32c==1.5.0
google-resumable-media==2.7.1
googleapis-common-protos==1.63.2
gprof2dot==2024.6.6
grpc-google-iam-v1==0.13.0
grpcio==1.65.1
grpcio-status==1.65.1
h11==0.14.0
hexbytes==0.3.1
httpcore==1.0.5
httpx==0.27.0
identify==2.6.0
idna==3.7
imagesize==1.4.1
importlib_metadata==8.2.0
iniconfig==2.0.0
ipykernel==6.29.4
ipython==8.25.0
ipywidgets==8.1.3
isoduration==20.11.0
isort==5.13.2
itsdangerous==2.2.0
jedi==0.19.1
Jinja2==3.1.4
joblib==1.4.2
json5==0.9.25
jsonpointer==3.0.0
jsons==1.6.3
jsonschema==4.23.0
jsonschema-specifications==2023.12.1
jupyter==1.0.0
jupyter-console==6.6.3
jupyter-events==0.10.0
jupyter-lsp==2.2.5
jupyter_client==8.6.2
jupyter_core==5.7.2
jupyter_server==2.14.1
jupyter_server_terminals==0.5.3
jupyterlab==4.2.2
jupyterlab_pygments==0.3.0
jupyterlab_server==2.27.2
jupyterlab_widgets==3.0.11
kiwisolver==1.4.5
lru-dict==1.2.0
MarkupSafe==2.1.5
marshmallow==3.21.3
matplotlib==3.8.3
matplotlib-inline==0.1.7
mccabe==0.7.0
mistune==3.0.2
multidict==6.0.5
mypy==1.11.0
mypy-extensions==1.0.0
nbclient==0.10.0
nbconvert==7.16.4
nbformat==5.10.4
nest-asyncio==1.6.0
nodeenv==1.9.1
notebook==7.2.1
notebook_shim==0.2.4
numpy==1.26.4
overrides==7.7.0
packaging==24.1
pandas==2.2.2
pandocfilters==1.5.1
parsimonious==0.10.0
parso==0.8.4
pathlib==1.0.1
pathspec==0.12.1
pexpect==4.9.0
pg8000==1.31.2
pillow==10.2.0
platformdirs==4.2.2
plotly==5.23.0
pluggy==1.5.0
pre-commit==2.21.0
prometheus_client==0.20.0
prompt_toolkit==3.0.47
proto-plus==1.24.0
protobuf==5.27.2
psutil==6.0.0
ptyprocess==0.7.0
pure-eval==0.2.2
py==1.11.0
py-machineid==0.3.0
py-solc-x==1.1.1
pyasn1==0.6.0
pyasn1_modules==0.4.0
pycodestyle==2.12.0
pycparser==2.22
pycryptodome==3.20.0
pydocstyle==6.3.0
pyflakes==3.2.0
Pygments==2.18.0
pyparsing==3.1.2
pytest==8.3.2
pytest-cov==2.12.1
pytest-xdist==3.6.1
python-dateutil==2.9.0.post0
python-dotenv==1.0.1
python-json-logger==2.0.7
pytz==2023.4
pyunormalize==15.1.0
PyYAML==6.0.1
pyzmq==26.0.3
qtconsole==5.5.2
QtPy==2.4.1
referencing==0.35.1
regex==2024.7.24
requests==2.32.3
retrying==1.3.4
rfc3339-validator==0.1.4
rfc3986-validator==0.1.1
rlp==4.0.1
rpds-py==0.19.1
rsa==4.9
scramp==1.4.5
semantic-version==2.10.0
Send2Trash==1.8.3
six==1.16.0
sniffio==1.3.1
snowballstemmer==2.2.0
soupsieve==2.5
Sphinx==7.4.7
sphinx-basic-ng==1.0.0b2
sphinx-multiversion==0.2.4
sphinx-rtd-theme==1.3.0
sphinxcontrib-applehelp==1.0.8
sphinxcontrib-devhelp==1.0.6
sphinxcontrib-htmlhelp==2.0.6
sphinxcontrib-jquery==4.1
sphinxcontrib-jsmath==1.0.1
sphinxcontrib-qthelp==1.0.8
sphinxcontrib-serializinghtml==1.1.10
SQLAlchemy==2.0.31
stack-data==0.6.3
tenacity==8.5.0
terminado==0.18.1
tinycss2==1.3.0
toml==0.10.2
toolz==0.12.1
tornado==6.4.1
tqdm==4.66.4
traitlets==5.14.3
types-python-dateutil==2.9.0.20240316
types-pytz==2024.1.0.20240417
types-requests==2.32.0.20240712
typing-inspect==0.9.0
typing_extensions==4.12.2
typish==1.9.3
tzdata==2024.1
ujson==5.9.0
untokenize==0.1.1
uri-template==1.3.0
urllib3==2.2.2
virtualenv==20.26.3
waitress==2.1.2
wcwidth==0.2.13
web3==6.20.1
webcolors==24.6.0
webencodings==0.5.1
websocket-client==1.8.0
websockets==12.0
Werkzeug==3.0.3
widgetsnbextension==4.0.11
winregistry==1.1.1
yarl==1.9.4
zipp==3.19.2