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.