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.
When encoding unions like
Union[List[str], str]
, hologram currently behaves differently thanUnion[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 includingNone
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 intests/test_union.py
pass. I also left separate support forSequence
if for some reason you want to treat strings as sequences of characters.