flake8-tidy-imports


Nameflake8-tidy-imports JSON
Version 4.10.0 PyPI version JSON
download
home_pagehttps://github.com/adamchainz/flake8-tidy-imports
SummaryA flake8 plugin that helps you write tidier imports.
upload_time2023-07-10 13:54:21
maintainer
docs_urlNone
authorAdam Johnson
requires_python>=3.8
licenseMIT
keywords flake8_tidy_imports
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ===================
flake8-tidy-imports
===================

.. image:: https://img.shields.io/github/actions/workflow/status/adamchainz/flake8-tidy-imports/main.yml?branch=main&style=for-the-badge
   :target: https://github.com/adamchainz/flake8-tidy-imports/actions?workflow=CI

.. image:: https://img.shields.io/pypi/v/flake8-tidy-imports.svg?style=for-the-badge
   :target: https://pypi.org/project/flake8-tidy-imports/

.. image:: https://img.shields.io/badge/code%20style-black-000000.svg?style=for-the-badge
   :target: https://github.com/psf/black

.. image:: https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white&style=for-the-badge
   :target: https://github.com/pre-commit/pre-commit
   :alt: pre-commit

A `flake8 <https://flake8.readthedocs.io/en/latest/index.html>`_ plugin that helps you write tidier imports.

Requirements
============

Python 3.8 to 3.12 supported.

Installation
============

First, install with ``pip``:

.. code-block:: sh

     python -m pip install flake8-tidy-imports

Second, if you define Flake8’s ``select`` setting, add the ``I25`` prefix to it.
Otherwise, the plugin should be active by default.

----

**Linting a Django project?**
Check out my book `Boost Your Django DX <https://adamchainz.gumroad.com/l/byddx>`__ which covers Flake8 and many other code quality tools.

----

Options
=======

``banned-modules``
------------------

Config for rule I251 (below).
Should contain a map where each line is a banned import string, followed by '=', then the message to use when encountering that import.

There is also a special directive to ban a preselected list of removed/moved modules between Python 2 and Python 3, recommending replacements from `six
<https://pythonhosted.org/six/>`_ where possible.
It can be turned on by adding ``{python2to3}`` to the list of ``banned-modules``.

For example in ``setup.cfg``:

.. code-block:: ini

    [flake8]
    banned-modules =
      mock = Use unittest.mock.
      {python2to3}

Note that despite the name, you can ban imported objects too, since the syntax is the same.
For example:

.. code-block:: ini

    [flake8]
    banned-modules =
      decimal.Decimal = Use ints and floats only.

Entries containing ``*`` are treated as wildcards matching zero or more path components.
For example:

* ``example.yellow.*`` matches ``example.yellow``, ``example.yellow.truck``, ``example.yellow.truck.driving`` etc.
* ``example.*.truck`` matches ``example.truck``, ``example.yellow.truck``, ``example.red.truck``, ``example.big.red.truck``, etc.

``ban-relative-imports``
------------------------

Controls rule I252 (below). Accepts two values:

* ``parents`` - bans imports from parent modules (and grandparents, etc.), i.e. with more than one ``.``.
* ``true`` - bans all relative imports.

For example:

.. code-block:: ini

    [flake8]
    ban-relative-imports = parents

(If you want to ban absolute imports, you can put your project's modules in ``banned-modules``.)

Rules
=====

**Note:** Before version 4.0.0, the rule codes were numbered 50 lower, e.g. I250 was I200.
They were changed in `Issue #106 <https://github.com/adamchainz/flake8-tidy-imports/issues/106>`__ due to conflict with ``flake8-import-order``.

I250: Unnecessary import alias
------------------------------

Complains about unnecessary import aliasing of three forms:

* ``import foo as foo`` -> ``import foo``
* ``import foo.bar as bar`` -> ``from foo import bar``
* ``from foo import bar as bar`` -> ``from foo import bar``

The message includes the suggested rewrite (which may not *always* be correct), for example:

.. code-block:: sh

    $ flake8 file.py
    file.py:1:1: I250 Unnecessary import alias - rewrite as 'from foo import bar'.

Such aliases can be automatically fixed by ``isort`` if you activate its `remove_redundant_aliases option <https://pycqa.github.io/isort/docs/configuration/options/#remove-redundant-aliases>`__.

I251: Banned import ``<import>`` used.
--------------------------------------

Complains about use of banned imports.
By default there are no imports banned - you should configure them with ``banned-modules`` as described above in 'Options'.

The message includes a user-defined part that comes from the configuration.
For example:

.. code-block:: sh

    $ flake8 file.py
    file.py:1:1: I251 Banned import 'mock' used - use unittest.mock instead.

I252: Relative imports <from parent modules> are banned.
--------------------------------------------------------

Complains about use of relative imports:

* ``from . import foo`` (sibling import)
* ``from .bar import foo`` (sibling import)
* ``from .. import foo`` (parent import)

Controlled by the ``ban-relative-imports`` configuration option.

Absolute imports, or relative imports from siblings, are recommended by `PEP8 <https://www.python.org/dev/peps/pep-0008/>`__:

    Absolute imports are recommended, as they are usually more readable and tend to be better behaved...

    .. code-block:: python

        import mypkg.sibling
        from mypkg import sibling
        from mypkg.sibling import example

    However, explicit relative imports are an acceptable alternative to absolute imports...

    .. code-block:: python

        from . import sibling
        from .sibling import example

See also
--------

For more advanced control of imports in your project, try `import-linter <https://pypi.org/project/import-linter/>`__.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/adamchainz/flake8-tidy-imports",
    "name": "flake8-tidy-imports",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "flake8_tidy_imports",
    "author": "Adam Johnson",
    "author_email": "me@adamj.eu",
    "download_url": "https://files.pythonhosted.org/packages/76/7e/e4b9e42a5434e7f819f50f47b1bd1c04b71539d22f06e917e0d0846e412c/flake8_tidy_imports-4.10.0.tar.gz",
    "platform": null,
    "description": "===================\nflake8-tidy-imports\n===================\n\n.. image:: https://img.shields.io/github/actions/workflow/status/adamchainz/flake8-tidy-imports/main.yml?branch=main&style=for-the-badge\n   :target: https://github.com/adamchainz/flake8-tidy-imports/actions?workflow=CI\n\n.. image:: https://img.shields.io/pypi/v/flake8-tidy-imports.svg?style=for-the-badge\n   :target: https://pypi.org/project/flake8-tidy-imports/\n\n.. image:: https://img.shields.io/badge/code%20style-black-000000.svg?style=for-the-badge\n   :target: https://github.com/psf/black\n\n.. image:: https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white&style=for-the-badge\n   :target: https://github.com/pre-commit/pre-commit\n   :alt: pre-commit\n\nA `flake8 <https://flake8.readthedocs.io/en/latest/index.html>`_ plugin that helps you write tidier imports.\n\nRequirements\n============\n\nPython 3.8 to 3.12 supported.\n\nInstallation\n============\n\nFirst, install with ``pip``:\n\n.. code-block:: sh\n\n     python -m pip install flake8-tidy-imports\n\nSecond, if you define Flake8\u2019s ``select`` setting, add the ``I25`` prefix to it.\nOtherwise, the plugin should be active by default.\n\n----\n\n**Linting a Django project?**\nCheck out my book `Boost Your Django DX <https://adamchainz.gumroad.com/l/byddx>`__ which covers Flake8 and many other code quality tools.\n\n----\n\nOptions\n=======\n\n``banned-modules``\n------------------\n\nConfig for rule I251 (below).\nShould contain a map where each line is a banned import string, followed by '=', then the message to use when encountering that import.\n\nThere is also a special directive to ban a preselected list of removed/moved modules between Python 2 and Python 3, recommending replacements from `six\n<https://pythonhosted.org/six/>`_ where possible.\nIt can be turned on by adding ``{python2to3}`` to the list of ``banned-modules``.\n\nFor example in ``setup.cfg``:\n\n.. code-block:: ini\n\n    [flake8]\n    banned-modules =\n      mock = Use unittest.mock.\n      {python2to3}\n\nNote that despite the name, you can ban imported objects too, since the syntax is the same.\nFor example:\n\n.. code-block:: ini\n\n    [flake8]\n    banned-modules =\n      decimal.Decimal = Use ints and floats only.\n\nEntries containing ``*`` are treated as wildcards matching zero or more path components.\nFor example:\n\n* ``example.yellow.*`` matches ``example.yellow``, ``example.yellow.truck``, ``example.yellow.truck.driving`` etc.\n* ``example.*.truck`` matches ``example.truck``, ``example.yellow.truck``, ``example.red.truck``, ``example.big.red.truck``, etc.\n\n``ban-relative-imports``\n------------------------\n\nControls rule I252 (below). Accepts two values:\n\n* ``parents`` - bans imports from parent modules (and grandparents, etc.), i.e. with more than one ``.``.\n* ``true`` - bans all relative imports.\n\nFor example:\n\n.. code-block:: ini\n\n    [flake8]\n    ban-relative-imports = parents\n\n(If you want to ban absolute imports, you can put your project's modules in ``banned-modules``.)\n\nRules\n=====\n\n**Note:** Before version 4.0.0, the rule codes were numbered 50 lower, e.g. I250 was I200.\nThey were changed in `Issue #106 <https://github.com/adamchainz/flake8-tidy-imports/issues/106>`__ due to conflict with ``flake8-import-order``.\n\nI250: Unnecessary import alias\n------------------------------\n\nComplains about unnecessary import aliasing of three forms:\n\n* ``import foo as foo`` -> ``import foo``\n* ``import foo.bar as bar`` -> ``from foo import bar``\n* ``from foo import bar as bar`` -> ``from foo import bar``\n\nThe message includes the suggested rewrite (which may not *always* be correct), for example:\n\n.. code-block:: sh\n\n    $ flake8 file.py\n    file.py:1:1: I250 Unnecessary import alias - rewrite as 'from foo import bar'.\n\nSuch aliases can be automatically fixed by ``isort`` if you activate its `remove_redundant_aliases option <https://pycqa.github.io/isort/docs/configuration/options/#remove-redundant-aliases>`__.\n\nI251: Banned import ``<import>`` used.\n--------------------------------------\n\nComplains about use of banned imports.\nBy default there are no imports banned - you should configure them with ``banned-modules`` as described above in 'Options'.\n\nThe message includes a user-defined part that comes from the configuration.\nFor example:\n\n.. code-block:: sh\n\n    $ flake8 file.py\n    file.py:1:1: I251 Banned import 'mock' used - use unittest.mock instead.\n\nI252: Relative imports <from parent modules> are banned.\n--------------------------------------------------------\n\nComplains about use of relative imports:\n\n* ``from . import foo`` (sibling import)\n* ``from .bar import foo`` (sibling import)\n* ``from .. import foo`` (parent import)\n\nControlled by the ``ban-relative-imports`` configuration option.\n\nAbsolute imports, or relative imports from siblings, are recommended by `PEP8 <https://www.python.org/dev/peps/pep-0008/>`__:\n\n    Absolute imports are recommended, as they are usually more readable and tend to be better behaved...\n\n    .. code-block:: python\n\n        import mypkg.sibling\n        from mypkg import sibling\n        from mypkg.sibling import example\n\n    However, explicit relative imports are an acceptable alternative to absolute imports...\n\n    .. code-block:: python\n\n        from . import sibling\n        from .sibling import example\n\nSee also\n--------\n\nFor more advanced control of imports in your project, try `import-linter <https://pypi.org/project/import-linter/>`__.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A flake8 plugin that helps you write tidier imports.",
    "version": "4.10.0",
    "project_urls": {
        "Changelog": "https://github.com/adamchainz/flake8-tidy-imports/blob/main/CHANGELOG.rst",
        "Homepage": "https://github.com/adamchainz/flake8-tidy-imports",
        "Mastodon": "https://fosstodon.org/@adamchainz",
        "Twitter": "https://twitter.com/adamchainz"
    },
    "split_keywords": [
        "flake8_tidy_imports"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3246ba219b0e774b68993008afe963fd809d2ba28de45ad2f4c8acc345f15249",
                "md5": "8c1b35e05423ccca9d20c9ac7bdb6e8a",
                "sha256": "b0387fb2ea200441bd142309e716fb7b8f4b0937bdf5f8b7c0c118a5f5e2b8ed"
            },
            "downloads": -1,
            "filename": "flake8_tidy_imports-4.10.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "8c1b35e05423ccca9d20c9ac7bdb6e8a",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 10049,
            "upload_time": "2023-07-10T13:54:20",
            "upload_time_iso_8601": "2023-07-10T13:54:20.275674Z",
            "url": "https://files.pythonhosted.org/packages/32/46/ba219b0e774b68993008afe963fd809d2ba28de45ad2f4c8acc345f15249/flake8_tidy_imports-4.10.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "767ee4b9e42a5434e7f819f50f47b1bd1c04b71539d22f06e917e0d0846e412c",
                "md5": "f319f2ed59171ea5c175e3a17dccc318",
                "sha256": "bd6cf86465402d2b86903009b748d85a628e599e17b76e810c9857e3a2815173"
            },
            "downloads": -1,
            "filename": "flake8_tidy_imports-4.10.0.tar.gz",
            "has_sig": false,
            "md5_digest": "f319f2ed59171ea5c175e3a17dccc318",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 13820,
            "upload_time": "2023-07-10T13:54:21",
            "upload_time_iso_8601": "2023-07-10T13:54:21.963312Z",
            "url": "https://files.pythonhosted.org/packages/76/7e/e4b9e42a5434e7f819f50f47b1bd1c04b71539d22f06e917e0d0846e412c/flake8_tidy_imports-4.10.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-07-10 13:54:21",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "adamchainz",
    "github_project": "flake8-tidy-imports",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "flake8-tidy-imports"
}
        
Elapsed time: 0.09375s