dacite icon indicating copy to clipboard operation
dacite copied to clipboard

Logic problem in types.is_optional?

Open joseph-long opened this issue 2 years ago • 3 comments

I was wondering why my optional union wasn't matching, and instead raising an exception from a type hook trying to convert a string into a float... and after some investigation I think it's this line:

https://github.com/konradhalas/dacite/blob/master/dacite/types.py#L48

Changing the implementation to

def is_optional(type_: Type) -> bool:
    members = extract_generic(type_)
    return is_union(type_) and type(None) in members and len(members) == 2

makes things work, and from_dict takes the _build_value path instead. (At least, I think so, if I'm following it correctly!)

Thanks for building this and sharing it. I apologize for not including a minimal reproducing example now, but it's late here and I wanted to write things down while they are in my head 🙃

joseph-long avatar Oct 26 '21 05:10 joseph-long

Hi @joseph-long - thank you for reporting this issue.

It looks like a bug, probably same issue as #163. I hope I will fix it soon.

konradhalas avatar Apr 12 '22 14:04 konradhalas

I've come across similar issue with A | B style type annotation for typing.Union.

date: datetime.date | None

didn't get type_hooks={datetime.date: datetime.date.fromisoformat} working, but

date: typing.Union[datetime.date, None]

did.

majiang avatar Apr 29 '22 17:04 majiang

@majiang See fix for PEP 604 unions in #184.

antonagestam avatar Jun 08 '22 09:06 antonagestam

@joseph-long hope it's fixed via https://github.com/konradhalas/dacite/pull/164

konradhalas avatar Dec 21 '22 10:12 konradhalas