Skip to main content

No project description provided

Project description

Check Dependencies

Check all imports from python files and compares them against the declared imports of a pyproject dependency list of expected imports. It can be used as a stand-alone or as part of a CI/CD to check if an application has all the necessary, but no superfluous imports.

Usage

usage: check_imports [-h] [--config-file CONFIG_FILE] [--include-dev] [--verbose] [--all] [--extra EXTRA] [--ignore IGNORE] file_name [file_name ...]

Find undeclared and unused (or all) imports in Python files

positional arguments:
  file_name             Python Source file to analyse

optional arguments:
  -h, --help            show this help message and exit
  --config-file CONFIG_FILE
                        Location of pyproject.toml file, can be file or a directory containing pyproject.toml file
  --include-dev         Include dev dependencies
  --verbose             Show every import of a package
  --all                 Show all imports (including correct ones)
  --extra EXTRA         Comma seperated list of extra requirements. Assume they are part of the requirements
  --ignore IGNORE       Comma seperated list of requirements to ignore. Assume they are not part of the requirements
> python -m  --config-file --all project/ project/src/**/*.py
  pandas
! matplotlib
  numpy
+ requests

> python -m check_dependencies --config-file --verbose project/ project/src/**/*.py
!NA matplotlib project/src/main.py:4
+EXTRA project/pyproject.toml requests 

> python -m check_dependencies --config-file --verbose --all project/ project/src/**/*.py
 OK project/src/data.py:5 pandas
 OK project/src/main.py:3 pandas
 OK project/src/plotting.py:4 pandas
!NA project/src/plotting.py:5 matplotlib
 OK project/src/plotting.py:6 numpy
 
 **** Dependencies in config file not used in application:
+EXTRA project/pyproject.toml requests 

Configuration

The configuration is read from pyproject.toml file. The configuration file supports two entries, [tool.check_dependencies.extra-requirements] that can be used to add extra dependencies to the list of requirements to be treated as existing requirements. The second entry, [tool.check_dependencies.ignore-requirements] does the opposite, it will ignore extra requirements that are not used in the application.

[tool.check_dependencies]
extra-requirements = [
  undeclared_package,
  another_package
]
ignore-requirements = [
  package_as_extra_for_another_package,
  yet_another_package
]

Output

The output is a list of imports with a prefix indicating the status of the import.

  • ! - Undeclared import
  • + - Extra import, declared in pyproject.toml, but not used in the file
  • - Correct import (only shown with --all)

In case of --verbose, the output is a list of all imports in the file, prefixed with:

  • !NA - Undeclared import
  • +EXTRA - Extra import, declared in pyproject.toml, but not used in the file
  • OK - Correct import (only shown with --all)

In case of --verbose, each import is prefixed with the file name and line number where it is declared.

Exit code

  • 0: No missing or superfluous dependencies found
  • 2: Missing (used, but not declared in pyproject.toml) dependencies found
  • 4: Extra (declared in pyproject.toml, but unused) dependencies found
  • 6: Both missing and superfluous dependencies found
  • 1: Another error occured

Development

Feature requests and merge requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate. Also with this project, I want to keep the dependencies to a minimum, so please keep that in mind when proposing a change. Currently, the only dependencies is toml to support Python 3.10 and below.

Coding Standards

Type Package Comment
Linter black Also for auto-formatted modules
Logging logger Minimize additional packages
Packaging poetry
Tests pytest
Typing mypy Type all methods
Linting flake8
Imports isort

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

check_dependencies-0.9.0.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

check_dependencies-0.9.0-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file check_dependencies-0.9.0.tar.gz.

File metadata

  • Download URL: check_dependencies-0.9.0.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.9.15 Darwin/23.0.0

File hashes

Hashes for check_dependencies-0.9.0.tar.gz
Algorithm Hash digest
SHA256 d687353a31c87dc004e2a51056af719c9ca6eb9ea8940a54e5ca6cf4328fe9a2
MD5 0dc753540efa7cef9511e06d04370385
BLAKE2b-256 b90e2a9e67bc54c9a721a72ed6df908298d8c19e237e4cd4a730ee6628a5417c

See more details on using hashes here.

File details

Details for the file check_dependencies-0.9.0-py3-none-any.whl.

File metadata

  • Download URL: check_dependencies-0.9.0-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.9.15 Darwin/23.0.0

File hashes

Hashes for check_dependencies-0.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 49d8665d45589b0ba2c8274b8a782d96a764d7a4464a4fdaf864d28c64d6bfc5
MD5 6c8af052a0e30b5f2117ae9871245333
BLAKE2b-256 a9269d437d7643d1836a4ddfbe3d7bc1f470f41bfa8b6e71c91b0084d7702714

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page