konradhalas / dacite

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

Skip the isinstance check if a cast was used #110

Closed tmke8 closed 4 years ago

tmke8 commented 4 years ago

Hi, I have a (maybe unusual) situation where I do a cast but the result will not pass the isinstance check. A minimal example:

from dataclasses import dataclass
from typing import NewType
import dacite

UserId = NewType("UserId", int)

@dataclass
class Config:
    user_id: UserId

dacite.from_dict(data_class=Config, data={"user_id": 3}, config=dacite.Config(cast=[UserId]))

In this case, isinstance(user_id, UserId) will fail, because NewType has no effect at runtime (it's purely for static type checking).

I would argue that if a cast was performed, the user knows what they're doing and the isinstance check is not needed anyway.

If this sounds reasonable to you, I'd be happy to make the code changes and send a PR.

tmke8 commented 4 years ago

Never mind actually. It does work if you don't do other weird things (like I did).