From ab9e470c353d1b36fb55bd72dc0cba62277a22ba Mon Sep 17 00:00:00 2001 From: s3lph Date: Fri, 29 Jun 2018 01:22:12 +0200 Subject: [PATCH] Some more type hinting/safety. --- matemat/webserver/requestargs.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/matemat/webserver/requestargs.py b/matemat/webserver/requestargs.py index 02df0b2..f69a53d 100644 --- a/matemat/webserver/requestargs.py +++ b/matemat/webserver/requestargs.py @@ -43,7 +43,7 @@ class RequestArgument(object): # Assign name self.__name: str = name # Initialize value - self.__value: Union[Tuple[str, Union[bytes, str]], List[Tuple[str, Union[bytes, str]]]] = None + self.__value: Union[Tuple[str, Union[bytes, str]], List[Tuple[str, Union[bytes, str]]]] = [] # Default to empty array if value is None: self.__value = [] @@ -98,6 +98,7 @@ class RequestArgument(object): :raises IndexError: If the index is out of bounds. :raises ValueError: If this is an array value, and no index is provided, or if this is a scalar value and an index is provided. + :raises TypeError: If the requested value is neither a str nor a bytes object. """ if self.is_array: # instance is an array value @@ -123,6 +124,7 @@ class RequestArgument(object): elif isinstance(self.__value[1], bytes): # The value is a bytes object, attempt to decode return self.__value[1].decode('utf-8') + raise TypeError('Value is neither a str nor bytes') def get_bytes(self, index: int = None) -> bytes: """ @@ -134,6 +136,7 @@ class RequestArgument(object): :raises IndexError: If the index is out of bounds. :raises ValueError: If this is an array value, and no index is provided, or if this is a scalar value and an index is provided. + :raises TypeError: If the requested value is neither a str nor a bytes object. """ if self.is_array: # instance is an array value @@ -159,6 +162,7 @@ class RequestArgument(object): elif isinstance(self.__value[1], str): # The value is a string, encode first return self.__value[1].encode('utf-8') + raise TypeError('Value is neither a str nor bytes') def get_content_type(self, index: int = None) -> Optional[str]: """ @@ -177,16 +181,18 @@ class RequestArgument(object): # Needs an index for array values raise ValueError('index must not be None') # Type hint; access array element - v: Tuple[str, Union[bytes, str]] = self.__value[index] + va: Tuple[str, Union[bytes, str]] = self.__value[index] # Return the content type of the requested value - return v[0] + return va[0] else: # instance is a scalar value if index is not None: # Must not have an index for array values raise ValueError('index must be None') + # Type hint + vs: Tuple[str, Union[bytes, str]] = self.__value # Return the content type of the scalar value - return self.__value[0] + return vs[0] def append(self, ctype: str, value: Union[str, bytes]): """ @@ -227,8 +233,8 @@ class RequestArgument(object): yield _View(self.__name, self.__value) else: # Typing helper - _value: List[Tuple[str, Union[bytes, str]]] = self.__value - for v in _value: + vs: List[Tuple[str, Union[bytes, str]]] = self.__value + for v in vs: # If this is an array, yield an immutable scalar view for each (ctype, value) element in the array yield _View(self.__name, v)