I ran into some errors recently after converting types from Union[T, V] -> T | V style and found that there is some subtle behavior with caching and the differences between these types that can lead to errors.
even though only the Union style defines its __origin__ attribute:
>>> (typing.Union[str, None]).__origin__
typing.Union
>>> (str | None).__origin__
AttributeError: 'types.UnionType' object has no attribute '__origin__'
To Reproduce
This can cause an error when dacite.types.is_generic is first called with a Union style type and then another function like dacite.types.is_generic_collection is called with the equivalent pipe style type:
>>> dacite.types.is_generic(Union[str, None])
True
>>> dacite.types.is_generic_collection(str | None)
...
AttributeError: 'types.UnionType' object has no attribute '__origin__'
Note that swapping the ordering of the type styles does not cause an error:
In my application call, I'm mostly just calling dacite.from_dict had trouble reproing from that higher-level function although saw this exception pop up in production usage.
Expected behavior
Expect that the two type styles for unions should be fully interchangeable and not lead to runtime errors.
Environment
Python version: 3.11.9
dacite version: 1.8.1
Additional context
Add any other context about the problem here.
Describe the bug
Potentially related to https://github.com/konradhalas/dacite/issues/180
I ran into some errors recently after converting types from
Union[T, V]
->T | V
style and found that there is some subtle behavior with caching and the differences between these types that can lead to errors.The two types hash to the same value:
even though only the
Union
style defines its__origin__
attribute:To Reproduce
This can cause an error when
dacite.types.is_generic
is first called with a Union style type and then another function likedacite.types.is_generic_collection
is called with the equivalent pipe style type:Note that swapping the ordering of the type styles does not cause an error:
In my application call, I'm mostly just calling
dacite.from_dict
had trouble reproing from that higher-level function although saw this exception pop up in production usage.Expected behavior
Expect that the two type styles for unions should be fully interchangeable and not lead to runtime errors.
Environment
dacite
version: 1.8.1Additional context Add any other context about the problem here.