I am currently working on adding type hints to a project and can't figure out how to get this right. I have a list of lists, with the nested list containing two elements of type int and float. The first element of the nested list is always an int and the second is always a float.
my_list = [[1000, 5.5], [1432, 2.2], [1234, 0.3]]
I would like to type annotate it so that unpacking the inner list in for loops or loop comprehensions keeps the type information. I could change the inner lists to tuples and would get what I'm looking for:
def some_function(list_arg: list[tuple[int, float]]): pass
However, I need the inner lists to be mutable. Is there a nice way to do this for lists? I know that abstract classes like Sequence and Collection do not support multiple types.
from Python type hinting for a generic mutable tuple / fixed length sequence with multiple types
No comments:
Post a Comment