Open mmaarouf1 opened 2 years ago
Consider the following:
@define class A: first_field: int second_field: int @define class B: first_field: int third_Field: int
Using the hook factories described here to structure/unstructure camelCase attributes:
converter = Converter() def to_camel_case(snake_str: str) -> str: .... converter.register_structure_hook_factory( has, to_camel_case_structure )
Then the following behavior is observed:
camel_case_dict = { "thirdField": 2, "firstField": 1 }
The following works:
converter.structure(camel_case_dict, Union[A, B]) converter.structure(camel_case_dict, Union[B, A])
However, if we structure using Union[B, A] first, then cattrs tries to structure the input as A instead of B:
Union[B, A]
A
B
converter.structure(camel_case_dict, Union[B, A])
o = {'firstField': 1, 'thirdField': 2} _ = (<class 'test.input_config.test_output_config.A'>,) def structure_A(o, *_): res = { 'first_field': structure_first_field(o['firstField'], type_first_field), > 'second_field': structure_second_field(o['secondField'], type_second_field), } E KeyError: 'secondField'
Hello,
thanks, this might be tricky to support with the current architeture but I'll keep it in mind!
Description
Consider the following:
Using the hook factories described here to structure/unstructure camelCase attributes:
Then the following behavior is observed:
The following works:
However, if we structure using
Union[B, A]
first, then cattrs tries to structure the input asA
instead ofB
: