dacite icon indicating copy to clipboard operation
dacite copied to clipboard

Support for Callable?

Open spl0k opened this issue 5 years ago • 2 comments

Hello.

First of all, thanks for this library. Would you consider adding support for Callable types? They currenly fail the type checking. For instance this code

import dacite

from dataclasses import dataclass
from typing import Callable


@dataclass
class A:
    i: int
    f: Callable[[int], bool]


def odd(value: int) -> bool:
    return bool(value % 2)


a = dacite.from_dict(A, {"i": 42, "f": odd})

produces the following error

Traceback (most recent call last):
  File "dacite_callable.py", line 17, in <module>
    a = dacite.from_dict(A, {"i": 42, "f": odd})
  File "E:\...\env\lib\site-packages\dacite\core.py", line 65, in from_dict
    raise WrongTypeError(field_path=field.name, field_type=field.type, value=value)
dacite.exceptions.WrongTypeError: wrong value type for field "f" - should be "Callable" instead of value "<function odd at 0x000001EA7FC11EA0>" of type "function"

spl0k avatar May 13 '20 15:05 spl0k

Hi @spl0k - thank you for reporting this issue.

Hmm, interesting idea. I have to think about it. Nevertheless it should: work OR be covered in documentation that it doesn't.

For now you can disable type checking with Config(check_types=False)

konradhalas avatar May 31 '20 14:05 konradhalas

I am having this same issue in version 1.8.1 and python 3.10.11.

sdaberdaku avatar Jul 10 '23 16:07 sdaberdaku