Skip to content

Commit 805ab29

Browse files
committed
Cope with field_transformer being a generator
This worked on 25.1.0 but was inadvertently broken by #1401
1 parent 19943b7 commit 805ab29

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/attr/_make.py

+3
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,9 @@ def _transform_attrs(
450450
if field_transformer is not None:
451451
attrs = field_transformer(cls, attrs)
452452

453+
if inspect.isgenerator(attrs):
454+
attrs = tuple(attrs)
455+
453456
# Check attr order after executing the field_transformer.
454457
# Mandatory vs non-mandatory attr order only matters when they are part of
455458
# the __init__ signature and when they aren't kw_only (which are moved to

tests/test_hooks.py

+10
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,16 @@ class C:
217217
assert "CAttributes" == fields_type.__name__
218218
assert issubclass(fields_type, tuple)
219219

220+
def test_hook_generator(self):
221+
def hook(cls, attribs):
222+
yield from attribs
223+
224+
@attr.s(auto_attribs=True, field_transformer=hook)
225+
class Base:
226+
x: int
227+
228+
assert ["x"] == [a.name for a in attr.fields(Base)]
229+
220230

221231
class TestAsDictHook:
222232
def test_asdict(self):

0 commit comments

Comments
 (0)