I'd like to annotate my return type which happens to be a list containing lists of integers. Is this annotation: List[List[int]]
okay? Here's exact example of my return type:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Yes, List[List[int]]
is the correct type.
As a side note, whenever you're unsure of the type, you can define that variable and use the Mypy reveal_type
method to have it guess the correct type. For example:
> cat foo.py
a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
reveal_type(a)
> mypy foo.py
1.py:2: note: Revealed type is 'builtins.list[builtins.list*[builtins.int]]'
which tells you that the type of a
is List[List[int]]
. Note that reveal_type
is not a valid function; it's rather a special syntax built into Mypy. If you try to run foo.py
in Python, it'll throw a NameError
.
For more information, consider reading the Mypy docs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With