Static typing in python3: list vs List [duplicate]

Not all lists are the same from a typing perspective. The program

def f(some_list: list):
    return [i+2 for i in some_list]

f(['a', 'b', 'c'])

won’t fail a static type checker, even though it won’t run. By contrast, you can specify the contents of the list using the abstract types from typing

def f(some_list: List[int]) -> List[int]:
    return [i+2 for i in some_list]

f(['a', 'b', 'c'])

will fail, as it should.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)