Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions bench/test_benchmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,22 @@ def test_astuple_atomic():
at(c)


@attrs.define
class NestedAtomicFields:
sequence: list[int] = attrs.Factory(lambda: list(range(100)))


def test_astuple_nested_atomic():
"""
Benchmark atomic values nested in a sequence.
"""
c = NestedAtomicFields()
at = attrs.astuple

for _ in range(ROUNDS):
at(c)


class TestCachedProperties:
@attrs.define
class Slotted:
Expand Down
1 change: 1 addition & 0 deletions changelog.d/1636.change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improved `attrs.astuple()` performance for atomic values nested in sequences by avoiding unnecessary attrs-class detection.
2 changes: 1 addition & 1 deletion src/attr/_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ def astuple(
tuple_factory=tuple_factory,
retain_collection_types=retain,
)
if has(j.__class__)
if type(j) not in _ATOMIC_TYPES and has(j.__class__)
else j
)
for j in v
Expand Down
Loading