flake8-tidy-imports


Nameflake8-tidy-imports JSON
Version 4.11.0 PyPI version JSON
download
home_pageNone
SummaryA flake8 plugin that helps you write tidier imports.
upload_time2024-10-27 22:36:50
maintainerNone
docs_urlNone
authorNone
requires_python>=3.9
licenseNone
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.svg?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/>`_ plugin that helps you write tidier imports.

----

**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.

----

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

Python 3.9 to 3.13 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.

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": null,
    "name": "flake8-tidy-imports",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": "flake8_tidy_imports",
    "author": null,
    "author_email": "Adam Johnson <me@adamj.eu>",
    "download_url": "https://files.pythonhosted.org/packages/d7/03/9ea93e3ab6e26a6c5da23c95318101491b92f5a0777d1e80cf2e39d11d02/flake8_tidy_imports-4.11.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.svg?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/>`_ plugin that helps you write tidier imports.\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\nRequirements\n============\n\nPython 3.9 to 3.13 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\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": null,
    "summary": "A flake8 plugin that helps you write tidier imports.",
    "version": "4.11.0",
    "project_urls": {
        "Changelog": "https://github.com/adamchainz/flake8-tidy-imports/blob/main/CHANGELOG.rst",
        "Funding": "https://adamj.eu/books/",
        "Repository": "https://github.com/adamchainz/flake8-tidy-imports"
    },
    "split_keywords": [
        "flake8_tidy_imports"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "063bd3c1b7f47a20c41b4811e15a783d29b1e83921422152a9770c66dea1017e",
                "md5": "f2f8b45884955b3559410cab875b13c9",
                "sha256": "607a76a7c2a9dec682e214f0692d4e7876862a618b766cef8f16979efac1ce22"
            },
            "downloads": -1,
            "filename": "flake8_tidy_imports-4.11.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "f2f8b45884955b3559410cab875b13c9",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 10013,
            "upload_time": "2024-10-27T22:36:48",
            "upload_time_iso_8601": "2024-10-27T22:36:48.930116Z",
            "url": "https://files.pythonhosted.org/packages/06/3b/d3c1b7f47a20c41b4811e15a783d29b1e83921422152a9770c66dea1017e/flake8_tidy_imports-4.11.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d7039ea93e3ab6e26a6c5da23c95318101491b92f5a0777d1e80cf2e39d11d02",
                "md5": "c6d19bd1bdecabbdd466811ea53949e7",
                "sha256": "fcd57e275a9f4f8163db2ecb17c9c942d311750e2315cf9ae931f22f7043d496"
            },
            "downloads": -1,
            "filename": "flake8_tidy_imports-4.11.0.tar.gz",
            "has_sig": false,
            "md5_digest": "c6d19bd1bdecabbdd466811ea53949e7",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 13666,
            "upload_time": "2024-10-27T22:36:50",
            "upload_time_iso_8601": "2024-10-27T22:36:50.553982Z",
            "url": "https://files.pythonhosted.org/packages/d7/03/9ea93e3ab6e26a6c5da23c95318101491b92f5a0777d1e80cf2e39d11d02/flake8_tidy_imports-4.11.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-27 22:36:50",
    "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.57972s