Default union disambiguation function doesn't work with renamed keys
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 25/100
Research direction
Start with src/cattrs/disambiguators.py at the linked default disambiguation function creator and reproduce the example using renamed keys. Trace how the generated hooks and union disambiguation select fields. Done means structuring both {'aField': 1} and {'anotherField': 1} into A | B succeeds with the shown renamed-key hooks, with regression coverage added where the project’s existing tests for disambiguation are located.
Written by the indexing model from the issue text.
Description
- cattrs version: cattrs version: 22.1.0
- Python version: 3.10
- Operating System: MacOS Monterey 12.2.1
Description
I tried to structure some data into the union type with keys renaming. It doesn't work because the default disambiguation function creator builds it based on non-overriden fields (link). I could write a proper disambiguator myself but I think it should work out-of-the-box.
What I Did
from attrs import define
import cattrs
from cattrs.gen import make_dict_structure_fn, override
@define
class A:
a_field: int
@define
class B:
another_field: int
c = cattrs.GenConverter()
c.register_structure_hook(A, make_dict_structure_fn(A, c, a_field=override(rename="aField")))
c.register_structure_hook(B, make_dict_structure_fn(B, c, another_field=override(rename="anotherField")))
print(c.structure({'aField': 1}, A))
# A(a_field=1)
print(c.structure({'anotherField': 1}, B))
# B(another_field=1)
print(c.structure({'anotherField': 1}, A | B))
# B(another_field=1)
print(c.structure({'aField': 1}, A | B))
# raises ClassValidationError: While structuring B (1 sub-exception)
# ClassValidationError('While structuring B', [KeyError('another_field')])
- 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
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
zostera/django-bootstrap4#894 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
use-agent-os/agent-os#3276 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
zephyrproject-rtos/zephyr#119726 ·
-
area/auth bug comp/agent P3 platform/discord type/security
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
NousResearch/hermes-agent#117848 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
zilliztech/memsearch#759 ·