Skip to content

Commit 3b0378d

Browse files
ashbhynek
andauthored
Cope with field_transformer being a generator (#1417)
* Cope with `field_transformer` being a generator This worked on 25.1.0 but was inadvertently broken by #1401 * Update src/attr/_make.py Co-authored-by: Ash Berlin-Taylor <[email protected]> --------- Co-authored-by: Hynek Schlawack <[email protected]>
1 parent 19943b7 commit 3b0378d

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/attr/_make.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ def _transform_attrs(
448448
attrs = base_attrs + own_attrs
449449

450450
if field_transformer is not None:
451-
attrs = field_transformer(cls, attrs)
451+
attrs = tuple(field_transformer(cls, attrs))
452452

453453
# Check attr order after executing the field_transformer.
454454
# Mandatory vs non-mandatory attr order only matters when they are part of

tests/test_hooks.py

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

220+
def test_hook_generator(self):
221+
"""
222+
Ensure that `attrs.fields` are correctly recorded when field_transformer is a generator
223+
224+
Regression test for #1417
225+
"""
226+
227+
def hook(cls, attribs):
228+
yield from attribs
229+
230+
@attr.s(auto_attribs=True, field_transformer=hook)
231+
class Base:
232+
x: int
233+
234+
assert ["x"] == [a.name for a in attr.fields(Base)]
235+
220236

221237
class TestAsDictHook:
222238
def test_asdict(self):

0 commit comments

Comments
 (0)