dbt-labs / hologram

A library for automatically generating Draft 7 JSON Schemas from Python dataclasses
MIT License
9 stars 13 forks source link

Fix: better decode errors + union decoding/encoding #23

Closed beckjake closed 5 years ago

beckjake commented 5 years ago

When encoding unions like Union[List[str], str], hologram currently behaves differently than Union[str, List[str]], because it treats strings as lists and decodes 'asdf' as ['a', 's', 'd', 'f] depending upon which comes out first in Union.args. There's a similar weird issue with Unions including None that get eagerly decoded into only the first possible member of the union, which is wrong! Instead, skip handling for those fields and let the union encoder/decoder take care of it.

This PR makes hologram a bit more picky about what types can get coerced into List[T]/Tuple[T, ...] - and as a consequence, the tests I added in tests/test_union.py pass. I also left separate support for Sequence if for some reason you want to treat strings as sequences of characters.