Skip to main content

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.

Release files for flake8-import-restrictions 2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flake8-import-restrictions 2.1
File Size Uploaded
flake8_import_restrictions-2.1.tar.gz 8.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for flake8-import-restrictions 2.1
File Interpreter ABI Platform
flake8_import_restrictions-2.1-py3-none-any.whl Python 3 none any Details

Total release size: 16.2 kB

Release files / flake8_import_restrictions-2.1.tar.gz

Download URL flake8_import_restrictions-2.1.tar.gz
Size 8.7 kB
Tags Source
SHA-256 checksum
How to use checksums
f27cb026e45ca60adfe7281f7145a250dbcc289b7cbc877a8cd295ee935bd56c
BLAKE2b-256 checksum
How to use checksums
cd25edfb89dded3644c3371c706b9f7165a9f05deb1137960fdbf93cc96760e7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.5

Release files / flake8_import_restrictions-2.1-py3-none-any.whl

Download URL flake8_import_restrictions-2.1-py3-none-any.whl
Size 7.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
77522e76b82a6ccb25820b1e195825a695094ceded23a18c1b5ce026c23e1071
BLAKE2b-256 checksum
How to use checksums
f6619155263732f085bf757d6687a06d31499d1b13a54a3dd903ad41573cf5c2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.5

Release history Release notifications | RSS feed

This release

2.1 This release

2 release files

2.0.1

2 release files

2.0

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.1

2 release files

1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page