Skip to main content

flake8-import-order

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.

I201 only checks that groups of imports are not consecutive and only takes into account the first line of each import statement. This means that multi-line from imports, comments between imports and so on may cause this error not to be raised correctly in all situations. This restriction is due to the data provided by the stdlib ast module.

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.
        ]
    }
)

Metadata

Release files for flake8-import-order 0.14.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-order 0.14.1
File Size Uploaded Provenance Actions
flake8-import-order-0.14.1.tar.gz 16.0 kB Without provenance Details for flake8-import-order-0.14.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for flake8-import-order 0.14.1
File Interpreter ABI Platform Actions
flake8_import_order-0.14.1-py2.py3-none-any.whl Python 3, Python 2 none any Details for flake8_import_order-0.14.1-py2.py3-none-any.whl

Total release size: 31.2 kB

Release files / flake8-import-order-0.14.1.tar.gz

Download URL flake8-import-order-0.14.1.tar.gz
Size 16.0 kB
Tags Source
SHA-256 checksum
How to use checksums
2499dca0d7fc0c05f072979139d47b3b3bf1339dc266d5208a4aee51be196d42
BLAKE2b-256 checksum
How to use checksums
2ae2ff7dba2927207e9d377e8e270c2d1515dfdc23186217ca51301ab6822577
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / flake8_import_order-0.14.1-py2.py3-none-any.whl

Download URL flake8_import_order-0.14.1-py2.py3-none-any.whl
Size 15.2 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
d8b5e1144224e673551c4d96ddce6a7d518a72792e868eacde1fceb0fc332bae
BLAKE2b-256 checksum
How to use checksums
abf4c9aef38ae757a511bb086d180cf686067c8b0cb75e7deb6feb23b028eab6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for flake8-import-order 0.14.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.14.1 This release

2 release files No provenance

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