attrs 23.1.0 breaks string annotated generics
Maintainers usually reply within 2 days
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 45/100
Research direction
Start in converters.py around lines 976-977 and compare the attrs 22.2.0 and 23.1.0 behavior for generic classes with string annotations. Run the supplied A[int] reproduction; done means structuring {'a': 1} succeeds while {'a': 'one'} still fails as expected.
Written by the indexing model from the issue text.
Description
- cattrs version: 23.1.2
- Python version: 3.10.7
- Operating System: Ubuntu
Description
When working with generic attrs types with cattrs, I updated my version of attrs and it caused failures. Having looked at the changelog in attrs:
https://www.attrs.org/en/stable/changelog.html#id1
This seems likely it was caused by:
attrs.has() and attrs.fields() now handle generic classes correctly. #1079
Changing the behaviour of: (converters.py 976-977)
attribs = fields(get_origin(cl) or cl if is_generic(cl) else cl)
if attrs_has(cl) and any(isinstance(a.type, str) for a in attribs):
in which case it probably can't be described as an attrs bug. (But I don't have high confidence in that).
(I confirmed that
resolve_types(cl)
failed on both versions of attrs, so the difference is somewhere in the if statement)
What I Did
from __future__ import annotations
from typing import TypeVar, Generic
import attrs
import cattrs
T = TypeVar('T')
@attrs.frozen
class A(Generic[T]):
a: T
converter = cattrs.Converter()
converter.gen_structure_attrs_fromdict(A[int]) # This fails with attrs 23.1.0
#And hence the lower calls won't work
cattrs.structure({'a': 1}, A[int]) # Succeeds with attrs 22.2.0
cattrs.structure({'a': 'one'}, A[int]) # Fails as expected with attrs 22.2.0
- Dominant language
- Python
- Stars
- 1.1k
- Forks
- 165
- Avg merge
- 3d 11h
- Merged PRs (30d)
- 5
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing 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 ·
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
python-attrs/cattrs#513 · 4 comments ·
Maintainers usually reply within 2 days
-
Difficulty 3/5 1-2 days Newbie friendliness 85/100
python-attrs/cattrs#774 ·
Maintainers usually reply within 2 days
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
python-attrs/cattrs#746 · 2 comments ·
Maintainers usually reply within 2 days
-
Difficulty 4/5 3-5 days Newbie friendliness 45/100
python-attrs/cattrs#743 · 2 comments ·
Maintainers usually reply within 2 days
All issues in python-attrs/cattrs
Similar issues
-
tool-calling
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
vllm-project/vllm#59838 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 92/100
raullenchai/Rapid-MLX#4042 ·
Maintainers usually reply within 1 day
-
documentation
Difficulty 1/5 Under an hour Newbie friendliness 92/100
transitmatters/mbta-slow-zone-bot#70 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
litestar-org/advanced-alchemy#811 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day