dep_checker

Tool to check all requirements are actually required.

Docs

Documentation Build Status Docs Check Status

Tests

Linux Test Status Windows Test Status macOS Test Status Coverage

PyPI

PyPI - Package Version PyPI - Supported Python Versions PyPI - Supported Implementations PyPI - Wheel

Activity

GitHub last commit GitHub commits since tagged version Maintenance PyPI - Downloads

QA

CodeFactor Grade Flake8 Status mypy status

Other

License GitHub top language Requirements Status

Installation

python3 -m pip install dep_checker --user

Contents

Usage

Configuration

dep-checker can be configured via the [tool.dep_checker] table of pyproject.toml.
The configuration can also be placed in the [dep_checker] section of tox.ini or setup.cfg.

Changed in version 0.5.0: Added support for pyproject.toml

allowed_unused

List of requirements which are allowed to be unused in the source code.

Examples:

# pyproject.toml
[tool.dep_checker]
allowed_unused = ["pytest", "sphinx"]
# tox.ini / setup.cfg
[dep_checker]
allowed_unused = pytest, sphinx
name_mapping

Mapping of requirement names (e.g. “biopython”) to the names of packages they provide (e.g. “Bio”).

New in version 0.4.1.

Examples:

# pyproject.toml
[tool.dep_checker.name_mapping]
biopython = "Bio"
# tox.ini / setup.cfg
[dep_checker]
name_mapping =
    biopython = Bio
namespace_packages

List of namespace packages, such as ruamel.yaml.

This currently only handles imports in the form import namespace.package or from namespace.package import object, but not from namespace import package.

New in version 0.4.1.

Examples:

# pyproject.toml
[tool.dep_checker]
namespace_packages = ["ruamel.yaml", "jaraco.docker"]
# tox.ini / setup.cfg
[dep_checker]
namespace_packages = ruamel.yaml, jaraco.docker
Ignoring imports that aren’t listed as requirements

To ignore lines where packages are imported, but aren’t listed in requirements.txt, use # nodep.

For example:

# 3rd party
import pytest  # nodep

New in version 0.4.1.

Command Line

Tool to check all requirements are actually required.

dep-checker [OPTIONS] PKG_NAME

Options

--req-file <FILENAME>

The requirements file.

-a, --allowed-unused <allowed_unused>

Requirements which are allowed to be unused in the source code.

-d, --work-dir <work_dir>

The directory to find the source of the package in. Useful with the src/ layout.

--colour, --no-colour

Whether to use coloured output.

Arguments

PKG_NAME

Required argument.

As a pre-commit hook

dep-checker can also be used as a pre-commit hook. To do so, add the following to your .pre-commit-config.yaml file:

- repo: https://github.com/python-coincidence/dep_checker
  rev: 0.8.0
  hooks:
  - id: dep_checker
    args:
    - <PKG_NAME>

<PKG_NAME> should be replaced with the name of the package to check, e.g. consolekit:

- repo: https://github.com/python-coincidence/dep_checker
  rev: 0.8.0
  hooks:
  - id: dep_checker
    args:
    - consolekit

Public API

Tool to check all requirements are actually required.

Classes:

DepChecker(pkg_name, requirements, *[, …])

Check imports for the given package, against the given requirements.

PassingRequirement(name, lineno, filename)

Represents a requirement which is listed in the requirements file and imported.

UnlistedRequirement(name, lineno, filename)

Represents a requirement which is imported but not listed in the requirements file.

UnusedRequirement(name)

Represents a requirement which is listed in the requirements file but never imported.

Functions:

check_imports(pkg_name[, req_file, …])

Check imports for the given package, against the given requirements file.

make_requirement_tuple(data)

Construct either a PassingRequirement, or UnlistedRequirement, or UnusedRequirement, depending on the value of the class key.

Data:

template

The template to use when printing output.

class DepChecker(pkg_name, requirements, *, allowed_unused=None, name_mapping=None, namespace_packages=None)[source]

Check imports for the given package, against the given requirements.

This class can be used to integrate dep_checker into other applications.

New in version 0.6.0.

Parameters
  • pkg_name (str)

  • requirements (Iterable[str]) – The package’s (supposed) requirements.

  • allowed_unused (Optional[Iterable[str]]) – List of requirements which are allowed to be unused in the source code. Default [].

  • name_mapping (Optional[Mapping[str, str]]) – Optional mapping of requirement names to import names, if they differ.

  • namespace_packages (Optional[Iterable[str]]) – List of namespace packages, e.g. ruamel.yaml.

check(work_dir)[source]

Perform the check itself.

Parameters

work_dir (Union[str, Path, PathLike]) – The directory to find the source of the package in. Useful with the src/ layout.

Return type

Iterable[Union[UnlistedRequirement, PassingRequirement, UnusedRequirement]]

namedtuple PassingRequirement(name, lineno, filename)[source]

Bases: NamedTuple

Represents a requirement which is listed in the requirements file and imported.

New in version 0.6.0.

Fields
  1.  name (str) – The name of the requirement.

  2.  lineno (int) – The line number where the requirement is imported.

  3.  filename (str) – The file where the requirement is imported.

format_error()[source]

Format the error (or, in this case, success) message.

Return type

str

namedtuple UnlistedRequirement(name, lineno, filename)[source]

Bases: NamedTuple

Represents a requirement which is imported but not listed in the requirements file.

New in version 0.6.0.

Fields
  1.  name (str) – The name of the requirement.

  2.  lineno (int) – The line number where the requirement is imported.

  3.  filename (str) – The file where the requirement is imported.

format_error()[source]

Format the error message.

Return type

str

namedtuple UnusedRequirement(name)[source]

Bases: NamedTuple

Represents a requirement which is listed in the requirements file but never imported.

New in version 0.6.0.

Fields
  1.  name (str) – The name of the requirement.

format_error()[source]

Format the error message.

Return type

str

check_imports(pkg_name, req_file='requirements.txt', allowed_unused=None, colour=None, name_mapping=None, namespace_packages=None, work_dir='.')[source]

Check imports for the given package, against the given requirements file.

Parameters
  • pkg_name (str)

  • req_file (Union[str, Path, PathLike]) – Default 'requirements.txt'.

  • allowed_unused (Optional[List[str]]) – List of requirements which are allowed to be unused in the source code. Default [].

  • colour (Optional[bool]) – Whether to use coloured output.

  • name_mapping (Optional[Dict[str, str]]) – Optional mapping of requirement names to import names, if they differ.

  • namespace_packages (Optional[List[str]]) – List of namespace packages, e.g. ruamel.yaml.

  • work_dir (Union[str, Path, PathLike]) – The directory to find the source of the package in. Useful with the src/ layout. Default '.'.

Return type

int

  • Returns 0 if all requirements are used and listed as requirements.

  • Returns 1 is a requirement is unused, or if a package is imported but not listed as a requirement.

Changed in version 0.4.1:
  • Added the name_mapping option.

  • Added the work_dir option.

make_requirement_tuple(data)[source]

Construct either a PassingRequirement, or UnlistedRequirement, or UnusedRequirement, depending on the value of the class key.

Typically used to reconstruct an object from the dictionary produced by the _asdict() method of those classes.

New in version 0.6.0.

Parameters

data (Dict[str, Any])

Return type

Union[Type[PassingRequirement], Type[UnlistedRequirement], Type[UnusedRequirement]]

template = '{name} imported on line {lineno} of {filename}'

Type:    str

The template to use when printing output.

Downloading source code

The dep_checker source code is available on GitHub, and can be accessed from the following URL: https://github.com/python-coincidence/dep_checker

If you have git installed, you can clone the repository with the following command:

git clone https://github.com/python-coincidence/dep_checker
Cloning into 'dep_checker'...
remote: Enumerating objects: 47, done.
remote: Counting objects: 100% (47/47), done.
remote: Compressing objects: 100% (41/41), done.
remote: Total 173 (delta 16), reused 17 (delta 6), pack-reused 126
Receiving objects: 100% (173/173), 126.56 KiB | 678.00 KiB/s, done.
Resolving deltas: 100% (66/66), done.
Alternatively, the code can be downloaded in a ‘zip’ file by clicking:
Clone or download –> Download Zip
Downloading a 'zip' file of the source code.

Downloading a ‘zip’ file of the source code

Building from source

The recommended way to build dep_checker is to use tox:

tox -e build

The source and wheel distributions will be in the directory dist.

If you wish, you may also use pep517.build or another PEP 517-compatible build tool.

License

dep_checker is licensed under the MIT License

A short and simple permissive license with conditions only requiring preservation of copyright and license notices. Licensed works, modifications, and larger works may be distributed under different terms and without source code.

Permissions Conditions Limitations
  • Commercial use
  • Modification
  • Distribution
  • Private use
  • Liability
  • Warranty

Copyright (c) 2020 Dominic Davis-Foster

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE.

View the Function Index or browse the Source Code.

Browse the GitHub Repository