user3310334
user3310334

Reputation:

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?

Upvotes: 4

Views: 3322

Answers (1)

Konrad Rudolph
Konrad Rudolph

Reputation: 545588

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.

Upvotes: 4

Related Questions