跳转到主要内容

验证python 3.7+文件使用from __future__ import annotations

项目描述

flake8-future-annotations

Python 3.7+ PyPI version GitHub license Downloads

如果模块中使用了可以使用PEP 563重写的类型,则验证python 3.7+文件使用from __future__ import annotations

pyupgrade配合使用--py37-plus标志或更高版本,因为pyupgrade仅在存在from __future__ import annotations时,才使用PEP 563规则替换类型注释。

flake8代码

代码 描述
FA100 如果模块中使用的类型可以使用PEP563重写,则缺少导入
FA101 当没有可用PEP563重写时(参见配置)缺少导入
FA102 当代码使用简化的类型(列表、字典、集合等)时缺少导入

示例

import typing as t
from typing import List

def function(a_dict: t.Dict[str, t.Optional[int]]) -> None:
    a_list: List[str] = []
    a_list.append("hello")

因此,此插件将发出

hello.py:1:1: FA100 Missing from __future__ import annotations but imports: List, t.Dict, t.Optional

添加未来注释导入后,运行pyupgrade允许代码自动重写为

from __future__ import annotations

def function(a_dict: dict[str, int | None]) -> None:
    a_list: list[str] = []
    a_list.append("hello")

配置

如果设置了--force-future-annotations选项,则无论根据PEP 563是否有可用的重写,都会报告缺少from __future__ import annotations;在这种情况下,使用FA101而不是FA100。

如果设置了--check-future-annotations选项,则会报告缺失from __future__ import annotations,因为以下代码将在低于Python 3.10的版本上出错(此检查对3.10及以上版本不输出任何内容)

def function(a_dict: dict[str, int | None]) -> None:
    a_list: list[str] = []
    a_list.append("hello")
hello.py:1:1: FA102 Missing from __future__ import annotations but uses simplified type annotations: dict, list, union

项目详情


下载文件

下载适用于您平台的文件。如果您不确定该选择哪个,请了解更多关于安装包的信息。

源分布

flake8-future-annotations-1.1.0.tar.gz (6.8 kB 查看散列值)

上传时间

构建分布

flake8_future_annotations-1.1.0-py3-none-any.whl (10.4 kB 查看散列值)

上传时间 Python 3

由以下组织支持