@@ -155,8 +155,9 @@ def create_from_github(
155155 self ._bill .full_clean ()
156156 self ._bill .save ()
157157 logger .info ("PR %s: Bill %s created" , pull_request .number , self ._bill .id )
158+ bill = self ._bill
158159
159- return self . _bill
160+ return bill # noqa: RET504
160161
161162 @contextmanager
162163 def _create_submit_task (self ) -> Iterator [PeriodicTask ]:
@@ -165,7 +166,7 @@ def _create_submit_task(self) -> Iterator[PeriodicTask]:
165166 Returns:
166167 The task that was scheduled
167168 """
168- # This can be extracted to a signal if we want to support other creation methods
169+ # This might be better as a signal but I want to keep it localized
169170 voting_ends , __ = IntervalSchedule .objects .get_or_create (
170171 every = settings .WEBISCITE_VOTING_PERIOD , period = IntervalSchedule .DAYS
171172 )
@@ -182,9 +183,15 @@ def _create_submit_task(self) -> Iterator[PeriodicTask]:
182183 try :
183184 yield submit_task
184185 finally :
185- # This might be better as a signal but I want to keep it localized
186- if not isinstance (self ._bill .id , int ):
187- raise TypeError ("self._bill was not saved in the submit task context" )
186+ if not (
187+ hasattr (self , "_bill" )
188+ and hasattr (self ._bill , "id" )
189+ and isinstance (self ._bill .id , int )
190+ ):
191+ raise AttributeError (
192+ "self._bill was not saved in the submit task context"
193+ )
194+
188195 submit_task .name = f"bill_submit:{ self ._bill .id } "
189196 submit_task .args = json .dumps ([self ._bill .id ])
190197 submit_task .save ()
@@ -194,6 +201,8 @@ def _create_submit_task(self) -> Iterator[PeriodicTask]:
194201 submit_task .name ,
195202 )
196203
204+ del self ._bill
205+
197206
198207class Bill (StatusModel , TimeStampedModel ):
199208 """Model for a proposal to merge a particular pull request into the main branch"""
0 commit comments