python-attrs / python-attrs/cattrs
Optional fields may break automatic disambiguator
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.1k
- Forks
- 159
- Avg merge
- 12h 21m
- Merged PRs (30d)
- 6
Description
- cattrs version: 0.9.0
- Python version: 3.6.8
- Operating System: Mac OS 10.13.16 (High Sierra)
Description
Feel free to close this if there's no intent on handling optional/default fields more than is already present.
I'm attempting to use the automatic disambiguation for union composed of two types types. Each of the types are composed of mutually exclusive required fields and optional fields. When structure is called it errors saying the TypeError: __init__() got an unexpected keyword argument .
What I Did
import attr
import cattr
import typing
@attr.s(auto_attribs=True)
class A:
z: str
x: str = None
y: str = None
@attr.s(auto_attribs=True)
class B:
c: str
a: str = None
b: str = None
@attr.s(auto_attribs=True)
class C:
d: typing.List[typing.Union[A, B]]
example_data = {
"d": [
{
"z": "test"
},
{
"c": "test"
}
]
}
data = cattr.structure(example_data, C)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/cattr/converters.py", line 178, in structure
return self._structure_func.dispatch(cl)(obj, cl)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/cattr/converters.py", line 296, in structure_attrs_fromdict
conv_obj[name] = dispatch(type_)(val, type_)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/cattr/converters.py", line 309, in _structure_list
for e in obj
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/cattr/converters.py", line 309, in <listcomp>
for e in obj
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/cattr/converters.py", line 384, in _structure_union
return self._structure_func.dispatch(cl)(obj, cl)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/cattr/converters.py", line 298, in structure_attrs_fromdict
return cl(**conv_obj)
TypeError: __init__() got an unexpected keyword argument 'z'
Potential root cause
Best I can tell the error is caused by disambiguator only placing the first unique field of the set in the uniq_attrs_dict regardless of whether its required or not. Meaning blocks that only use required fields may go to the fallback which won't match the parameters.
https://github.com/Tinche/cattrs/blob/master/src/cattr/disambiguators.py#L42
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
Start with the reproduction in the issue and inspect src/cattr/disambiguators.py around line 42, then follow the automatic union-structuring path in src/cattr/converters.py. Confirm how required and optional unique fields are selected, and add or update a focused regression test showing that the A/B example structures without an unexpected keyword argument.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100