forked from s3lph/matemat
Some more type hinting/safety.
This commit is contained in:
parent
8898abc77b
commit
ab9e470c35
1 changed files with 12 additions and 6 deletions
|
@ -43,7 +43,7 @@ class RequestArgument(object):
|
||||||
# Assign name
|
# Assign name
|
||||||
self.__name: str = name
|
self.__name: str = name
|
||||||
# Initialize value
|
# 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
|
# Default to empty array
|
||||||
if value is None:
|
if value is None:
|
||||||
self.__value = []
|
self.__value = []
|
||||||
|
@ -98,6 +98,7 @@ class RequestArgument(object):
|
||||||
:raises IndexError: If the index is out of bounds.
|
: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
|
: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.
|
index is provided.
|
||||||
|
:raises TypeError: If the requested value is neither a str nor a bytes object.
|
||||||
"""
|
"""
|
||||||
if self.is_array:
|
if self.is_array:
|
||||||
# instance is an array value
|
# instance is an array value
|
||||||
|
@ -123,6 +124,7 @@ class RequestArgument(object):
|
||||||
elif isinstance(self.__value[1], bytes):
|
elif isinstance(self.__value[1], bytes):
|
||||||
# The value is a bytes object, attempt to decode
|
# The value is a bytes object, attempt to decode
|
||||||
return self.__value[1].decode('utf-8')
|
return self.__value[1].decode('utf-8')
|
||||||
|
raise TypeError('Value is neither a str nor bytes')
|
||||||
|
|
||||||
def get_bytes(self, index: int = None) -> 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 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
|
: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.
|
index is provided.
|
||||||
|
:raises TypeError: If the requested value is neither a str nor a bytes object.
|
||||||
"""
|
"""
|
||||||
if self.is_array:
|
if self.is_array:
|
||||||
# instance is an array value
|
# instance is an array value
|
||||||
|
@ -159,6 +162,7 @@ class RequestArgument(object):
|
||||||
elif isinstance(self.__value[1], str):
|
elif isinstance(self.__value[1], str):
|
||||||
# The value is a string, encode first
|
# The value is a string, encode first
|
||||||
return self.__value[1].encode('utf-8')
|
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]:
|
def get_content_type(self, index: int = None) -> Optional[str]:
|
||||||
"""
|
"""
|
||||||
|
@ -177,16 +181,18 @@ class RequestArgument(object):
|
||||||
# Needs an index for array values
|
# Needs an index for array values
|
||||||
raise ValueError('index must not be None')
|
raise ValueError('index must not be None')
|
||||||
# Type hint; access array element
|
# 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 the content type of the requested value
|
||||||
return v[0]
|
return va[0]
|
||||||
else:
|
else:
|
||||||
# instance is a scalar value
|
# instance is a scalar value
|
||||||
if index is not None:
|
if index is not None:
|
||||||
# Must not have an index for array values
|
# Must not have an index for array values
|
||||||
raise ValueError('index must be None')
|
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 the content type of the scalar value
|
||||||
return self.__value[0]
|
return vs[0]
|
||||||
|
|
||||||
def append(self, ctype: str, value: Union[str, bytes]):
|
def append(self, ctype: str, value: Union[str, bytes]):
|
||||||
"""
|
"""
|
||||||
|
@ -227,8 +233,8 @@ class RequestArgument(object):
|
||||||
yield _View(self.__name, self.__value)
|
yield _View(self.__name, self.__value)
|
||||||
else:
|
else:
|
||||||
# Typing helper
|
# Typing helper
|
||||||
_value: List[Tuple[str, Union[bytes, str]]] = self.__value
|
vs: List[Tuple[str, Union[bytes, str]]] = self.__value
|
||||||
for v in _value:
|
for v in vs:
|
||||||
# If this is an array, yield an immutable scalar view for each (ctype, value) element in the array
|
# If this is an array, yield an immutable scalar view for each (ctype, value) element in the array
|
||||||
yield _View(self.__name, v)
|
yield _View(self.__name, v)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue