Skip to main content

A flake8 plugin used to disallow certain forms of imports.

Project description

flake8-import-restrictions

Build Status Build Status Build Status Build Status

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 IMR2xx listed below, there are options --imr2xx_include and --imr2xx_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, IMR200, IMR201, IMR202, IMR221, IMR223, IMR241, and IMR243 include all (*) modules. Only IMR241 excludes the typing module from checks, the other errors have no excludes by default.

General Import Errors

IMR200

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")

IMR201

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

IMR202

Alias identifiers should not have the same name as the imported object.

# Bad
import sys as sys

# Good
import sys

import Syntax Errors

IMR220

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

IMR221

When using the import syntax, each import statement should only import one module.

# Bad
import sys, os

# Good
import sys
import os

IMR222

The import syntax should not be used.

IMR223

When using the import syntax, do not duplicate module names in the as segment.

# Bad
import os.path as path

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

from Syntax Errors

IMR240

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

IMR241

When using the from syntax, only submodules are imported, not module elements.

# Bad
from os.path import join

# Good
from os import path

IMR242

When using the from syntax, only module elements are imported, not submodules.

# Bad
from os import path

# Good
from os.path import join

IMR243

When using the from syntax, import * should not be used.

# Bad
from os.path import *

# Good
from os.path import join

IMR244

Relative imports should not be used.

# Bad
from . import foo

# Good
from flake8_import_restrictions import foo

IMR245

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

flake8_import_restrictions-2.1.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

flake8_import_restrictions-2.1-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file flake8_import_restrictions-2.1.tar.gz.

File metadata

File hashes

Hashes for flake8_import_restrictions-2.1.tar.gz
Algorithm Hash digest
SHA256 f27cb026e45ca60adfe7281f7145a250dbcc289b7cbc877a8cd295ee935bd56c
MD5 788eb087c64250c046b163c666482ddf
BLAKE2b-256 cd25edfb89dded3644c3371c706b9f7165a9f05deb1137960fdbf93cc96760e7

See more details on using hashes here.

File details

Details for the file flake8_import_restrictions-2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for flake8_import_restrictions-2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 77522e76b82a6ccb25820b1e195825a695094ceded23a18c1b5ce026c23e1071
MD5 a3d84af810904fcb42be5ced39c09fa0
BLAKE2b-256 f6619155263732f085bf757d6687a06d31499d1b13a54a3dd903ad41573cf5c2

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page