Skip to content

Commit 464fcd0

Browse files
committed
Use a defaultdict for building mapped list
We were previously manually doing what defaultdict does for us.
1 parent 27fbac2 commit 464fcd0

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

cbv/views.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from collections import defaultdict
12
from typing import Any
23

34
import attrs
@@ -105,12 +106,9 @@ def get_prepared_attributes(
105106
) -> models.QuerySet["KlassAttribute"]:
106107
attributes = class_.get_attributes()
107108
# Make a dictionary of attributes based on name
108-
attribute_names: dict[str, list[KlassAttribute]] = {}
109+
attribute_names: dict[str, list[KlassAttribute]] = defaultdict(list)
109110
for attr in attributes:
110-
try:
111-
attribute_names[attr.name] += [attr]
112-
except KeyError:
113-
attribute_names[attr.name] = [attr]
111+
attribute_names[attr.name].append(attr)
114112

115113
ancestors = class_.get_all_ancestors()
116114

0 commit comments

Comments
 (0)