Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

type declaration type object is not subscriptable list

def words(string: str) -> list[str]:
    return string.split(' ')

Running this raises the error

TypeError: 'type' object is not subscriptable

How do I correctly specify that the output will be a list of strings?


1 Answers

For now, you need to write the following:

from typing import List


def words(string: str) -> List[str]:
    return string.split(' ')

Note the capitalisation of List vs list. There’s a PEP for making your code work going forward, starting with Python 3.9.

like image 184
Konrad Rudolph Avatar answered Sep 16 '25 07:09

Konrad Rudolph