flake8-import-order


Nameflake8-import-order JSON
Version 0.18.2 PyPI version JSON
download
home_pagehttps://github.com/PyCQA/flake8-import-order
SummaryFlake8 and pylama plugin that checks the ordering of import statements.
upload_time2022-11-26 20:14:25
maintainerPhil Jones
docs_urlNone
authorAlex Stapleton
requires_python
licenseLGPLv3
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            flake8-import-order
===================

|Build Status|

A `flake8 <http://flake8.readthedocs.org/en/latest/>`__ and `Pylama
<https://github.com/klen/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
<#styles>`_ and is extensible allowing for `custom styles
<#extending-styles>`_.

This plugin was originally developed to match the style preferences of
the `cryptography <https://github.com/pyca/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 import groups.
-  ``I202``: Additional newline in a group of imports.

Styles
------

The following styles are directly supported,

* ``cryptography`` - see an `example <https://github.com/PyCQA/flake8-import-order/blob/master/tests/test_cases/complete_cryptography.py>`__
* ``google`` - style described in `Google Style Guidelines <https://google.github.io/styleguide/pyguide.html?showone=Imports_formatting#Imports_formatting>`__, see an `example <https://github.com/PyCQA/flake8-import-order/blob/master/tests/test_cases/complete_google.py>`__
* ``smarkets`` - style as ``google`` only with `import` statements before `from X import ...` statements, see an `example <https://github.com/PyCQA/flake8-import-order/blob/master/tests/test_cases/complete_smarkets.py>`__
* ``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 <https://github.com/PyCQA/flake8-import-order/blob/master/tests/test_cases/complete_appnexus.py>`__
* ``edited`` - see an `example <https://github.com/PyCQA/flake8-import-order/blob/master/tests/test_cases/complete_edited.py>`__
* ``pycharm`` - style as ``smarkets`` only with case sensitive sorting imported names
* ``pep8`` - style that only enforces groups without enforcing the order within the groups

You can also `add your own style <#extending-styles>`_ 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.

The ``application-import-names`` and ``application-package-names`` can
contain namespaced packages or even exact nested module names. (This
is possible with 0.16 onwards).

``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,

.. code-block:: python

    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.

The ``I202`` check will consider any blank line between imports to
count, even if the line is not contextually related to the
imports. For example,

.. code-block:: python

    import logging
    try:
        from logging import NullHandler
    except ImportError:
        class NullHandler(logging.Handler):
            """Shim for version of Python < 2.7."""

            def emit(self, record):
                pass
    import sys  # I202 due to the blank line before the 'def emit'

will trigger a ``I202`` error despite the blank line not being
contextually related.

Extending styles
----------------

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

.. code-block:: python

    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.

.. code-block:: python

    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
<https://setuptools.readthedocs.io/en/latest/pkg_resources.html#entry-points>`__
mechanism:

.. code-block:: python

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

.. |Build Status| image:: https://travis-ci.org/PyCQA/flake8-import-order.svg?branch=master
   :target: https://travis-ci.org/PyCQA/flake8-import-order

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/PyCQA/flake8-import-order",
    "name": "flake8-import-order",
    "maintainer": "Phil Jones",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "philip.graham.jones+flake8-import@gmail.com",
    "keywords": "",
    "author": "Alex Stapleton",
    "author_email": "alexs@prol.etari.at",
    "download_url": "https://files.pythonhosted.org/packages/23/55/181d5e1b70ff1c7e1ee1d5e2247dfd53f1168fe706656f57e788aa1df2ab/flake8-import-order-0.18.2.tar.gz",
    "platform": null,
    "description": "flake8-import-order\n===================\n\n|Build Status|\n\nA `flake8 <http://flake8.readthedocs.org/en/latest/>`__ and `Pylama\n<https://github.com/klen/pylama>`__ plugin that checks the ordering of\nyour imports. It does not check anything else about the\nimports. Merely that they are grouped and ordered correctly.\n\nIn general stdlib comes first, then 3rd party, then local packages,\nand that each group is individually alphabetized, however this depends\non the style used. Flake8-Import-Order supports a number of `styles\n<#styles>`_ and is extensible allowing for `custom styles\n<#extending-styles>`_.\n\nThis plugin was originally developed to match the style preferences of\nthe `cryptography <https://github.com/pyca/cryptography>`__ project,\nwith this style remaining the default.\n\nWarnings\n--------\n\nThis package adds 4 new flake8 warnings\n\n-  ``I100``: Your import statements are in the wrong order.\n-  ``I101``: The names in your from import are in the wrong order.\n-  ``I201``: Missing newline between import groups.\n-  ``I202``: Additional newline in a group of imports.\n\nStyles\n------\n\nThe following styles are directly supported,\n\n* ``cryptography`` - see an `example <https://github.com/PyCQA/flake8-import-order/blob/master/tests/test_cases/complete_cryptography.py>`__\n* ``google`` - style described in `Google Style Guidelines <https://google.github.io/styleguide/pyguide.html?showone=Imports_formatting#Imports_formatting>`__, see an `example <https://github.com/PyCQA/flake8-import-order/blob/master/tests/test_cases/complete_google.py>`__\n* ``smarkets`` - style as ``google`` only with `import` statements before `from X import ...` statements, see an `example <https://github.com/PyCQA/flake8-import-order/blob/master/tests/test_cases/complete_smarkets.py>`__\n* ``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 <https://github.com/PyCQA/flake8-import-order/blob/master/tests/test_cases/complete_appnexus.py>`__\n* ``edited`` - see an `example <https://github.com/PyCQA/flake8-import-order/blob/master/tests/test_cases/complete_edited.py>`__\n* ``pycharm`` - style as ``smarkets`` only with case sensitive sorting imported names\n* ``pep8`` - style that only enforces groups without enforcing the order within the groups\n\nYou can also `add your own style <#extending-styles>`_ by extending ``Style``\nclass.\n\nConfiguration\n-------------\n\nYou will want to set the ``application-import-names`` option to a\ncomma separated list of names that should be considered local to your\napplication. These will be used to help categorise your import\nstatements into the correct groups. Note that relative imports are\nalways considered local.\n\nYou will want to set the ``application-package-names`` option to a\ncomma separated list of names that should be considered local to your\ncompany or organisation, but which are obtained using some sort of\npackage manager like Pip, Apt, or Yum.  Typically, code representing\nthe values listed in this option is located in a different repository\nthan the code being developed.  This option is only accepted in the\nsupported ``appnexus`` or ``edited`` styles or in any style that\naccepts application package names.\n\nThe ``application-import-names`` and ``application-package-names`` can\ncontain namespaced packages or even exact nested module names. (This\nis possible with 0.16 onwards).\n\n``import-order-style`` controls what style the plugin follows\n(``cryptography`` is the default).\n\nLimitations\n-----------\n\nCurrently these checks are limited to module scope imports only.\nConditional imports in module scope will also be ignored.\n\nClassification of an imported module is achieved by checking the\nmodule against a stdlib list and then if there is no match against the\n``application-import-names`` list and ``application-package-names`` if\nthe style accepts application-package names. Only if none of these\nlists contain the imported module will it be classified as third\nparty.\n\nThese checks only consider an import against its previous import,\nrather than considering all the imports together. This means that\n``I100`` errors are only raised for the latter of adjacent imports out\nof order. For example,\n\n.. code-block:: python\n\n    import X.B\n    import X  # I100\n    import X.A\n\nonly ``import X`` raises an ``I100`` error, yet ``import X.A`` is also\nout of order compared with the ``import X.B``.\n\nImported modules are classified as stdlib if the module is in a\nvendored list of stdlib modules. This list is based on the latest\nrelease of Python and hence the results can be misleading. This list\nis also the same for all Python versions because otherwise it would\nbe impossible to write programs that work under both Python 2 and 3\n*and* pass the import order check.\n\nThe ``I202`` check will consider any blank line between imports to\ncount, even if the line is not contextually related to the\nimports. For example,\n\n.. code-block:: python\n\n    import logging\n    try:\n        from logging import NullHandler\n    except ImportError:\n        class NullHandler(logging.Handler):\n            \"\"\"Shim for version of Python < 2.7.\"\"\"\n\n            def emit(self, record):\n                pass\n    import sys  # I202 due to the blank line before the 'def emit'\n\nwill trigger a ``I202`` error despite the blank line not being\ncontextually related.\n\nExtending styles\n----------------\n\nYou can add your own style by extending ``flake8_import_order.styles.Style``\nclass. Here's an example:\n\n.. code-block:: python\n\n    from flake8_import_order.styles import Cryptography\n\n\n    class ReversedCryptography(Cryptography):\n        # Note that Cryptography is a subclass of Style.\n\n        @staticmethod\n        def sorted_names(names):\n            return reversed(Cryptography.sorted_names(names))\n\nBy default there are five import groupings or sections; future,\nstdlib, third party, application, and relative imports. A style can\nchoose to accept another grouping, application-package, by setting the\n``Style`` class variable ``accepts_application_package_names`` to\nTrue, e.g.\n\n.. code-block:: python\n\n    class PackageNameCryptography(Cryptography):\n        accepts_application_package_names = True\n\nTo make flake8-import-order able to discover your extended style, you need to\nregister it as ``flake8_import_order.styles`` using setuptools' `entry points\n<https://setuptools.readthedocs.io/en/latest/pkg_resources.html#entry-points>`__\nmechanism:\n\n.. code-block:: python\n\n    # setup.py of your style package\n    setup(\n        name='flake8-import-order-reversed-cryptography',\n        ...,\n        entry_points={\n            'flake8_import_order.styles': [\n                'reversed = reversedcryptography:ReversedCryptography',\n                # 'reversed' is a style name.  You can pass it to\n                # --import-order-style option\n                # 'reversedcryptography:ReversedCryptography' is an import path\n                # of your extended style class.\n            ]\n        }\n    )\n\n.. |Build Status| image:: https://travis-ci.org/PyCQA/flake8-import-order.svg?branch=master\n   :target: https://travis-ci.org/PyCQA/flake8-import-order\n",
    "bugtrack_url": null,
    "license": "LGPLv3",
    "summary": "Flake8 and pylama plugin that checks the ordering of import statements.",
    "version": "0.18.2",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "4b54c376adfff6454ea9c28a1bc2b317",
                "sha256": "82ed59f1083b629b030ee9d3928d9e06b6213eb196fe745b3a7d4af2168130df"
            },
            "downloads": -1,
            "filename": "flake8_import_order-0.18.2-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "4b54c376adfff6454ea9c28a1bc2b317",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": null,
            "size": 15971,
            "upload_time": "2022-11-26T20:14:23",
            "upload_time_iso_8601": "2022-11-26T20:14:23.899863Z",
            "url": "https://files.pythonhosted.org/packages/0c/52/9c5cb50a61f3d90f9d6c98ba67e3227f4057dc398cf664f3b56cb7c261f7/flake8_import_order-0.18.2-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "9d863a6c484c569a5b4935406eea1b4d",
                "sha256": "e23941f892da3e0c09d711babbb0c73bc735242e9b216b726616758a920d900e"
            },
            "downloads": -1,
            "filename": "flake8-import-order-0.18.2.tar.gz",
            "has_sig": false,
            "md5_digest": "9d863a6c484c569a5b4935406eea1b4d",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 21717,
            "upload_time": "2022-11-26T20:14:25",
            "upload_time_iso_8601": "2022-11-26T20:14:25.869875Z",
            "url": "https://files.pythonhosted.org/packages/23/55/181d5e1b70ff1c7e1ee1d5e2247dfd53f1168fe706656f57e788aa1df2ab/flake8-import-order-0.18.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-11-26 20:14:25",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "PyCQA",
    "github_project": "flake8-import-order",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "flake8-import-order"
}
        
Elapsed time: 0.03696s