Lambda passed to register_unstructure_hook is not called for an instance when instance is in an inherited field
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 45/100
Research direction
Start by reproducing the example with cattrs 22.2.0 on Python 3.11, including the inherited frozen classes and registered PType hook. Trace unstructuring of the inherited fields and verify that the PType hook is called and json.dumps accepts the resulting value.
Written by the indexing model from the issue text.
Description
- cattrs version: 22.2.0
- Python version: 3.11
- Operating System: Windows 10
Description
I have a @frozen class that inherits a @frozen class containing two enums PType and Split. I am trying to write the class to a file using json.dumps. Unfortunately the unstructure_hook I provided is not getting called and as a result I get an Exception when dumping to json. I expect the PType unstructure_hook I provided to allow the class to be written to json.
What I Did
Here is the code that causes the exception. I have verified that the unstructure_hook I provided to c is never called.
with open(MOVES_FILE_OUT, "w") as fo:
c = cattrs.Converter()
c.register_unstructure_hook(
PType, lambda pt: pt.name
)
fo.write(json.dumps(c.unstructure(moves)))
This is the exception
TypeError: Object of type PType is not JSON serializable
My understanding is that the convertor calls the unstructure_hook when it sees a PType and coverts it to the type returned by the lambda (a str here), but it appears to miss the PType fields.
- Dominant language
- Python
- Stars
- 1.1k
- Forks
- 159
- Avg merge
- 12h 21m
- Merged PRs (30d)
- 6
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.
More from python-attrs/cattrs
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
python-attrs/cattrs#761 · 2 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
python-attrs/cattrs#513 · 4 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 35/100
python-attrs/cattrs#779 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 85/100
python-attrs/cattrs#774 ·
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
python-attrs/cattrs#746 · 2 comments ·
All issues in python-attrs/cattrs
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
syfoud/Simulated_Scepter#172 ·
-
A cancelled tests run makes the coverage comment workflow fail and reports it as a red check on main Openarea: ci bug perceived difficulty: 3
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Nitjsefnie-Harness-Commons/daedalus#921 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
EleutherAI/lm-evaluation-harness#4207 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 92/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
ClickHouse/clickhouse-connect#1057 ·