Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

flake8-import-restrictions

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

flake8-import-restrictions

A flake8 plugin used to disallow certain forms of imports.

  • 2.1
  • PyPI
  • Socket score

Maintainers
1

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.

Keywords

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc