Skip to content
Snippets Groups Projects
Commit 58cd87ed authored by Joseph Weston's avatar Joseph Weston
Browse files

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.
parent 0ec82bcb
No related branches found
No related tags found
No related merge requests found
...@@ -41,30 +41,6 @@ class UserCodeError(Exception): ...@@ -41,30 +41,6 @@ class UserCodeError(Exception):
pass 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): def ensure_isinstance(obj, typ, msg=None):
if isinstance(obj, typ): if isinstance(obj, typ):
return return
......
...@@ -6,18 +6,15 @@ ...@@ -6,18 +6,15 @@
# the file AUTHORS.rst at the top-level directory of this distribution and at # the file AUTHORS.rst at the top-level directory of this distribution and at
# http://kwant-project.org/authors. # http://kwant-project.org/authors.
import sys
from .._common import ExtensionUnavailable
try: try:
from .discretizer import discretize, discretize_symbolic, build_discretized from .discretizer import discretize, discretize_symbolic, build_discretized
from ._common import sympify, lambdify from ._common import sympify, lambdify
from ._common import momentum_operators, position_operators from ._common import momentum_operators, position_operators
except ImportError: except ImportError as error:
sys.modules[__name__] = ExtensionUnavailable(__name__, ('sympy',)) 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', __all__ = ['discretize', 'discretize_symbolic', 'build_discretized',
'sympify', 'lambdify'] 'sympify', 'lambdify']
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment