初识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中的类型标注

你可能感兴趣的:(小白学Python,typing,hints,mypy)