Skip to content
Snippets Groups Projects
Forked from kwant / kwant
718 commits behind the upstream repository.
  • Joseph Weston's avatar
    7cb76722
    fix discretizer packaging and optional testing · 7cb76722
    Joseph Weston authored
    Previously the testing/importing 'continuum' would fail if sympy was not
    installed. Now we do the following:
    
        * add sympy as an optional dependency in 'extras_require'
        * force pytest to ignore tests in packages that have uninstalled
          dependencies by defining a hook in 'conftest.py'
        * use the 'class as a module' hack when importing 'continuum'.
          When sympy is not installed the continuum module will be replaced
          with an instance of 'ExtensionUnavailable' that will raise a runtime
          error on attribute access.
        * no warning is raised if sympy is not installed (it is an
          optional dependency).
    7cb76722
    History
    fix discretizer packaging and optional testing
    Joseph Weston authored
    Previously the testing/importing 'continuum' would fail if sympy was not
    installed. Now we do the following:
    
        * add sympy as an optional dependency in 'extras_require'
        * force pytest to ignore tests in packages that have uninstalled
          dependencies by defining a hook in 'conftest.py'
        * use the 'class as a module' hack when importing 'continuum'.
          When sympy is not installed the continuum module will be replaced
          with an instance of 'ExtensionUnavailable' that will raise a runtime
          error on attribute access.
        * no warning is raised if sympy is not installed (it is an
          optional dependency).
conftest.py 1.49 KiB
# Copyright 2011-2017 Kwant authors.
#
# This file is part of Kwant.  It is subject to the license terms in the file
# LICENSE.rst found in the top-level directory of this distribution and at
# http://kwant-project.org/license.  A list of Kwant authors can be found in
# the file AUTHORS.rst at the top-level directory of this distribution and at
# http://kwant-project.org/authors.
"""Pytest plugin to ignore packages that have uninstalled dependencies.

This ignores packages on test collection, which is required when the
tests reside in a package that itself requires the dependency to be
installed.
"""

import importlib


# map from subpackage to sequence of dependency module names
subpackage_dependencies = {
    'kwant/continuum': ['sympy']
}


# map from subpackage to sequence of dependency modules that are not installed
dependencies_not_installed = {}
for package, dependencies in subpackage_dependencies.items():
    not_installed = []
    for dep in dependencies:
        try:
            importlib.import_module(dep)
        except ImportError:
            not_installed.append(dep)
    if len(not_installed) != 0:
        dependencies_not_installed[package] = not_installed


def pytest_ignore_collect(path, config):
    for subpackage, not_installed in dependencies_not_installed.items():
        if subpackage in path.strpath:
            print('ignoring {} because the following dependencies are not '
                  'installed: {}'.format(subpackage, ', '.join(not_installed)))
            return True