python-attrs / python-attrs/cattrs
GenConverter's dict_factory argument has no effect
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.1k
- Forks
- 159
- Avg merge
- 12h 21m
- Merged PRs (30d)
- 6
Description
- cattrs version: 1.10.0
- Python version: 3.10.0
- Operating System: Win10
Description
I was experimenting with ways to modify the unstructured data, and noticed that GenConverter accepts the dict_factory argument, and also passes it to Converter, but effectively ignores it since the generated code hardcodes a dict.
For example
import attrs, cattr
@attrs.define
class Example:
a: int
b: int
class ExampleDict(dict):
def __repr__(self):
return f"ExampleDict({super().__repr__()})"
def __setitem__(self, key, value):
if abs(value) > 1<<52:
value = str(value)
super().__setitem__(key, value)
foo = Example(42, 1189998819991197253)
print(cattr.Converter(dict_factory=ExampleDict).unstructure(foo))
# ExampleDict({'a': 42, 'b': '1189998819991197253'})
print(cattr.GenConverter(dict_factory=ExampleDict).unstructure(foo))
# {'a': 42, 'b': 1189998819991197253}
GenConverter returns a dict (and therefore the unchanged values), while Converter returns an instance of ExampleDict, as expected.
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 by locating GenConverter and the generated unstructure path, then compare its dict_factory handling with Converter. Reproduce the Example/ExampleDict case from the issue and verify that generated unstructure output uses the configured factory and preserves its value transformation.
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
- 42/100