验证python 3.7+文件使用from __future__ import annotations
项目描述
flake8-future-annotations
如果模块中使用了可以使用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的散列值
算法 | 散列摘要 | |
---|---|---|
SHA256 | df416bd2b9e1eda7ea639a5fd2a083dabb942ffe49d197f836df380d0dcf6608 |
|
MD5 | ed3c8f57ec74601c090dff6e2d9c40ab |
|
BLAKE2b-256 | ceebeee3a350b4c1cae50f9040e78b80b39a3a0d7a5165c5837b68bff00513b3 |
关闭
flake8_future_annotations-1.1.0-py3-none-any.whl的散列值
算法 | 散列摘要 | |
---|---|---|
SHA256 | 555f16f51ae24ab4d0683b1ce8d0f59d36259c3a7e776bd5642f58c78ce7d3ab |
|
MD5 | 38abc98630f34d96e5e79d20034fc360 |
|
BLAKE2b-256 | 45e4e3e6f788d9d5b5ff9a5fe2ceb6aaff9dea248aaa24c0217866c65fa5d0bf |