Skip to content

Commit 14beb71

Browse files
committed
Move most UnicornView class variables to be instance variables.
1 parent c299d1a commit 14beb71

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

django_unicorn/components/unicorn_view.py

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -166,35 +166,33 @@ def construct_component(
166166

167167

168168
class UnicornView(TemplateView):
169-
response_class = UnicornTemplateResponse
169+
# These class variables are required to set these via kwargs
170170
component_name: str = ""
171171
component_key: str = ""
172172
component_id: str = ""
173-
request = None
174-
parent = None
175-
children = []
176173

177-
# Caches to reduce the amount of time introspecting the class
178-
_methods_cache: Dict[str, Callable] = {}
179-
_attribute_names_cache: List[str] = []
180-
_hook_methods_cache: List[str] = []
174+
def __init__(self, **kwargs):
175+
self.response_class = UnicornTemplateResponse
181176

182-
# Dictionary with key: attribute name; value: pickled attribute value
183-
_resettable_attributes_cache: Dict[str, Any] = {}
177+
self.component_name: str = ""
178+
self.component_key: str = ""
179+
self.component_id: str = ""
184180

185-
# JavaScript method calls
186-
calls = []
181+
# Without these instance variables calling UnicornView() outside the
182+
# Django view/template logic (i.e. in unit tests) results in odd results.
183+
self.request: HttpRequest = None
184+
self.parent: UnicornView = None
185+
self.children: List[UnicornView] = []
187186

188-
def __init__(self, **kwargs):
189-
# Set all these as instance variables instead of just class variables. Otherwise,
190-
# calling UnicornView() outside the Django view/template logic results in odd
191-
# shared class variables for these. For example in unit tests.
192-
self.component_name = ""
193-
self.component_key = ""
194-
self.component_id = ""
195-
self.request = None
196-
self.parent = None
197-
self.children = []
187+
# Caches to reduce the amount of time introspecting the class
188+
self._methods_cache: Dict[str, Callable] = {}
189+
self._attribute_names_cache: List[str] = []
190+
self._hook_methods_cache: List[str] = []
191+
192+
# Dictionary with key: attribute name; value: pickled attribute value
193+
self._resettable_attributes_cache: Dict[str, Any] = {}
194+
195+
# JavaScript method calls
198196
self.calls = []
199197

200198
super().__init__(**kwargs)

0 commit comments

Comments
 (0)