Skip to main content

Check dependencies of a python project against pyproject.toml requirements

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.

This is a pure-Python zero-dependency (Up until Python 3.11 one, toml) package.

Usage

usage: check-dependencies [-h] [--include-extra] [--verbose] [--all] [--missing MISSING] [--extra EXTRA] 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
  --include-extra    Include dev dependencies
  --verbose          Show every import of a package
  --all              Show all imports (including correct ones)
  --missing MISSING  Comma seperated list of requirements known to be missing. 
                     Assume they are part of the requirements.
  --extra EXTRA      Comma seperated list of requirements known to not be imported.
                     Assume they are not part of the requirements.

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)

Additionally, each import is prefixed with the file name and line number where it is imported.

Examples

Basic usage

check-dependencies  project/src/
  pandas
! matplotlib
  numpy
+ requests

Output all dependencies

Output all dependencies, including the correct ones.

check-dependencies --all project/src/
  pandas
! matplotlib
  numpy
+ requests

Verbose output

Output each erroneous import and extra dependency with cause, file name and line number.

check-dependencies --verbose project/src/
!NA matplotlib project/src/main.py:4
+EXTRA project/pyproject.toml requests

Combine verbose and all

Output all imports, including the correct ones with file name and line number.

check-dependencies --verbose --all project/src/
 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:
# Config file: project/pyproject.toml
+EXTRA 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]
known-missing = [
  "undeclared_package",
  "another_package"
]
known-extra = [
  "package_as_extra_for_another_package",
  "yet_another_package"
]

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
  • 8: Could not find associated pyproject.toml file
  • 1: Another error occurred

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
Logging logger Minimize additional packages
Packaging uv
Tests pytest
Typing mypy Type all methods
Linting ruff Also used for formatting

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.12.6.tar.gz (14.6 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.12.6-py3-none-any.whl (17.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: check_dependencies-0.12.6.tar.gz
  • Upload date:
  • Size: 14.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for check_dependencies-0.12.6.tar.gz
Algorithm Hash digest
SHA256 a066e00ca462ac6a8c47d4c2c0a6295c077570ec8f5d01d1712404d24dd2719f
MD5 4015ceecb634ee6b51e9f9a94cffa72c
BLAKE2b-256 be819686469f68b5ef228a627bd986b41fec6a2dafa26298553644cda647da73

See more details on using hashes here.

Provenance

The following attestation bundles were made for check_dependencies-0.12.6.tar.gz:

Publisher: python.yml on schollm/check-dependencies

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

File hashes

Hashes for check_dependencies-0.12.6-py3-none-any.whl
Algorithm Hash digest
SHA256 67918f0b57ff8483dd970e962f0a6197d53197285849d448a08fc7fe3064e441
MD5 ebc96e2fae223ec5622f96ebdf430bf8
BLAKE2b-256 17627640bdcaaa0436bb5157891915d5c4c47f8e311a8af1297cc591a7ac3753

See more details on using hashes here.

Provenance

The following attestation bundles were made for check_dependencies-0.12.6-py3-none-any.whl:

Publisher: python.yml on schollm/check-dependencies

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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