Skip to content

Confusing ValueError when trying to deserialize a dict into a field of type list #289

@x3ro

Description

@x3ro

Describe the bug
When dacite tries to deserialize a field as a list which in reality is a dict, the error message is confusing and does not indicate which field the problem originates from.

To Reproduce

from dataclasses import dataclass
from typing import List

from dacite import from_dict


@dataclass
class Sub:
    name: str


@dataclass
class Example:
    xs: List[Sub]

from_dict(data_class=Example, data={"xs": {"name": "foo"}})
# ValueError: dictionary update sequence element #0 has length 4; 2 is required

Expected behavior
An error that indicates which field caused the problem, and maybe some detail on what happened, e.g.

dacite.exceptions.WrongTypeError: wrong value type for field "xs" - should be "List" instead of value "{'name': 'foo'}" of type "dict"

Environment

  • Python version: 3.14.0
  • dacite version: 1.9.2

Additional context
Error originates here, where the following kind of call happens (based on the above example). It seems like maybe the condition on L160 is too broad?

dict(["name"])
# ValueError: dictionary update sequence element #0 has length 4; 2 is required

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions