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] Numpy decode issue... #498

Open
bgyooPtr opened this issue Oct 30, 2023 · 0 comments
Open

[BUG] Numpy decode issue... #498

bgyooPtr opened this issue Oct 30, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@bgyooPtr
Copy link

Description

The bug occurs when you type np.float32 into np.ndarray and run the json.dumps function.
Even though it has clearly been changed to a list through the to_dict function, an error occurs in json.dumps for some reason.

Add.....

The cause of this bug is that the elements of the numpy array are specified as numpy type. (see code below)

I think it's a bug in dataclass_json.

Code snippet that reproduces the issue

from dataclasses import dataclass, field
from dataclasses_json import dataclass_json, config
import numpy as np

import json


@dataclass_json
@dataclass
class Foo:
    a: np.ndarray = field(default=np.array([0.0, 0.0, 0.0, 0.0, 0.0]), metadata=config(decoder=np.asarray))


@dataclass_json
@dataclass
class Bar:
    a: np.ndarray = field(
        default=np.array([0.0, 0.0, 0.0, 0.0, 0.0], dtype=np.float32), metadata=config(decoder=np.asarray)
    )


a = Foo()
print(a)
b = Bar()
print(b)

json.dumps(a.to_dict())
json.dumps(b.to_dict())  # ERROR TypeError: Object of type float32 is not JSON serializable

b_dict = b.to_dict()
print(isinstance(b_dict["a"], list))  # True
print(b_dict["a"].__class__.__name__)  # list
print(type(b_dict["a"][0]))  # numpy.float32

Describe the results you expected

Note the last line

Python version you are using

3.8

Environment description

black==22.12.0
certifi==2023.7.22
charset-normalizer==3.3.1
click==8.1.7
coverage==7.3.2
dataclasses-json==0.5.9
docstring-parser==0.15
exceptiongroup==1.1.3
falcon==2.0.0
flake8==5.0.4
ghp-import==2.1.0
gitdb==4.0.11
GitPython==3.1.40
hug==2.6.1
hypothesis==6.88.1
idna==3.4
importlib-metadata==6.8.0
iniconfig==2.0.0
Jinja2==3.1.2
livereload==2.6.3
Mako==1.2.4
Markdown==3.5
MarkupSafe==2.1.3
marshmallow==3.20.1
marshmallow-enum==1.5.1
mccabe==0.7.0
mergedeep==1.3.4
mkdocs==1.3.0
mkdocs-material==8.5.4
mkdocs-material-extensions==1.3
mypy==1.6.1
mypy-extensions==1.0.0
numpy==1.24.4
packaging==23.2
pathspec==0.11.2
pdocs==1.2.0
platformdirs==3.11.0
pluggy==1.3.0
portray==1.8.0
pycodestyle==2.9.1
pyflakes==2.5.0
Pygments==2.16.1
pymdown-extensions==10.3.1
pypng==0.0.21
PyQt5==5.15.10
PyQt5-Qt5==5.15.2
PyQt5-sip==12.13.0
pytest==7.4.3
pytest-cov==2.12.1
python-dateutil==2.8.2
PyYAML==6.0.1
pyyaml_env_tag==0.1
requests==2.31.0
scipy==1.10.1
simplejson==3.19.2
six==1.16.0
smmap==5.0.1
sortedcontainers==2.4.0
termcolor==2.3.0
toml==0.10.2
tomli==2.0.1
tornado==6.3.3
typing-inspect==0.9.0
typing_extensions==4.8.0
urllib3==2.0.7
watchdog==3.0.0
yaspin==2.4.0
zipp==3.17.0

@bgyooPtr bgyooPtr added the bug Something isn't working label Oct 30, 2023
@bgyooPtr bgyooPtr changed the title [BUG] ... [BUG] Numpy decode issue... Oct 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant