====================
Pinned Import Linter
====================
.. image:: https://img.shields.io/pypi/v/pinned-import-linter.svg
:target: https://pypi.org/project/pinned-import-linter
.. image:: https://img.shields.io/pypi/pyversions/pinned-import-linter.svg
:alt: Python versions
:target: https://pypi.org/project/pinned-import-linter/
.. image:: https://github.com/maintainer64/pinned-import-linter/actions/workflows/main.yml/badge.svg?branch=main
:target: https://github.com/maintainer64/pinned-import-linter/actions/workflows/main.yml
:alt: CI Status
⚡ A plugin for python that will help you standardize imports from any libraries.
* Free software: MIT License
Overview
--------
Pinned Import Lint is a command-line utility that allows users to verify whether they
are adhering to the established syntax for importing modules in their Python projects.
This is done by checking the permissible names for modules, packages, and their importation rules,
as specified in a configuration file.
The configuration file lists the modules to monitor
and provides settings for each module, such as the ``allow_from`` option,
which restricts the import of submodules from packages using the ``from`` keyword.
Only selected packages can be imported through a common name or alias.
Pinned Import lint helps maintain consistency in the way modules are imported,
enforcing a uniform style of importing packages in Python.
It draws inspiration from similar projects, such as:
1. https://github.com/seddonym/import-lint
2. https://github.com/adamchainz/flake8-tidy-import
Quick start
-----------
Install Pinned Import Linter::
pip install pinned-import-linter
Select libraries and what import styles are available for them.
In this example, we show the standard configuration for importing libraries into Python.
Create a ``tox.ini`` file in your project or any other file
(then it will need to be connected via the ``--config`` parameter in the *CLI*)
with similar contents:
.. code-block:: ini
[pinned_import_linter]
package_names = typing,itertools,datetime,sys,pathlib
file_extensions = py,pyi
exclude = ^(venv|.venv)
[pinned_import_linter.typing]
allow_alias = true
alias_names = t
allow_from = false
allow_package = false
[pinned_import_linter.itertools]
allow_alias = true
alias_names = it
allow_from = false
allow_package = false
[pinned_import_linter.datetime]
allow_alias = true
alias_names = dt
allow_from = false
allow_package = false
[pinned_import_linter.sys]
allow_alias = false
allow_from = false
allow_package = true
[pinned_import_linter.pathlib]
allow_alias = false
allow_from = true
allow_package = false
In the ``[pinned_import_linter]`` section, there is only one parameter expected, ``package_name``.
These are the libraries whose import styles will be restricted in the subsequent sections.
1. For the ``typing``, ``itertools``, and ``datetime`` libraries, we have specified the ``allow_alias`` parameter.
This allows for the use of alias imports while prohibiting the usage of the `from` statement.
We accomplish this by setting ``allow_from = false`` and disallowing
imports without an alias by setting ``allow_package = false``.
To define allowed names (if none are defined, all names are allowed), we use the ``alias_names = t,tu,tp,tv`` parameter, separated by commas.
Therefore, only the specified packages will be permitted to be imported via alias from this list.
2. For the ``sys`` standard library set ``allow_package = true`` and
the rest to ``false`` in order to import a package using the
keyword ``from`` or alias a name (``import sys as ...``)
becomes unavailable.
3. For the ``pathlib`` standard library set ``allow_from = true`` and
the rest to ``false`` in order to allow importing only through the ``from`` keyword.
4. For the other Libraries (not described) in
the configuration can be imported in any way.
Now, from your project root, run::
lint-pinned-imports --config tox.ini main.py your_folder .
For a file with this configuration:
.. code-block:: python
from typing import Callable, List
from itertools import product
import itertools
import pathlib as pt
from os import linesep
Output after CLI execution:
.. code-block:: text
main.py:1: error: Banned import 'from typing import ...'
main.py:2: error: Banned import 'from itertools import ...'
main.py:3: error: Banned import 'import itertools'
main.py:4: error: Banned import 'import pathlib as ...'
Connect all files on pre-commit
-------------------------------
1. Add package on dev-dependency in your project on Python
2. Add step into your .pre-commit-config.yaml
Check all files on directories:
.. code-block:: yml
repos:
- repo: local
hooks:
- id: lint-pinned-imports
name: Restricted imports
entry: lint-pinned-imports --config tox.ini .
language: system
pass_filenames: false
Alternative .pre-commit-config.yaml checked only changed files:
.. code-block:: yml
repos:
- repo: local
hooks:
- id: lint-pinned-imports
name: Restricted imports
entry: lint-pinned-imports --config tox.ini
language: system
types: [ python ]
Raw data
{
"_id": null,
"home_page": null,
"name": "pinned-import-linter",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": null,
"author": null,
"author_email": "Danil Gubanov <20949800+maintainer64@users.noreply.github.com>",
"download_url": "https://files.pythonhosted.org/packages/db/8a/d0e9e1b5db45199336ea44599b6be3d6e836172df482d5ba9ba4d89848b3/pinned_import_linter-1.1.0.tar.gz",
"platform": null,
"description": "====================\nPinned Import Linter\n====================\n\n.. image:: https://img.shields.io/pypi/v/pinned-import-linter.svg\n :target: https://pypi.org/project/pinned-import-linter\n\n.. image:: https://img.shields.io/pypi/pyversions/pinned-import-linter.svg\n :alt: Python versions\n :target: https://pypi.org/project/pinned-import-linter/\n\n.. image:: https://github.com/maintainer64/pinned-import-linter/actions/workflows/main.yml/badge.svg?branch=main\n :target: https://github.com/maintainer64/pinned-import-linter/actions/workflows/main.yml\n :alt: CI Status\n\n\u26a1 A plugin for python that will help you standardize imports from any libraries.\n\n* Free software: MIT License\n\nOverview\n--------\n\nPinned Import Lint is a command-line utility that allows users to verify whether they\nare adhering to the established syntax for importing modules in their Python projects.\nThis is done by checking the permissible names for modules, packages, and their importation rules,\nas specified in a configuration file.\n\nThe configuration file lists the modules to monitor\nand provides settings for each module, such as the ``allow_from`` option,\nwhich restricts the import of submodules from packages using the ``from`` keyword.\nOnly selected packages can be imported through a common name or alias.\n\nPinned Import lint helps maintain consistency in the way modules are imported,\nenforcing a uniform style of importing packages in Python.\n\nIt draws inspiration from similar projects, such as:\n\n1. https://github.com/seddonym/import-lint\n2. https://github.com/adamchainz/flake8-tidy-import\n\nQuick start\n-----------\n\nInstall Pinned Import Linter::\n\n\n pip install pinned-import-linter\n\nSelect libraries and what import styles are available for them.\nIn this example, we show the standard configuration for importing libraries into Python.\n\nCreate a ``tox.ini`` file in your project or any other file\n(then it will need to be connected via the ``--config`` parameter in the *CLI*)\nwith similar contents:\n\n.. code-block:: ini\n\n [pinned_import_linter]\n package_names = typing,itertools,datetime,sys,pathlib\n file_extensions = py,pyi\n exclude = ^(venv|.venv)\n\n [pinned_import_linter.typing]\n allow_alias = true\n alias_names = t\n allow_from = false\n allow_package = false\n\n [pinned_import_linter.itertools]\n allow_alias = true\n alias_names = it\n allow_from = false\n allow_package = false\n\n [pinned_import_linter.datetime]\n allow_alias = true\n alias_names = dt\n allow_from = false\n allow_package = false\n\n [pinned_import_linter.sys]\n allow_alias = false\n allow_from = false\n allow_package = true\n\n [pinned_import_linter.pathlib]\n allow_alias = false\n allow_from = true\n allow_package = false\n\nIn the ``[pinned_import_linter]`` section, there is only one parameter expected, ``package_name``.\nThese are the libraries whose import styles will be restricted in the subsequent sections.\n\n1. For the ``typing``, ``itertools``, and ``datetime`` libraries, we have specified the ``allow_alias`` parameter.\nThis allows for the use of alias imports while prohibiting the usage of the `from` statement.\nWe accomplish this by setting ``allow_from = false`` and disallowing\nimports without an alias by setting ``allow_package = false``.\n\nTo define allowed names (if none are defined, all names are allowed), we use the ``alias_names = t,tu,tp,tv`` parameter, separated by commas.\nTherefore, only the specified packages will be permitted to be imported via alias from this list.\n\n2. For the ``sys`` standard library set ``allow_package = true`` and\nthe rest to ``false`` in order to import a package using the\nkeyword ``from`` or alias a name (``import sys as ...``)\nbecomes unavailable.\n\n3. For the ``pathlib`` standard library set ``allow_from = true`` and\nthe rest to ``false`` in order to allow importing only through the ``from`` keyword.\n\n4. For the other Libraries (not described) in\nthe configuration can be imported in any way.\n\n\nNow, from your project root, run::\n\n\n lint-pinned-imports --config tox.ini main.py your_folder .\n\nFor a file with this configuration:\n\n.. code-block:: python\n\n from typing import Callable, List\n from itertools import product\n import itertools\n import pathlib as pt\n from os import linesep\n\nOutput after CLI execution:\n\n.. code-block:: text\n\n main.py:1: error: Banned import 'from typing import ...'\n main.py:2: error: Banned import 'from itertools import ...'\n main.py:3: error: Banned import 'import itertools'\n main.py:4: error: Banned import 'import pathlib as ...'\n\nConnect all files on pre-commit\n-------------------------------\n\n1. Add package on dev-dependency in your project on Python\n2. Add step into your .pre-commit-config.yaml\n\nCheck all files on directories:\n\n.. code-block:: yml\n\n repos:\n - repo: local\n hooks:\n - id: lint-pinned-imports\n name: Restricted imports\n entry: lint-pinned-imports --config tox.ini .\n language: system\n pass_filenames: false\n\n\nAlternative .pre-commit-config.yaml checked only changed files:\n\n.. code-block:: yml\n\n repos:\n - repo: local\n hooks:\n - id: lint-pinned-imports\n name: Restricted imports\n entry: lint-pinned-imports --config tox.ini\n language: system\n types: [ python ]\n",
"bugtrack_url": null,
"license": "MIT LICENSE",
"summary": "A plugin for python that will help you standardize imports from any libraries.",
"version": "1.1.0",
"project_urls": {
"Source-code": "https://github.com/maintainer64/pinned-import-linter/"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "c97d7d13fb4e49450ed9f48df5aecae542e547da77df5798209d43ccd6e5120f",
"md5": "ea1773c518e1e02dca1530678c236d8c",
"sha256": "2649a1049961b8958993ab942d82ad6e8dcf2ae8bbfe8806bd0088538d65360c"
},
"downloads": -1,
"filename": "pinned_import_linter-1.1.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "ea1773c518e1e02dca1530678c236d8c",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 11025,
"upload_time": "2024-05-13T15:37:58",
"upload_time_iso_8601": "2024-05-13T15:37:58.510301Z",
"url": "https://files.pythonhosted.org/packages/c9/7d/7d13fb4e49450ed9f48df5aecae542e547da77df5798209d43ccd6e5120f/pinned_import_linter-1.1.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "db8ad0e9e1b5db45199336ea44599b6be3d6e836172df482d5ba9ba4d89848b3",
"md5": "e886f836dc0c649992607d9a25ddaa21",
"sha256": "9a908a3f8b6c24c497b8ac272a90ca03a4387d01039d424201bd2e38431faa2a"
},
"downloads": -1,
"filename": "pinned_import_linter-1.1.0.tar.gz",
"has_sig": false,
"md5_digest": "e886f836dc0c649992607d9a25ddaa21",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 7969,
"upload_time": "2024-05-13T15:37:59",
"upload_time_iso_8601": "2024-05-13T15:37:59.492564Z",
"url": "https://files.pythonhosted.org/packages/db/8a/d0e9e1b5db45199336ea44599b6be3d6e836172df482d5ba9ba4d89848b3/pinned_import_linter-1.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-05-13 15:37:59",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "maintainer64",
"github_project": "pinned-import-linter",
"travis_ci": false,
"coveralls": true,
"github_actions": true,
"requirements": [
{
"name": "pytest",
"specs": [
[
"~=",
"7.4.0"
]
]
},
{
"name": "pytest-cov",
"specs": [
[
"~=",
"4.1.0"
]
]
},
{
"name": "PyYAML",
"specs": [
[
"~=",
"6.0.1"
]
]
}
],
"tox": true,
"lcname": "pinned-import-linter"
}