Skip to main content

Flake8 and pylama plugin that checks the ordering of import statements.

Project description

Build Status

A flake8 and Pylama plugin that checks the ordering of your imports. It does not check anything else about the imports. Merely that they are grouped and ordered correctly.

In general stdlib comes first, then 3rd party, then local packages, and that each group is individually alphabetized, however this depends on the style used. Flake8-Import-Order supports a number of styles and is extensible allowing for custom styles.

This plugin was originally developed to match the style preferences of the cryptography project, with this style remaining the default.

Warnings

This package adds 4 new flake8 warnings

  • I100: Your import statements are in the wrong order.

  • I101: The names in your from import are in the wrong order.

  • I201: Missing newline between sections or imports.

  • I202: Additional newline in a section of imports.

Styles

The following styles are directly supported,

  • cryptography - see an example

  • google - style described in Google Style Guidelines, see an example

  • smarkets - style as google only with import statements before from X import … statements, see an example

  • appnexus - style as google only with import statements for packages local to your company or organisation coming after import statements for third-party packages, see an example

  • edited - style as smarkets only with import statements for packages local to your company or organisation coming after import statements for third-party packages, see an example

  • pep8 - style that only enforces groups without enforcing the order within the groups

You can also add your own style by extending Style class.

Configuration

You will want to set the application-import-names option to a comma separated list of names that should be considered local to your application. These will be used to help categorise your import statements into the correct groups. Note that relative imports are always considered local.

You will want to set the application-package-names option to a comma separated list of names that should be considered local to your company or organisation, but which are obtained using some sort of package manager like Pip, Apt, or Yum. Typically, code representing the values listed in this option is located in a different repository than the code being developed. This option is only accepted in the supported appnexus or edited styles or in any style that accepts application package names.

import-order-style controls what style the plugin follows (cryptography is the default).

Limitations

Currently these checks are limited to module scope imports only. Conditional imports in module scope will also be ignored.

Classification of an imported module is achieved by checking the module against a stdlib list and then if there is no match against the application-import-names list and application-package-names if the style accepts application-package names. Only if none of these lists contain the imported module will it be classified as third party.

These checks only consider an import against its previous import, rather than considering all the imports together. This means that I100 errors are only raised for the latter of adjacent imports out of order. For example,

import X.B
import X  # I100
import X.A

only import X raises an I100 error, yet import X.A is also out of order compared with the import X.B.

Imported modules are classified as stdlib if the module is in a vendored list of stdlib modules. This list is based on the latest release of Python and hence the results can be misleading. This list is also the same for all Python versions because otherwise it would be impossible to write programs that work under both Python 2 and 3 and pass the import order check.

Extending styles

You can add your own style by extending flake8_import_order.styles.Style class. Here’s an example:

from flake8_import_order.styles import Cryptography


class ReversedCryptography(Cryptography):
    # Note that Cryptography is a subclass of Style.

    @staticmethod
    def sorted_names(names):
        return reversed(Cryptography.sorted_names(names))

By default there are five import groupings or sections; future, stdlib, third party, application, and relative imports. A style can choose to accept another grouping, application-package, by setting the Style class variable accepts_application_package_names to True, e.g.

class PackageNameCryptography(Cryptography):
    accepts_application_package_names = True

To make flake8-import-order able to discover your extended style, you need to register it as flake8_import_order.styles using setuptools’ entry points mechanism:

# setup.py of your style package
setup(
    name='flake8-import-order-reversed-cryptography',
    ...,
    entry_points={
        'flake8_import_order.styles': [
            'reversed = reversedcryptography:ReversedCryptography',
            # 'reversed' is a style name.  You can pass it to
            # --import-order-style option
            # 'reversedcryptography:ReversedCryptography' is an import path
            # of your extended style class.
        ]
    }
)

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-order-0.15.tar.gz (16.7 kB view details)

Uploaded Source

Built Distribution

flake8_import_order-0.15-py2.py3-none-any.whl (15.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file flake8-import-order-0.15.tar.gz.

File metadata

File hashes

Hashes for flake8-import-order-0.15.tar.gz
Algorithm Hash digest
SHA256 fd8608a29df93589ecbe5281981222929261c915068f2aedfb51f243f05f5466
MD5 d2623d25f5270886149f6e309480d0a9
BLAKE2b-256 6fa551162d23b1cfe235a44927521124daf95afe5cb5384a41b0e53e54a2a2f5

See more details on using hashes here.

Provenance

File details

Details for the file flake8_import_order-0.15-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for flake8_import_order-0.15-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 68edbbf4089af57bd303ff51bb2aa2df24d266c23308db3e6b47357d1f80ad60
MD5 2b7da4c28705a5860ec89a22bf4b43a9
BLAKE2b-256 6ca468056e3d6ebf820970ddcad5fe0271a5c0723799ed3df7b002a9c5842fb1

See more details on using hashes here.

Provenance

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