From 58cd87ed0ac7d36336c254bb806a87530a221dba Mon Sep 17 00:00:00 2001 From: Joseph Weston <joseph@weston.cloud> Date: Fri, 23 Feb 2018 19:57:12 +0100 Subject: [PATCH] reraise ImportError when importing kwant.continuum Previously we used the ExtensionUnavailable proxy that would raise an exception when accessed. Now because kwant.continuum is loaded lazily we can just raise an exception. We choose to re-raise ImportError with a better error message. --- kwant/_common.py | 24 ------------------------ kwant/continuum/__init__.py | 11 ++++------- 2 files changed, 4 insertions(+), 31 deletions(-) diff --git a/kwant/_common.py b/kwant/_common.py index eda672a1..4cd0de1a 100644 --- a/kwant/_common.py +++ b/kwant/_common.py @@ -41,30 +41,6 @@ class UserCodeError(Exception): pass -class ExtensionUnavailable: - """Class that replaces unavailable extension modules in the Kwant namespace. - - Some extensions for Kwant (e.g. 'kwant.continuum') require additional - dependencies that are not required for core functionality. When the - additional dependencies are not installed an instance of this class will - be inserted into Kwant's root namespace to simulate the presence of the - extension and inform users that they need to install additional - dependencies. - - See https://mail.python.org/pipermail/python-ideas/2012-May/014969.html - for more details. - """ - - def __init__(self, name, dependencies): - self.name = name - self.dependencies = ', '.join(dependencies) - - def __getattr__(self, _): - msg = ("'{}' is not available because one or more of the following " - "dependencies are not installed: {}") - raise RuntimeError(msg.format(self.name, self.dependencies)) - - def ensure_isinstance(obj, typ, msg=None): if isinstance(obj, typ): return diff --git a/kwant/continuum/__init__.py b/kwant/continuum/__init__.py index 718f5015..bc2420e9 100644 --- a/kwant/continuum/__init__.py +++ b/kwant/continuum/__init__.py @@ -6,18 +6,15 @@ # the file AUTHORS.rst at the top-level directory of this distribution and at # http://kwant-project.org/authors. -import sys - -from .._common import ExtensionUnavailable - try: from .discretizer import discretize, discretize_symbolic, build_discretized from ._common import sympify, lambdify from ._common import momentum_operators, position_operators -except ImportError: - sys.modules[__name__] = ExtensionUnavailable(__name__, ('sympy',)) +except ImportError as error: + msg = ("'kwant.continuum' is not available because one or more of its " + "dependencies is not installed.") + raise ImportError(msg) from error -del sys, ExtensionUnavailable __all__ = ['discretize', 'discretize_symbolic', 'build_discretized', 'sympify', 'lambdify'] -- GitLab