def test_structuring_primitive_union_hook(converter, ints): # type: (Converter, List[int]) -> None """Registering a union loading hook works.""" def structure_hook(val, cl): """Even ints are passed through, odd are stringified.""" return val if val % 2 == 0 else unicode(val) converter.register_structure_hook(Union[unicode, int], structure_hook) converted = converter.structure(ints, List[Union[unicode, int]]) for x, y in zip(ints, converted): if x % 2 == 0: assert x == y else: assert unicode(x) == y
def structure_hook(val, cl): """Even ints are passed through, odd are stringified.""" return val if val % 2 == 0 else unicode(val)