以下所有例子都参考了最新版本的 Python 文档与 mypy 文档

必备条件

安装最新版本的 Python 和 mypy

要学会按需配置自己的编辑器,比如我的 VSCode 就装好了 Python 和 Pyright 扩展

变量

age: int = 1

child: bool
if age < 18:
child = True
else:
child = False

常量

from typing import Final

RATE: Final = 3000

class Base:
DEFAULT_ID: Final = 0 RATE = 300 # Error: can't assign to final attribute
Base.DEFAULT_ID = 1 # Error: can't override a final attribute

内置类型

from typing import List, Set, Dict, Tuple, Optional

x: int = 1
x: float = 1.0
x: bool = True
x: str = "test"
x: bytes = b"test" # 集合类型是首字母大写的
# 元素的类型写在中括号里面(泛型)
x: List[int] = [1]
x: Set[int] = {6, 7} # 字典需要写出 key 和 value 的类型
x: Dict[str, float] = {"field": 2.0} # 元组需要写出所有元素的类型
x: Tuple[int, str, float] = (3, "yes", 7.5) # 用 Optional[] 表示可以为 None 的类型
x: Optional[str] = some_function()
# mypy 可以推断出 if 语句里 x 不能为 None
if x is not None:
print(x.upper())
# 如果 x 的值不可能为 None, 用 assert
assert x is not None
print(x.upper())

函数

from typing import Callable, Iterator, Union, Optional, List

def stringify(num: int) -> str:
return str(num) def plus(num1: int, num2: int) -> int:
return num1 + num2 def f(num1: int, my_float: float = 3.5) -> float:
return num1 + my_float # callable (函数) 类型
x: Callable[[int, float], float] = f # 生成器函数会返回可迭代的元素
def g(n: int) -> Iterator[int]:
i = 0
while i < n:
yield i
i += 1

from typing import ClassVar

class MyClass:
attr: int
# 实例变量可以有默认值
charge_percent: int = 100 # 什么也不返回,就是返回 None def __init__(self) -> None:
... # 实例方法,省略 self 的类型
def my_method(self, num: int, str1: str) -> str:
return num * str1 # 类可以用作类型
x: MyClass = MyClass() # 类变量
class Car:
seats: ClassVar[int] = 4
passengers: ClassVar[List[str]] # 需要注意还没定义就使用一个类会报错
def f(foo: A) -> int: # Error
... class A:
... # 你可以使用字符串的形式来规避
def f(foo: "A") -> int: # OK
...

Named tuples 命名元组

from typing import NamedTuple

class Point(NamedTuple):
x: int
y: int p = Point(x=1, y="x") # Error: Argument has incompatible type "str"; expected "int"

异步迭代器

from typing import AsyncIterator

async def gen() -> AsyncIterator[bytes]:
lst = [b async for b in gen()] # 推断类型是 "List[bytes]"
yield "no way" # Error: Incompatible types (got "str", expected "bytes")

类型别名

AliasType = Union[List[Dict[Tuple[int, str], Set[int]]], Tuple[str, List[str]]]

def f() -> AliasType:
...

Dataclasses 数据类

from dataclasses import dataclass, field

@dataclass
class Application:
name: str
plugins: List[str] = field(default_factory=list) test = Application("Testing...") # OK
bad = Application("Testing...", "with plugin") # Error: List[str] expected

泛型

from typing import TypeVar, Generic

T = TypeVar("T")

class Stack(Generic[T]):    # 泛型
def __init__(self) -> None:
self.items: List[T] = [] def push(self, item: T) -> None:
self.items.append(item) def pop(self) -> T:
return self.items.pop() def empty(self) -> bool:
return not self.items # Stack[int] 实例
stack = Stack[int]()
stack.push(2)
stack.pop()
stack.push("x") # Type error

Literal types

PrimaryColors = Literal["red", "blue", "yellow"]
SecondaryColors = Literal["purple", "green", "orange"]
AllowedColors = Literal[PrimaryColors, SecondaryColors] def paint(color: AllowedColors) -> None: ... paint("red") # OK
paint("turquoise") # Error

Protocol 协议

实现结构化子类型(静态鸭子类型),可以当成接口一样用。

from typing import Iterable, Protocol

class SupportsClose(Protocol):
def close(self) -> None:
... # 函数体可以为空 (explicit '...') class Resource: # 没有写 SupportsClose
def close(self) -> None:
self.resource.release() def close_all(items: Iterable[SupportsClose]) -> None:
for item in items:
item.close() close_all([Resource(), open("some/file")]) # OK

Abstractmethod 抽象方法

方法可以有默认的实现,但是抽象方法规定必须在子类中实现。

from typing import Protocol
from abc import abstractmethod class Example(Protocol):
def first(self) -> int:
return 42 @abstractmethod
def second(self) -> int: # 没有默认实现
raise NotImplementedError # 防止这个方法被调用

Enum 枚举

如果要像 C 语言一样定义枚举的话,也是用类来实现的。

from enum import Enum

class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3 print(Color.RED)

这样就能用类当成枚举一样用了。

参考资料

Python 静态类型检查 mypy 示例的更多相关文章

  1. Java中静态类型检查是如何进行的

    以下内容来自维基百科,关于静态类型检查和动态类型检查的解释: 静态类型检查:基于程序的源代码来验证类型安全的过程: 动态类型检查:在程序运行期间验证类型安全的过程: Java使用静态类型检查在编译期间 ...

  2. Python静态代码检查工具Flake8

    简介 Flake8 是由Python官方发布的一款辅助检测Python代码是否规范的工具,相对于目前热度比较高的Pylint来说,Flake8检查规则灵活,支持集成额外插件,扩展性强.Flake8是对 ...

  3. 理解Flow静态类型检查

    一.为什么在JavaScript中使用静态类型 了解静态类型的最快方法是将其与动态类型进行对比. 有静态类型参数的语言被称为静态类型语言. 另一方面,有动态类型参数的语言被称为动态类型语言.核心区别是 ...

  4. 编译器开发系列--Ocelot语言6.静态类型检查

    关于"静态类型检查",想必使用C 或Java 的各位应该非常熟悉了.在此过程中将检查表达式的类型,发现类型不正确的操作时就会报错.例如结构体之间无法用+ 进行加法运算,指针和数值之 ...

  5. Flow: JavaScript静态类型检查工具

    Flow: JavaScript静态类型检查工具 Flow是Facebook出品的,针对JavaScript的静态类型检查工具.其代码托管在github之上,并遵守BSD开源协议. 关于Flow 它可 ...

  6. flow 静态类型检查 js

    1.flow介绍 https://ustbhuangyi.github.io/vue-analysis/prepare/flow.html#为什么用-flow 2.使用 (1)安装flow (2)项目 ...

  7. 如何使用flow进行静态类型检查

    Flow 是 facebook 出品的 JavaScript 静态类型检查⼯具.Vue.js 的源码利⽤了 Flow 做了静态类型检查,所以了解 Flow 有助于我们阅读源码. 为什么⽤ Flow? ...

  8. Python 加入类型检查

    Python 是一门强类型的动态语言, 对于一个 Python 函数或者方法, 无需声明形参及返回值的数据类型, 在程序的执行的过程中, Python 解释器也不会对输入参数做任何的类型检查, 如果程 ...

  9. flow JavaScript 静态类型检查工具

    内置类型 flow 内置类型有 boolean, number, string, null, void, any, mixed, literal type. 其中 boolean, number, s ...

随机推荐

  1. 深入SpringMVC注解

    原文链接:https://blog.csdn.net/chenpeng19910926/article/details/70837756 @Controller 在SpringMVC 中提供了一个非常 ...

  2. Redis搭建哨兵模式

    一 安装Redis 1. 从https://redis.io/download redis官网下载二进制包安装 例如:wget http://download.redis.io/releases/re ...

  3. gulp实现自动化打包(二)

    引言 在这篇文章中我基于上一篇文章gulp的简单打包示例(一)的代码(重点,不然看的懵逼状态)来介绍gulp的自动化打包,主要是修改gulpfile.js配置文件.当我们执行gulp任务,gulp自动 ...

  4. HanLP《自然语言处理入门》笔记--5.感知机模型与序列标注

    笔记转载于GitHub项目:https://github.com/NLP-LOVE/Introduction-NLP 5. 感知机分类与序列标注 第4章我们利用隐马尔可夫模型实现了第一个基于序列标注的 ...

  5. 暑假第一周总结(在centos虚拟机上安装jdk以及hadoop并对hadoop进行配置)

    本周主要就是对虚拟机进行安装并在上边安装jdk以及hadoop并对其进行配置. 在看林子雨老师的教程时,下载了老师所给的全套的下载软件,在安装时发现老师所给的VirtualBox安装后无法正常启动,尝 ...

  6. 最简单的基于FFMPEG+SDL的视频播放器:拆分-解码器和播放器

    ===================================================== 最简单的基于FFmpeg的视频播放器系列文章列表: 100行代码实现最简单的基于FFMPEG ...

  7. nginx+lua在我司的实践

    导读:nginx是一个高性能的反向代理服务器,lua是一个小巧的脚本语言,这两个的巧妙结合会擦出怎样的火花呢. 关键词:nginx,lua,nginx+lua 前言 nginx,lua,nginx+l ...

  8. Java数据结构系列(1)——自平衡二叉树

    1.基本概念 所谓自平衡二叉树,就是当我们插入或删除元素之后,二叉树的高度会自动调整到最小,这样我们就可以在对数时间内查找二叉树内的元素. 2.定义 TreeSet<Elemtype> s ...

  9. DRF框架的安装与使用

    目录 DRF框架的安装与配置 基于restful接口规范的接口设计 DRF框架的安装与配置 """ 1)安装 >: pip install djangorestfr ...

  10. 从敏捷开发到微服务,maybe再到中台

    -- 先说下准备这个的背景: 本来是想让我分享下敏捷开发,可能是听我说为as**搭建并完善了敏捷开发体系的原因吧. 我一般分享一个东西,希望大家能真的理解,而不只是知道. 我不大相信有万能的东西,不希 ...