You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to write disambiguation function to handle union type based on the default value of child class but got into weird error, also want to understand the correct way to do it with cattrs.
@define(kw_only=True)classGenType:
pass@define(kw_only=True)classTypeOne(GenType):
id:Literal["A"] ="A"@define(kw_only=True)classTypeTwo(GenType):
id:Literal["B"] ="B"UniType=Union[TypeOne, TypeTwo]
@define(kw_only=True)classContainer:
types: List[UniType]
defdiscriminate_model_types(value: typing.Any, _klass: typing.Type) ->UniType:
# Write some logic to find the exact type !!, I was wondering if there is correct pythonic version to do this with cattrs.forchild_clsinGenType.__subclasses__():
ifvalue["id"] ==child_cls().id:
returncattr.structure(value, child_cls)
defrun_union_logic():
json= {"types":[{"id":"A"}]}
cattr.register_structure_hook(UniType, discriminate_model_types)
obj=cattr.structure(json, Container)
print(obj)
json= {"id": "A"}
obj=cattr.structure(json, TypeOne)
print(obj)
#Prints following:#Container(types=[TypeOne()]) --> I dont know why property 'id' is not populated here. #TypeOne(id='A')
The text was updated successfully, but these errors were encountered:
Hi, I just stumbled across this issue because I noticed the same problem. While I don't know what exactly causes the problem, I know at least why you get this result. The reason is because the original classes are still contained in the list returned by GenType.__subclasses__(). (Weirdly enough, the problem with your code example occurs for me only for class TypeTwo, but I experienced similar behavior in my own code).
What I get as an output is [__main__.TypeOne, __main__.TypeTwo, __main__.TypeTwo], where you can see that the class appears twice (in your case, you'll probably also see the same for TypeOne, causing your error). If you select from the list the second version of the class, your example will probably work.
However, I don't know the root cause for the problem, but it might have something to do with garbage collection!? On the attrs page, there is a hint related to slotted classes:
which points to this issue: python-attrs/attrs#407
Also, it seems somehow related to the Union call because in my cases the duplicates only appeared after the call. Any idea here?
Anyway, the clean way to do it is described here: #140
Hope this helps 👍
Description
Thanks for this amazing library !!
I am trying to write disambiguation function to handle union type based on the default value of child class but got into weird error, also want to understand the correct way to do it with cattrs.
The text was updated successfully, but these errors were encountered: