Public API

Functions:

load_toml(filename)

Load the importcheck configuration mapping from the given TOML file.

check_module(module[, combine_output])

Try to import module, otherwise handle the resulting error.

paths_to_modules(*paths)

Convert filesystem paths into dotted import names.

Classes:

ConfigDict

typing.TypedDict representing the configuration mapping parsed from pyproject.toml or similar.

ImportChecker(modules, *[, show, colour])

Class for checking modules can be imported.

OK(module)

Returned by check_module() if the module is successfully imported.

Error(module, stdout, stderr)

Returned by check_module() if the module could not be successfully imported.

load_toml(filename)[source]

Load the importcheck configuration mapping from the given TOML file.

Parameters

filename (Union[str, Path, PathLike])

Return type

ConfigDict

check_module(module, combine_output=False)[source]

Try to import module, otherwise handle the resulting error.

Parameters
  • module (str)

  • combine_output (bool) – If True stderr is combined with stdout. Default False.

Return type

Union[OK, Error]

paths_to_modules(*paths)[source]

Convert filesystem paths into dotted import names.

For example, foo/bar.py becomes foo.bar.

New in version 0.3.0.

Parameters

*paths (Union[str, Path, PathLike]) – The paths to convert.

Return type

Iterator[str]

typeddict ConfigDict[source]

Bases: TypedDict

typing.TypedDict representing the configuration mapping parsed from pyproject.toml or similar.

Optional Keys
  • always (List[str]) – List of modules to always try to import.

  • only_if (Mapping[str, List[str]]) – Mapping of PEP 508 markers to lists of imports to try to import if the markers evaluate to True.

  • config (Dict[str, Any]) – Configuration for importcheck.

class ImportChecker(modules, *, show=False, colour=False)[source]

Bases: object

Class for checking modules can be imported.

New in version 0.3.0.

Parameters
  • modules (Iterable[str]) – The list of modules to be checked.

  • show (bool) – Whether to show stdout and stderr generated from imports. Default False.

  • colour (bool) – Whether to use coloured output. Default False.

Attributes:

modules

The list of modules to be checked.

stats

Dictionary holding statistics about passing/failing imports.

show

Whether to show stdout and stderr generated from imports.

colour

Whether to use coloured output.

Methods:

check_modules()

Checks modules can be imported.

format_statistics()

Returns a string reporting the number of modules imported successfully.

modules

Type:    List[str]

The list of modules to be checked.

stats

Type:    Dict[str, int]

Dictionary holding statistics about passing/failing imports.

show

Type:    bool

Whether to show stdout and stderr generated from imports.

colour

Type:    bool

Whether to use coloured output.

check_modules()[source]

Checks modules can be imported.

Return type

Iterator[Tuple[str, int]]

Returns

An iterator of 2-element tuples comprising the name of the module and the import status:

  1. The module was imported successfully.

  2. The module could not be imported. If show is True the traceback will be shown.

format_statistics()[source]

Returns a string reporting the number of modules imported successfully.

Return type

str

namedtuple OK(module)[source]

Bases: NamedTuple

Returned by check_module() if the module is successfully imported.

Fields
  1.  module (str) – The name of the module being checked.

__bool__()[source]

OK objects always evaluate as False.

Return type

bool

__repr__()

Return a string representation of the OK.

Return type

str

namedtuple Error(module, stdout, stderr)[source]

Bases: NamedTuple

Returned by check_module() if the module could not be successfully imported.

Fields
  1.  module (str) – The name of the module being checked.

  2.  stdout (str) – The standard output from importing the module. This may also contain standard error if the streams are combined by check_module()

  3.  stderr (str) – Standard error generated by importing the module. This may also contain standard out if the streams are combined by check_module().

__bool__()[source]

Error objects always evaluate as True.

Return type

bool

__repr__()

Return a string representation of the Error.

Return type

str