4 ms·
Sorry for posting this issue here: python3 type_annotations.py def sum_and_stringify(nums: List[int]) -> str: NameError: name 'List' is not defined ED
by singularity2001 10y ago
Sorry for posting this issue here:
python3 type_annotations.py
def sum_and_stringify(nums: List[int]) -> str:
NameError: name 'List' is not defined
EDIT: Solved: Either just list or List[int] with import:
from typing import List
Thanks for all the help guys! By the way I took the example from the mypy page. A classical case of 'critical line missing from example code' (as perfectioned by Bjarne Stroustrup back when)
- ddorian43 10y agoI think you have to import that.
- singularity2001 10y agoimport List?? At least this works: def sum_and_stringify(nums: list) -> str:
- dcre 10y agoThe built-in constructor is `list`, not `List`.
- singularity2001 10y agoThanks for all the help guys! by the way I took the example from the mypy page def sum_and_stringify(nums: list[int]) -> str: TypeError: 'type' object is not subscriptable so either just list or List[int] with import, got it. A classical case of 'critical line missing from example code' (as perfectioned by Bjarne Stroustrup back when)
- ddfisher 10y agoYeah, you need to write `from typing import List` to get the generic List class that you can parameterize with an item type. As you pointed out, you can use the builtin lowercase-l list without importing anything, but that doesn't allow you to specify the item type.
- ddorian43 10y agoNo. Try: from typing import List, TYPE_CHECKING How I found it: quick google search for "mypy validate list"