konradhalas / dacite

Simple creation of data classes from dictionaries.
MIT License
1.72k stars 107 forks source link

Support for Callable? #97

Open spl0k opened 4 years ago

spl0k commented 4 years ago

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"
konradhalas commented 4 years ago

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)

sdaberdaku commented 1 year ago

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