From 83fab5fde5ab308f7cf94390681a01ac47788b89 Mon Sep 17 00:00:00 2001
From: Christoph Groth <christoph.groth@cea.fr>
Date: Tue, 19 Mar 2019 16:51:49 +0100
Subject: [PATCH] merge contents of submodules in a more static way

This allows static analyzers like "Jedi" to work.
Closes #223
---
 kwant/__init__.py           | 31 +++++++++++++++++++------------
 kwant/continuum/__init__.py |  1 -
 kwant/graph/__init__.py     |  9 ++++-----
 kwant/linalg/__init__.py    | 11 +++++++----
 kwant/physics/__init__.py   | 16 +++++++++++-----
 5 files changed, 41 insertions(+), 27 deletions(-)

diff --git a/kwant/__init__.py b/kwant/__init__.py
index 054657c3..d8c88ebf 100644
--- a/kwant/__init__.py
+++ b/kwant/__init__.py
@@ -29,22 +29,29 @@ except ImportError:
         raise
 
 from ._common import KwantDeprecationWarning, UserCodeError
-
 __all__.extend(['KwantDeprecationWarning', 'UserCodeError'])
 
-for module in ['system', 'builder', 'lattice', 'solvers', 'digest', 'rmt',
-               'operator', 'kpm', 'wraparound']:
-    exec('from . import {0}'.format(module))
-    __all__.append(module)
+# Pre-import most submodules.  (We make exceptions for submodules that have
+# special dependencies or where that would take too long.)
+from . import system
+from . import builder
+from . import lattice
+from . import solvers
+from . import digest
+from . import rmt
+from . import operator
+from . import kpm
+from . import wraparound
+__all__.extend(['system', 'builder', 'lattice', 'solvers', 'digest', 'rmt',
+                'operator', 'kpm', 'wraparound'])
 
 # Make selected functionality available directly in the root namespace.
-available = [('builder', ['Builder', 'HoppingKind']),
-             ('lattice', ['TranslationalSymmetry']),
-             ('solvers.default',
-              ['smatrix', 'greens_function', 'ldos', 'wave_function'])]
-for module, names in available:
-    exec('from .{0} import {1}'.format(module, ', '.join(names)))
-    __all__.extend(names)
+from .builder import Builder, HoppingKind
+__all__.extend(['Builder', 'HoppingKind'])
+from .lattice import TranslationalSymmetry
+__all__.extend(['TranslationalSymmetry'])
+from .solvers.default import smatrix, greens_function, ldos, wave_function
+__all__.extend(['smatrix', 'greens_function', 'ldos', 'wave_function'])
 
 # Importing plotter might not work, but this does not have to be a problem --
 # only no plotting will be available.
diff --git a/kwant/continuum/__init__.py b/kwant/continuum/__init__.py
index c445f440..fd08848f 100644
--- a/kwant/continuum/__init__.py
+++ b/kwant/continuum/__init__.py
@@ -15,6 +15,5 @@ except ImportError as error:
            "dependencies is not installed.")
     raise ImportError(msg) from error
 
-
 __all__ = ['discretize', 'discretize_symbolic', 'build_discretized',
            'sympify', 'lambdify', 'momentum_operators', 'position_operators']
diff --git a/kwant/graph/__init__.py b/kwant/graph/__init__.py
index 950ded75..2ea8e963 100644
--- a/kwant/graph/__init__.py
+++ b/kwant/graph/__init__.py
@@ -9,8 +9,7 @@
 """Functionality for graphs"""
 
 # Merge the public interface of all submodules.
-__all__ = []
-for module in ['core', 'defs']:
-    exec('from . import {0}'.format(module))
-    exec('from .{0} import *'.format(module))
-    exec('__all__.extend({0}.__all__)'.format(module))
+from .core import *
+from .defs import *
+
+__all__ = [core.__all__ + defs.__all__]
diff --git a/kwant/linalg/__init__.py b/kwant/linalg/__init__.py
index cd46cf5a..2456b350 100644
--- a/kwant/linalg/__init__.py
+++ b/kwant/linalg/__init__.py
@@ -10,7 +10,10 @@ __all__ = ['lapack']
 from . import lapack
 
 # Merge the public interface of the other submodules.
-for module in ['decomp_lu', 'decomp_ev', 'decomp_schur']:
-    exec('from . import {0}'.format(module))
-    exec('from .{0} import *'.format(module))
-    exec('__all__.extend({0}.__all__)'.format(module))
+from .decomp_lu import *
+from .decomp_schur import *
+from .decomp_ev import *
+
+__all__.extend([decomp_lu.__all__,
+                decomp_ev.__all__,
+                decomp_schur.__all__])
diff --git a/kwant/physics/__init__.py b/kwant/physics/__init__.py
index d59fc85e..199d68d2 100644
--- a/kwant/physics/__init__.py
+++ b/kwant/physics/__init__.py
@@ -8,8 +8,14 @@
 """Physics-related algorithms"""
 
 # Merge the public interface of all submodules.
-__all__ = []
-for module in ['leads', 'dispersion', 'noise', 'symmetry', 'gauge']:
-    exec('from . import {0}'.format(module))
-    exec('from .{0} import *'.format(module))
-    exec('__all__.extend({0}.__all__)'.format(module))
+from .leads import *
+from .dispersion import *
+from .noise import *
+from .symmetry import *
+from .gauge import *
+
+__all__ = [leads.__all__
+           + dispersion.__all__
+           + noise.__all__
+           + symmetry.__all__
+           + gauge.__all__]
-- 
GitLab