forked from sympy/sympy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_submodule_imports.py
executable file
·90 lines (73 loc) · 2.14 KB
/
test_submodule_imports.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python
"""
Test that
from sympy import *
only imports those sympy submodules that have names that are part of the
top-level namespace.
"""
import sys
import os
# hook in-tree SymPy into Python path, if possible
this_path = os.path.abspath(__file__)
this_dir = os.path.dirname(this_path)
sympy_top = os.path.split(this_dir)[0]
sympy_dir = os.path.join(sympy_top, 'sympy')
if os.path.isdir(sympy_dir):
sys.path.insert(0, sympy_top)
submodule_whitelist = [
'algebras',
'assumptions',
'calculus',
'combinatorics',
'concrete',
'core',
'deprecated',
'discrete',
'external',
'functions',
'geometry',
'integrals',
'interactive',
'logic',
'matrices',
'multipledispatch',
'ntheory',
'parsing',
'plotting',
'polys',
'printing',
'release',
'series',
'sets',
'simplify',
'solvers',
'strategies',
'tensor',
'testing',
'utilities',
]
def test_submodule_imports():
if 'sympy' in sys.modules:
raise RuntimeError("SymPy has already been imported, the test_submodule_imports test cannot run")
exec("from sympy import *", {})
for mod in sys.modules:
if not mod.startswith('sympy'):
continue
if not mod.count('.') == 1:
continue
_, submodule = mod.split('.')
if submodule not in submodule_whitelist:
sys.exit(f"""\
Error: The submodule {mod} was imported with 'from sympy import *', but it was
not expected to be.
If {mod} is a new module that has functions that are imported at the
top-level, then the whitelist in bin/test_submodule_imports should be updated.
If it is not, the place that imports it should be modified so that it does not
get imported at the top-level, e.g., by moving the 'import {mod}' import
inside the function that uses it.
If you are unsure which code is importing {mod}, it may help to add 'raise
Exception' to sympy/{submodule}/__init__.py and observe the traceback from
running 'from sympy import *'.""")
print("No unexpected submodules were imported with 'from sympy import *'")
if __name__ == '__main__':
test_submodule_imports()