Correct typing for list of ints

Viewed 73

Consider the following code in file x.py

from typing import List, Optional

my_list: List[Optional[int]] = list()
for i in range(7):
    my_list.append(i)
my_list = sorted(my_list)
if (len(my_list) > 0):
    my_list.append(None)
# do something with my_list

The command mypy x.py gives the following error:

x.py:6: error: Value of type variable "SupportsRichComparisonT" of "sorted" cannot be "Optional[int]"

So I changed x.py to the following:

from typing import List

my_list: List[int] = list()
for i in range(7):
    my_list.append(i)
my_list = sorted(my_list)
if (len(my_list) > 0):
    my_list.append(None)
# do something with my_list

But now mypy x.py outputs

x.py:8: error: Argument 1 to "append" of "list" has incompatible type "None"; expected "int"

So how do I annotate the code correctly for mypy to not complain (of course both versions behave exactly the same during runtime and work as expected)?

The "None"-element at the end of the list is needed in the "do something"-part of the script (which is not relevant for the problem and therefore not shown here)

EDIT (19/09/2022) START As requested, this is the productive code:

from typing import List, Optional

def parse_job_attributes(string: str, tokens: List[str]) -> List[str]:
    indices = []
    for token in tokens:
        if token in string.lower():
            indices.append(string.lower().index(token))
    sorted_indices: List[Optional[int]] = list(sorted(indices))

    # indices = sorted(indices)
    if len(sorted_indices) > 0:
        sorted_indices.append(None)
    parts = [string[sorted_indices[i]:sorted_indices[i + 1]] for i in range(len(sorted_indices) - 1)]
    return parts

EDIT END

3 Answers

Instead of assigning the sorted list back to the original variable my_list, assign it to a new variable which mypy will allow to hold different types.

my_list: List[int] = list()
for i in range(7):
    my_list.append(i)
# change here
sorted_list: List[Any] = sorted(my_list)
if len(sorted_list) > 0:
    sorted_list.append(None)

Based on crunker99's answer that correctly pointed out the new list I was able to come up with this code that satisfies mypy:

from typing import List, Optional

my_list: List[int] = list()
for i in range(7):
    my_list.append(i)
my_sorted_list: List[Optional[int]] = list(sorted(my_list))
if (len(my_sorted_list) > 0):
    my_sorted_list.append(None)
# do something with my_sorted_list

You can get around the problem by looking at what your actual code is doing: None as the endpoint of the slice means len(string), so if you put that instead, then your list can stay List[int].

from typing import List

def parse_job_attributes(string: str, tokens: List[str]) -> List[str]:
    indices = []
    for token in tokens:
        if token in string.lower():
            indices.append(string.lower().index(token))
    indices.sort()

    if len(indices) > 0:
        indices.append(len(string))
    parts = [string[indices[i]:indices[i + 1]] for i in range(len(indices) - 1)]
    return parts

This passes mypy --strict.

Example output, just to be sure:

>>> parse_job_attributes('re_foobar', ['foo', 're'])
['re_', 'foobar']

As well, since you're subtracting one from the len in range(len(indices) - 1), there's not even any need to check if len(sorted_indices) > 0; you can append unconditionally, and the range will still be empty. That opens up some other solutions, like:

sorted_indices: List[Optional[int]] = [*sorted(indices), None]

P.S. Note that this whole answer is based on the "do something" part, which you thought was unimportant. I don't bring it up to shame you or anything, but to emphasize that it's important to provide working code to avoid the XY problem.

Related