Skip to main content

用于禁止某些形式的导入的 flake8 插件。

项目描述

flake8-进口限制

构建状态 构建状态 构建状态 构建状态

用于禁止某些形式的导入的 flake8 插件。

这个插件讨论了import语法 ( import X.Y.Z [as foo]) 和from语法 ( from X.Y import Z [as foo])。它讨论了 import段 ( import X)、from段 ( from Y) 和as 段 ( as Z)。

选项

对于下面列出的每个错误I20xx,都有选项--i20xx_include,每个选项--i20xx_exclude 都传递一个逗号分隔的 UNIX 通配符模式列表。然后只会在导入匹配包含模式但不匹配排除模式的模块时报告错误。

默认情况下,I2000、I2001、I2002、I2021、I2023、I2041 和 I2043 包括所有 ( *) 模块。只有 I2041 将 typing模块排除在检查之外,其他错误默认没有排除。

一般导入错误

I2000

导入应该只发生在模块级别,而不是本地。

# Bad
def f():
    import os.path
    return os.path.join("a", "b")

# Good
import os.path
def f():
    return os.path.join("a", "b")

I2001

从段定义的别名标识符as应至少有两个字符长。

# Bad
import os.path as p

# Good
import os.path as path

I2002

别名标识符不应与导入的对象同名。

# Bad
import sys as sys

# Good
import sys

import语法错误

2020年

使用import语法时,如果导入的模块是子模块,即不是顶级模块,则as应该存在一个段。

# Bad
import os.path

# Good
import sys
import os.path as path

I2021

使用该import语法时,每个导入语句应该只导入一个模块。

# Bad
import sys, os

# Good
import sys
import os

I2022

import不应使用该语法。

I2023

使用import语法时,不要在as 段中重复模块名称。

# Bad
import os.path as path

# Good
from os import path
import os.path as ospath

from语法错误

I2040

使用该from语法时,该import段仅包含一个导入。

# Bad
from os import path, environ

# Good
from os import path
from os import environ

I2041

使用from语法时,只导入子模块,而不是模块元素。

# Bad
from os.path import join

# Good
from os import path

I2042

使用from语法时,只导入模块元素,不导入子模块。

# Bad
from os import path

# Good
from os.path import join

I2043

使用from语法时,import *不应使用。

# Bad
from os.path import *

# Good
from os.path import join

I2044

不应使用相对导入。

# Bad
from . import foo

# Good
from flake8_import_restrictions import foo

I2045

from不应使用该语法。

项目详情


下载文件

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

源分布

flake8-import-restrictions-1.1.1.tar.gz (7.0 kB 查看哈希

已上传 source

内置分布

flake8_import_restrictions-1.1.1-py3-none-any.whl (7.4 kB 查看哈希

已上传 py3