From e1b2c9491bd7d73e6049d8ad54828cc785577103 Mon Sep 17 00:00:00 2001 From: Denis Laxalde Date: Wed, 26 Jun 2024 14:25:39 +0200 Subject: [PATCH] refactor: Only loop once on "cached_properties" when building slotted classes (#1290) Only loop once on "cached_properties" when building slotted classes Previously the cached_properties dict was read three times. Co-authored-by: Hynek Schlawack --- src/attr/_make.py | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/attr/_make.py b/src/attr/_make.py index d3bfb440f..d1c08dea4 100644 --- a/src/attr/_make.py +++ b/src/attr/_make.py @@ -931,19 +931,13 @@ def _create_slots_class(self): # To know to update them. additional_closure_functions_to_update = [] if cached_properties: - # Add cached properties to names for slotting. - names += tuple(cached_properties.keys()) - - for name in cached_properties: - # Clear out function from class to avoid clashing. - del cd[name] - - additional_closure_functions_to_update.extend( - cached_properties.values() - ) - class_annotations = _get_annotations(self._cls) for name, func in cached_properties.items(): + # Add cached properties to names for slotting. + names += (name,) + # Clear out function from class to avoid clashing. + del cd[name] + additional_closure_functions_to_update.append(func) annotation = inspect.signature(func).return_annotation if annotation is not inspect.Parameter.empty: class_annotations[name] = annotation