Typing
Type Check
  • Python does not implement type checking, use mypy to implement check
  • run it as part of your unit tests
  • #!/usr/bin/python
    
    from typing import List
    
    # declare variable type
    # variable : type
    numbers: List[int] = list(range(4))
    
    # numbers2: List[str] = list(range(4)) # error by mypy
    
    print(numbers)
    
    # decleare function input and output types
    # def function_name(parameter1: type) -> return_type:
    def get_info(m: str) -> str:
        return 'Info: ' + m
    
    def get_info2(m: int) -> str:
        return 'Info: ' + str(m)
    
    print(get_info2('Hello')) # error by mypy
    		
    Multiple Types
  • @overload-decorated definitions are for the benefit of the type checker only, they are not called at runtime
  • non-@overload-decorated definition will be called at runtime
  • #!/usr/bin/python
    
    import typing
    
    @typing.overload
    def get_info(m: str) -> str:
        print('Call str type ...')
        return 'Info: ' + str(m)
    
    @typing.overload
    def get_info(m: int) -> str:
        print('Call int type ...')
        return 'Info: ' + str(m)
    
    def get_info(m):
        print('Call general function ...')
        return 'Info: ' + str(m)
    
    # general function will be call at run time
    print(get_info(100))
    print(get_info('Hello'))
    print(get_info(3.14)) # error by type checker
    		
  • Use Union to define either type contained in Union[]
  • #!/usr/bin/python
    
    import typing
    
    def get_info(m : typing.Union[int, str]) -> str:
        print('Call general function ...')
        return 'Info: ' + str(m)
    
    print(get_info(100))
    print(get_info('Hello'))
    print(get_info(3.14)) # error by type checker
    		
    Typing types
    #!/usr/bin/python
    
    from typing import List, Tuple, Dict, Set
    
    l: List[int] = list(range(4))
    t: Tuple[int, int] = (1, 2)
    d: Dict[str, int] = {'Age': 38}
    s: Set[int] = set([3, 2, 5, 6])
    
    l_d: List[Dict[str, int]] = [{'Age': 39}, {'ID': 1}]
    		
    Any
  • Take any data type
  • #!/usr/bin/python
    
    import typing
    
    def get_info(m : typing.Any) -> str:
        return 'Info: ' + str(m)
    
    print(get_info(100))
    print(get_info('Hello'))
    print(get_info(3.14))
    		
    None
    #!/usr/bin/python
    
    import typing
    
    def get_info(m : typing.Any) -> None:
        print('Info: ' + str(m))
        return 'Done' # error by type checker
    
    get_info(100)
    		
    Type Aliase
    #!/usr/bin/python
    
    import typing
    
    Lint = typing.List[int]
    
    def get_info(m : Lint) -> None:
        print('Info: ' + str(m))
    
    get_info([1, 2, 3, 4])
    get_info(100) # error by type checker
    		
    Generics
    #!/usr/bin/python
    
    import typing
    
    T = typing.TypeVar('T', str, int)
    
    def get_info(m : T, n : T) -> str:
        return str(m) + str(n)
    
    print(get_info('Hello', 'World!'))
    print(get_info(1, 2))
    print(get_info(1, 'Hello')) # error by type checker
    print(get_info(1, 3.14)) # error by type checker
    		
    Reference
  • Python STL Library
  • A BRIEF INTRODUCTION TO MYPY
  • mpy
  • How to Use Static Type Checking in Python 3.6