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

Deserialization issues with dataclasses in dictionaries #159

Closed
camilanovaes opened this issue Dec 2, 2024 · 3 comments
Closed

Deserialization issues with dataclasses in dictionaries #159

camilanovaes opened this issue Dec 2, 2024 · 3 comments
Labels
acknowledged bug Something isn't working

Comments

@camilanovaes
Copy link

camilanovaes commented Dec 2, 2024

  • Dataclass Wizard version: 0.32.0
  • Python version: 3.10.12
  • Operating System: Ubuntu 22.04.5 LTS

Description

The code provided worked in version 0.30.1 but stopped working in versions 0.31.0 and 0.32.0. The issue happens when using a dataclass in a dictionary (i.e., dict[str, Test]).

What I Did

Code example:

from dataclasses import dataclass
from dataclass_wizard import fromdict

config = {"tests": {"test_a": {"field": "a"}, "test_b": {"field": "b"}}}

@dataclass
class Test:
    field: str
    
@dataclass
class Config:
    tests: dict[str, Test]

test_config = fromdict(Config, config)
print(test_config)

Traceback:

Traceback (most recent call last):
  File "/home/camila/.virtualenvs/gn/lib/python3.10/site-packages/dataclass_wizard/loaders.py", line 592, in fromdict
    load = CLASS_TO_LOAD_FUNC[cls]
KeyError: <class '__main__.Config'>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/camila/work/test_datawizard.py", line 18, in <module>
    test_config = fromdict(Config, config)
  File "/home/camila/.virtualenvs/gn/lib/python3.10/site-packages/dataclass_wizard/loaders.py", line 594, in fromdict
    load = load_func_for_dataclass(cls)
  File "/home/camila/.virtualenvs/gn/lib/python3.10/site-packages/dataclass_wizard/loaders.py", line 650, in load_func_for_dataclass
    field_to_parser = dataclass_field_to_load_parser(cls_loader, cls, config)
  File "/home/camila/.virtualenvs/gn/lib/python3.10/site-packages/dataclass_wizard/class_helper.py", line 131, in dataclass_field_to_load_parser
    return _setup_load_config_for_cls(cls_loader, cls, config, save)
  File "/home/camila/.virtualenvs/gn/lib/python3.10/site-packages/dataclass_wizard/class_helper.py", line 211, in _setup_load_config_for_cls
    name_to_parser[f.name] = getattr(p := cls_loader.get_parser_for_annotation(
  File "/home/camila/.virtualenvs/gn/lib/python3.10/site-packages/dataclass_wizard/loaders.py", line 416, in get_parser_for_annotation
    return MappingParser(
  File "<string>", line 5, in __init__
  File "/home/camila/.virtualenvs/gn/lib/python3.10/site-packages/dataclass_wizard/parsers.py", line 559, in __post_init__
    self.val_base_type = val_parser.base_type
AttributeError: 'function' object has no attribute 'base_type'

Expected:

Config(tests={'test_a': Test(field='a'), 'test_b': Test(field='b')})
@rnag
Copy link
Owner

rnag commented Dec 4, 2024

Hi @camilanovaes 👋,

Thank you for opening this issue and providing a minimal example!

I’ve confirmed it’s a bug and identified the cause. While I initially thought all uses of base_type were confined to UnionParser, it seems I missed one 😅.

I’ll be working on a bugfix patch for this shortly — stay tuned!

@rnag rnag added bug Something isn't working acknowledged labels Dec 4, 2024
@rnag
Copy link
Owner

rnag commented Dec 4, 2024

I forgot to link the PR, but this is now fixed in v0.32.1, as part of #162 🙌 . Also added a test case to confirm intended functionality.

@rnag rnag closed this as completed Dec 4, 2024
@camilanovaes
Copy link
Author

Amazing, @rnag !
Thanks!! 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
acknowledged bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants