I'm adding some type hints to a stub of vertica_python
. One of the functions internally accepts the following parameter ( cursor_type
):
def row_formatter(self, row_data):
if self.cursor_type is None:
return self.format_row_as_array(row_data)
elif self.cursor_type in (list, 'list'):
return self.format_row_as_array(row_data)
elif self.cursor_type in (dict, 'dict'):
return self.format_row_as_dict(row_data)
else:
raise TypeError('Unrecognized cursor_type: {0}'.format(self.cursor_type))
So this function expects 'list', 'dict', the type list or type dict. I would like to use Optional[Literal[list, dict, 'list', 'dict']], but this is not supported:
Type arguments for "Literal" must be None, a literal value (int, bool, str, or bytes), or an enum value
Is there any way to achieve what I am trying?
You can type hint it as Union[None, Type[list], Type[dict], Literal["list", "dict"]]
(import Union and Type from typing)
Just watch out, as this would also accept a subclass of list or dict
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.