-
-
Notifications
You must be signed in to change notification settings - Fork 121
Open
Labels
more-info-neededMore information required.More information required.
Description
- cattrs version: 23.2.3
- Python version: 3.11
- Operating System: Windows (dev)/Linux (prod)
Hey there!
I have a few attrs classes. Some are members of a (tagged) union, together with dict
.
The unstructure already works. Among other things, datetime
objects are converted into strings -- in a special format (_TIMESTAMP_FORMAT: Final[str] = '%Y%m%d_%H%M%S'
).
The structuring of datetime
attributes in attrs instances works perfectly.
My problem is with the structuring of simple dict
objects.
I need to hook into it to convert the strings (which are in that special format) into datetime
objects.
But since the simple dict
objects can also contain attrs instances, it must be possible to call the structuring recursively again.
I just need a hint how to call the structuring without creating an endless loop.
Metadata
Metadata
Assignees
Labels
more-info-neededMore information required.More information required.
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
Tinche commentedon Mar 18, 2024
Could you provide a minimal example in code?
kkg-else42 commentedon Mar 20, 2024
Sorry for the delay...
I don't know if it is a minimal one, but here is my example:
As a result of this:
I get this output:
Frame(data={'a': {'some': 'a', 'sub': {'foo': 'bar'}}, 'ts': datetime.datetime(2024, 3, 20, 1, 2, 3)})
But what I need is this output:
Frame(data={'a': A(some='a', sub=Sub(foo='bar')), 'ts': datetime.datetime(2024, 3, 20, 1, 2, 3)})
kkg-else42 commentedon Apr 8, 2024
Hi Tin,
Is there anything else I should add or is it just a busy schedule?
Tinche commentedon Apr 10, 2024
Hey,
yeah sorry I got sidetracked by other things.
This is going to be complicated without modeling this more precisely. How do you know a nested dict is supposed to be converter into a class instance and not left as a dict? If a nested dict always means
A | B
, then it gets easier.That looks correct given the input. Even if we assume
data['a']
is logically typed asFrameData
, it has no_type
field and so will default to a dict. In other words, how can we telldata['a']
is supposed to beA
?kkg-else42 commentedon Apr 12, 2024
I now see that my example is misleading.
My sentence (which you quoted) referred to the following line in the
_structure_dict
function:The goal is not to convert an arbitrary dict into an attrs instance. But dict-values, which in turn can be attrs instances (with datetime), should also be converted accordingly. However, they are not recognized as such before the datetime conversion (due to the special format).
To achieve this, the converter would have to be called again within the
_structure_dict
function. Something like this:Of course, this doesn't work because it creates an endless loop.
With attrs classes I use
conv.structure_attrs_fromdict
to achieve this (as in the case ofFrame
).If necessary, I can rework the example. (But that will certainly not be until the week after next.)
Tinche commentedon Apr 13, 2024
Yeah, a simplified example would be good.
You can just call
_structure_dict
on each value yourself, right? You don't even need to jump back into cattrs. It won't create an endless loop since it will stop when there are no values.