A flake8 plugin used to disallow certain forms of imports.
Project description
flake8-import-restrictions
A flake8 plugin used to disallow certain forms of imports.
This plugin talks about the import
syntax (import X.Y.Z [as foo]
)
and the from
syntax (from X.Y import Z [as foo]
). It talks about
import
segments (import X
), from
segments (from Y
), and as
segments (as Z
).
Options
For every error I20xx
listed below, there are options --i20xx_include
and --i20xx_exclude
which are passed a comma separated list of UNIX wildcard patterns each. The error
will then only be reported on imports of modules that match a include pattern but no exclude
pattern.
By default, I2000, I2001, I2002, I2021, I2041, and I2043 include all (*
) modules. Only I2041 excludes the
typing
module from checks, the other errors have no excludes by default.
General Import Errors
I2000
Imports should only happen on module level, not locally.
# 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
Alias identifiers defined from as
segments should be at
least two characters long.
# Bad
import os.path as p
# Good
import os.path as path
I2002
Alias identifiers should not have the same name as the imported object.
# Bad
import sys as sys
# Good
import sys
import
Syntax Errors
I2020
When using the import
syntax, if the imported module is a submodule,
i.e. not a top level module, an as
segment should be present.
# Bad
import os.path
# Good
import sys
import os.path as path
I2021
When using the import
syntax, each import statement should
only import one module.
# Bad
import sys, os
# Good
import sys
import os
I2022
The import
syntax should not be used.
from
Syntax Errors
I2040
When using the from
syntax, the import
segment only contains one
import.
# Bad
from os import path, environ
# Good
from os import path
from os import environ
I2041
When using the from
syntax, only submodules are imported, not
module elements.
# Bad
from os.path import join
# Good
from os import path
I2042
When using the from
syntax, only module elements are imported,
not submodules.
# Bad
from os import path
# Good
from os.path import join
I2043
When using the from
syntax, import *
should not be used.
# Bad
from os.path import *
# Good
from os.path import join
I2044
Relative imports should not be used.
# Bad
from . import foo
# Good
from flake8_import_restrictions import foo
I2045
The from
syntax should not be used.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for flake8-import-restrictions-1.0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 67c9e593e77798765b54729ad0b90e9d40a923b03c2a8193f3a0ce48c2e698bd |
|
MD5 | 928d5d00dcd1be490710c214dcad9fec |
|
BLAKE2b-256 | 6cbb8e970ffcbb2cb5736727286fa1d9f2605e57334a49a1f4a51008a51fea26 |
Hashes for flake8_import_restrictions-1.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3797033a30dab4878aaf2310dc1cb640f6ea05f3c33d7a81c90cc6713b459a33 |
|
MD5 | 291bd95c024df4ebf3aa95a3f9a731dd |
|
BLAKE2b-256 | a3c34218911136281992dd1c45e8fb9e23e4316c56e5b88ad3ee2919f1e4ca62 |