python-attrs / python-attrs/cattrs
Unstructuring subclasses
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.1k
- Forks
- 159
- Avg merge
- 12h 21m
- Merged PRs (30d)
- 6
Description
- cattrs version: 22.2.0
- Python version: 3.8
- Operating System: macOS
Description
Hi @Tinche, I am currently working on several projects where I need to (un)structure objects of subclasses of a common base class. Digging through the documentation and also some issues here on Github, I eventually found your example how this can be done with the current version of cattrs. However, I think I have found two problems (bugs? unsupported features?) with the approach and would love to hear your opinion.
But before I go into the details, a quick question upfront: Is this going to remain the "recommended" approach to unstructure objects from a class hierarchy? I am asking because I also saw #312 which seems about to be merged but I am not entirely sure how/if it's going to replace your described method.
So here are now the two issues I noticed:
- With the version described above, your example no longer seems to work. In order to get it running, one now needs to explicitly set
slotted=False. I guess this is probably due to due the changes defaults (see also #267)? On the other hand, I wonder why your example then originally worked because it already usesdefineinstead ofattr.s? - Still, I struggled a lot to get it running in my own code until I finally found the problem: The approach crashes when using
from __future__ import annotationsand throws the following exception:
Traceback (most recent call last):
File "/usr/local/Caskroom/mambaforge/base/envs/baybe/lib/python3.8/site-packages/cattrs/converters.py", line 461, in _structure_attribute
return self._structure_func.dispatch(type_)(value, type_)
File "/usr/local/Caskroom/mambaforge/base/envs/baybe/lib/python3.8/site-packages/cattrs/converters.py", line 377, in _structure_error
raise StructureHandlerNotFoundError(msg, type_=cl)
cattrs.errors.StructureHandlerNotFoundError: Unsupported type: 'int'. Register a structure hook for it.
Is the second issue related to #215 or is this a bug?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the subclass unstructuring example from issue #140 with cattrs 22.2.0, including slotted=False and from __future__ import annotations. Start by inspecting cattrs/converters.py, especially _structure_attribute, and compare the behavior with the changes discussed in issues #267, #312, and #215. Done means the supported approach and the cause or resolution of both reported failures are clear.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100