初識Python模塊typing、mypy類型註釋

  • type notation 類型註釋

    PEP484作爲核心,周邊多個PEP構成的type hints矩陣,目前普及度並不高,上次看到相關應用還是cython加速,需要用到類型註解。

    docstring是寫給人看到,type hint更多是寫給機器看的。

    能夠讓IDE瞭解variable是什麼類型,因此便於IDE進行自動補全。

    在運行時會過濾掉類型信息。

    談到type就需要考慮到靜態語言與動態語言孰優孰劣的問題。參見(英文原文中文翻譯),文中得出了靜態語言比動態語言更好維護的結論,這也與reference2 中給出的type hint的優點一致:看代碼要比寫代碼多得多,便於維護很重要。

    It’s usually faster to write new code in a dynamically-typed language like Python because you don’t have to write out all the type declarations by hand. But when your codebase starts to get large, you’ll inevitably run into lots of runtime bugs that static typing would have prevented.

    It’s not all-or-nothing. So you can start by declaring types in the places that are the most valuable without changing all your code.

  • The Theory of Type Hints (PEP483)

    1. Subtype relationships

      A crucial notion for static type checker is the subtype relationship.

      It arises from the question: if first_var has type first_type, and second_var has type second_type, is it safe to assign first_var = second_var?

    2. type vs. class

      In Python, classes are object factories defined by the class statement, and returned by the type(obj) built-in function. Class is a dynamic, runtime concept.

      types appear in variable and function type annotations, can be constructed from building blocks described below, and are used by static type checkers.

    3. fundamental building blocks

      • Any
      • Union[t1, t2, …]
      • Optional[t1]
      • Tuple[t1, t2, …, tn]
      • Callable[[t1, t2, …, tn], tr] 函數,變量爲t1,t2,…,返回值爲tn
      • Intersection[t1, t2, …]
    4. Generic types

      The fundamental building blocks defined above allow to construct new types in a generic manner.

  • typing

    From Document, This module provides runtime support for type hints as specified by PEP484 and others.

    The most fundamental support consists of the types Any, Union, Tuple, Callable, TypeVar and Generic.

    variable: type
    my_string: str = "My String Value"
    def greeting(name: str) -> str:
    	return "Hello" + name
    
  • mypy

    Mypy is an optional static type checker for Python that aims to combine the benefits of dynamic(or “duck”) typing and static typing.

    Mypy combines the expressive power and convenience of Python with a powerful type system and compile-time type checking.

    Mypy type checks standard Python programs, run them using any Python VM with basically no runtime overhead.

    Migrate existing code to static typing, a function at a time. you can freely mix static and dynamic typing within a program, within a module or within an expression. No need to give up dynamic typing – use static typing when it makes sense. Often just adding function signatures gives you statically typed code.

    Through mypy program.py , it will check for errors without ever running your code, just like a linter.

    You can find many of more complex static types inside of the typing module.

  • tutorial

    1. Python Type Checking (Guide)
  • Reference

  1. 知乎 : 如何看待類型註解在Python中的前途
  2. 全面理解Python中的類型提示(Type Hints) --> 雄文長篇
  3. Python中的類型標註
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章