2525class DiscretizeConfig :
2626 """
2727 Configuration class for RocketPy function discretization.
28+
29+ This class allows easy configuration of discretization parameters
30+ for different types of RocketPy objects and their callable attributes.
2831 """
2932
3033 def __init__ (
@@ -34,19 +37,19 @@ def __init__(
3437 self .samples = samples
3538
3639 @classmethod
37- def for_environment (cls ) -> " DiscretizeConfig" :
40+ def for_environment (cls ) -> ' DiscretizeConfig' :
3841 return cls (bounds = (0 , 50000 ), samples = 100 )
3942
4043 @classmethod
41- def for_motor (cls ) -> " DiscretizeConfig" :
44+ def for_motor (cls ) -> ' DiscretizeConfig' :
4245 return cls (bounds = (0 , 10 ), samples = 150 )
4346
4447 @classmethod
45- def for_rocket (cls ) -> " DiscretizeConfig" :
48+ def for_rocket (cls ) -> ' DiscretizeConfig' :
4649 return cls (bounds = (0 , 1 ), samples = 100 )
4750
4851 @classmethod
49- def for_flight (cls ) -> " DiscretizeConfig" :
52+ def for_flight (cls ) -> ' DiscretizeConfig' :
5053 return cls (bounds = (0 , 30 ), samples = 200 )
5154
5255
@@ -208,10 +211,11 @@ def _fix_datetime_fields(data):
208211 fixed = {}
209212 for key , value in data .items ():
210213 if (
211- key in [" date" , " local_date" , " datetime_date" ]
214+ key in [' date' , ' local_date' , ' datetime_date' ]
212215 and isinstance (value , list )
213216 and len (value ) >= 3
214217 ):
218+ # Convert [year, month, day, hour, ...] back to datetime
215219 try :
216220 year , month , day = value [0 :3 ]
217221 hour = value [3 ] if len (value ) > 3 else 0
@@ -222,7 +226,8 @@ def _fix_datetime_fields(data):
222226 fixed [key ] = datetime (
223227 year , month , day , hour , minute , second , microsecond
224228 )
225- except Exception :
229+ except (ValueError , TypeError , IndexError ):
230+ # If conversion fails, keep the original value
226231 fixed [key ] = value
227232 else :
228233 fixed [key ] = _fix_datetime_fields (value )
@@ -257,6 +262,7 @@ async def __call__(
257262
258263
259264class GZipResponder :
265+ # fork of https://github.com/encode/starlette/blob/master/starlette/middleware/gzip.py
260266 def __init__ (
261267 self , app : ASGIApp , minimum_size : int , compresslevel : int = 9
262268 ) -> None :
@@ -281,6 +287,8 @@ async def __call__(
281287 async def send_with_gzip (self , message : Message ) -> None :
282288 message_type = message ["type" ]
283289 if message_type == "http.response.start" :
290+ # Don't send the initial message until we've determined how to
291+ # modify the outgoing headers correctly.
284292 self .initial_message = message
285293 headers = Headers (raw = self .initial_message ["headers" ])
286294 self .content_encoding_set = "content-encoding" in headers
@@ -296,12 +304,14 @@ async def send_with_gzip(self, message: Message) -> None:
296304 body = message .get ("body" , b"" )
297305 more_body = message .get ("more_body" , False )
298306 if ((len (body ) < self .minimum_size ) and not more_body ) or any (
299- value == b" application/octet-stream"
307+ value == b' application/octet-stream'
300308 for header , value in self .initial_message ["headers" ]
301309 ):
310+ # Don't apply GZip to small outgoing responses or octet-streams.
302311 await self .send (self .initial_message )
303- await self .send (message )
312+ await self .send (message ) # pylint: disable=unreachable
304313 elif not more_body :
314+ # Standard GZip response.
305315 self .gzip_file .write (body )
306316 self .gzip_file .close ()
307317 body = self .gzip_buffer .getvalue ()
@@ -313,8 +323,9 @@ async def send_with_gzip(self, message: Message) -> None:
313323 message ["body" ] = body
314324
315325 await self .send (self .initial_message )
316- await self .send (message )
326+ await self .send (message ) # pylint: disable=unreachable
317327 else :
328+ # Initial body in streaming GZip response.
318329 headers = MutableHeaders (raw = self .initial_message ["headers" ])
319330 headers ["Content-Encoding" ] = "gzip"
320331 headers .add_vary_header ("Accept-Encoding" )
@@ -326,9 +337,10 @@ async def send_with_gzip(self, message: Message) -> None:
326337 self .gzip_buffer .truncate ()
327338
328339 await self .send (self .initial_message )
329- await self .send (message )
340+ await self .send (message ) # pylint: disable=unreachable
330341
331342 elif message_type == "http.response.body" :
343+ # Remaining body in streaming GZip response.
332344 body = message .get ("body" , b"" )
333345 more_body = message .get ("more_body" , False )
334346
@@ -341,12 +353,14 @@ async def send_with_gzip(self, message: Message) -> None:
341353 self .gzip_buffer .truncate ()
342354
343355 await self .send (message )
356+
344357 else :
358+ # Pass through other message types unmodified.
345359 if not self .started :
346360 self .started = True
347361 await self .send (self .initial_message )
348362 await self .send (message )
349363
350364
351365async def unattached_send (message : Message ) -> NoReturn :
352- raise RuntimeError ("send awaitable not set" )
366+ raise RuntimeError ("send awaitable not set" ) # pragma: no cover
0 commit comments