Reputation:
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
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