Closed tillahoffmann closed 3 months ago
Hi @tillahoffmann, could you point me to an example where the value is None but it is not a lazy property?
I'm not actually aware of an example where the value is None
. We could also just not set the value if it's None
without the extra lazy_property
check.
I added it to prevent surprises. E.g., if a user implemented a custom distribution where one of the fields has a None
value and we didn't check the lazy_property
, they would get an AttributeError
if they accessed the attribute after a flatten/unflatten cycle. It's probably an unlikely scenario, however.
Oh, I think I understand your implementation now. That makes sense to me. Thanks!!
The
tree_flatten
function usesself.__dict__.get(name)
to obtain the data field with the given name. Unevaluated lazy properties do not appear inself.__dict__
, and they are set toNone
when the representation is unflattened. Here is an example.The changes in this PR only set the attribute on the reconstructed instance if the value is not
None
or if the attribute is not a lazy property. While there is ambiguity betweenNone
representing an unevaluated property andNone
being the value of a lazy property, the implementation remains correct: If the evaluated value isNone
it is not cached and re-evaluates toNone
the first time the attribute is accessed on the reconstructed instance.I've also added a test.