Coverage for src/chainalysis/util_functions/check_list_type.py: 100%

9 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-20 16:53 -0400

1from typing import Any, List, Type 

2 

3from chainalysis.util_functions.exceptions import BadRequest 

4 

5 

6def get_list_type(_list: List[Any]) -> Type: 

7 """Return the type of list elements if all are the same type, otherwise raise an error.""" 

8 if not _list: 

9 raise ValueError("The list is empty and has no element type.") 

10 

11 element_types = {type(item) for item in _list} 

12 

13 if len(element_types) > 1: 

14 raise BadRequest( 

15 f"The list contains multiple types: {element_types}. Enter a list with only numbers, strings, or bools." 

16 ) 

17 

18 return element_types.pop()