konradhalas / dacite

Simple creation of data classes from dictionaries.
MIT License
1.76k stars 106 forks source link

Support base classes in "cast" #70

Closed konradhalas closed 4 years ago

antonagestam commented 4 years ago

@konradhalas I get TypeError: issubclass() arg 1 must be a class when testing this. Partial stack trace:

  File "/app/src/kco3/dataclasses.py", line 127, in from_dict
    return dacite.from_dict(cls, data, config=cnf)
  File "/app/venv/lib/python3.8/site-packages/dacite/core.py", line 56, in from_dict
    transformed_value = transform_value(
  File "/app/venv/lib/python3.8/site-packages/dacite/types.py", line 13, in transform_value
    if issubclass(target_type, cast_type):
TypeError: issubclass() arg 1 must be a class

I think this should either check with inspect.isclass(...) and issubclass(...) or catch the TypeError, see https://github.com/konradhalas/dacite/pull/65/files :)

antonagestam commented 4 years ago

Adding a testcase with a non-class type like Optional should cover this.

antonagestam commented 4 years ago

Example catching the TypeError :) https://github.com/konradhalas/dacite/commit/a57b31b34c83759d80e15a964573569d8122e485

konradhalas commented 4 years ago

@antonagestam thank you very much for a review and testing. Good catch - I fixed Optional[...] case. Should be ok now :)