Skip to main content

Package requirements checker, plugin for flake8

Project description

This module provides a plug-in for flake8, which checks/validates package import requirements. It reports missing and/or not used project direct dependencies.

This plug-in adds new flake8 warnings:

  • I900: Package is not listed as a requirement.

  • I901: Package is required but not used.

Important notice

In order to collect project’s dependencies, this checker evaluates Python code from the setup.py file stored in the project’s root directory. Code evaluation is done with the eval() function. As a fall-back method, this checker also tries to load dependencies from the pyproject.toml file from the poetry tool section, or from the requirements.txt text file in the project’s root directory.

At this point it is very important to be aware of the consequences of the above approach. One might inject malicious code into the setup.py file, which will be executed by this checker. Hence, this checker shall NEVER be use to check code from an unknown source! However, in most cases, one validates code from a known source (e.g. own code) and one will run script stored in the setup.py file anyway. The worst case scenario is, that this checker will execute the equivalent of the python setup.py, which shall be idempotent (it’s a horribly designed setup.py file if it’s not).

If you have noticed some side effects during the flake8 check and your setup.py file is written in a standard way (e.g. pypa-sampleproject), please fill out a bug report.

Installation

You can install, upgrade, or uninstall flake8-requirements with these commands:

$ pip install flake8-requirements
$ pip install --upgrade flake8-requirements
$ pip uninstall flake8-requirements

Customization

For projects with custom (private) dependencies, one can provide mapping between project name and provided modules. Such a mapping can be set on the command line during the flake8 invocation with the --known-modules option or alternatively in the [flake8] section of the configuration file, e.g. setup.cfg. The syntax of the custom mapping looks like follows:

1st-project-name:[module1,module2,...],2nd-project-name:[moduleA,moduleB,...],...

If some local project lacks “name” attribute in the setup.py file (it is highly discouraged not to provide the “name” attribute, though), one can omit the project name in the mapping and do as follows:

:[localmodule1,localmodule2,...],1st-local-library:[moduleA,moduleB,...],...

Real life example:

$ cat setup.cfg
[flake8]
max-line-length = 100
known-modules = my-lib:[mylib.drm,mylib.encryption]

It is also possible to scan host’s site-packages directory for installed packages. This feature is disabled by default, but user can enable it with the --scan-host-site-packages command line option. Please note, however, that the location of the site-packages directory will be determined by the Python version used for flake8 execution.

In order to read requirements from the text file, user shall provide the location of such a file with the --requirements-file option. If the given location is not an absolute path, then it has to be specified as a path relative to the project’s root directory.

If you use the -r flag in your requirements text file with more than one level of recursion (in other words, one file includes another, the included file includes yet another, and so on), add the --requirements-max-depth option to flake8 (for example, --requirements-max-depth=3 to allow three levels of recursion).

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

flake8_requirements-1.5.0-py2.py3-none-any.whl (13.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file flake8_requirements-1.5.0-py2.py3-none-any.whl.

File metadata

  • Download URL: flake8_requirements-1.5.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 13.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

File hashes

Hashes for flake8_requirements-1.5.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 001afd020822e36f41f20055264bc99bff42188195ebd3af8c04b81e3851e13e
MD5 3a25ac2c5d747b3799e76f11661d4731
BLAKE2b-256 24db78041f95e5b5a044ac0cc7a50fe2b34529f67e1b83a68cf0f95b5950d135

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