Skip to content

Commit f6e1e1e

Browse files
authored
Fixing import of PotentialTools in WallGo/__init__.py (#341)
* Removed import of PotentialTools in WallGo/__init__.py * Wrote draft of lazy import for PotentialTools * Relative import again * Tweak * Solved import error locally * Changed import statements in tests * Trying something on scipy * Installing with -e * Removed _version * Removed version from test * Made main tests the same * Added comment * Removed -e for testing again * Tweak * Revert * Changed [tool.setuptools] in pyproject.toml * Fixed pyproject.toml * Package data update for pyproject.toml * Removed __all__ from __init__. * Updated tests to include both 3.10 and 3.13
1 parent 96fb405 commit f6e1e1e

File tree

8 files changed

+38
-33
lines changed

8 files changed

+38
-33
lines changed

.github/workflows/main.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717

1818
matrix:
1919
os: [ubuntu-latest, macos-latest, windows-latest]
20-
python-version: ["3.11"]
20+
python-version: ["3.10", "3.13"]
2121

2222
steps:
2323
- uses: actions/checkout@v4
@@ -31,4 +31,4 @@ jobs:
3131
run: pip install .[tests]
3232

3333
- name: Test with pytest
34-
run: cd ${{ github.workspace }} && pytest -vs
34+
run: pytest -vs

.github/workflows/publish.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
- name: Set up Python
2323
uses: actions/setup-python@v4
2424
with:
25-
python-version: '3.11'
25+
python-version: "3.11"
2626
- name: Install dependencies
2727
run: |
2828
python -m pip install --upgrade pip

.github/workflows/tests.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717

1818
matrix:
1919
os: [ubuntu-latest, macos-latest, windows-latest]
20-
python-version: ["3.11"]
20+
python-version: ["3.10", "3.13"]
2121

2222
steps:
2323
- uses: actions/checkout@v4
@@ -31,4 +31,4 @@ jobs:
3131
run: pip install .[tests]
3232

3333
- name: Test with pytest
34-
run: cd ${{ github.workspace }} && pytest -vs
34+
run: pytest -vs

pyproject.toml

+3-6
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,13 @@ Issues = "https://github.com/Wall-Go/WallGo/issues"
6868
Changelog = "https://github.com/Wall-Go/WallGo/blob/main/CHANGELOG.rst"
6969

7070
[tool.setuptools]
71+
packages = ["WallGo", "WallGo.PotentialTools"]
72+
package-dir = {"" = "src"}
7173
include-package-data = true
7274

73-
[tool.setuptools.packages.find]
74-
where = ["src"]
75-
include = ["WallGo"]
76-
#exclude = [ ... ]
77-
7875
[tool.setuptools.package-data]
7976
# specify data files relative to packages. These need to be located inside the package. Including files from outside packages is apparently deprecated
80-
WallGo = ["Config/*.ini", "Data/*"]
77+
"WallGo.PotentialTools" = ["Config/*.ini", "Data/*.txt"]
8178

8279
[tool.setuptools_scm]
8380

src/WallGo/PotentialTools/__init__.py

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from .utils import getSafePathToResource
88

99

10-
1110
_bInitialized = False # pylint: disable=invalid-name
1211
"""Configuration settings, using class from WallGo"""
1312
config = configparser.ConfigParser()

src/WallGo/__init__.py

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
"""Initialising WallGo package"""
22

3+
import types
34
import warnings
4-
5-
# subpackage
6-
from . import PotentialTools
5+
import importlib
76

87
# package level modules
98
from .boltzmann import BoltzmannSolver
9+
from .config import Config
1010
from .collisionArray import CollisionArray
1111
from .containers import PhaseInfo, BoltzmannBackground, BoltzmannDeltas, WallParams
1212
from .effectivePotential import EffectivePotential, VeffDerivativeSettings
@@ -27,7 +27,18 @@
2727
from .results import WallGoResults
2828
from .utils import getSafePathToResource
2929

30-
from .config import Config
30+
# list of submodules for lazy importing
31+
submodules = ["PotentialTools"]
32+
33+
34+
def __getattr__(name: str) -> types.ModuleType: # pylint: disable=invalid-name
35+
"""Lazy subpackage import, following Numpy and Scipy"""
36+
if name in submodules:
37+
return importlib.import_module(f'WallGo.{name}')
38+
try:
39+
return globals()[name]
40+
except KeyError as esc:
41+
raise AttributeError(f"Module 'WallGo' has no attribute '{name}'") from esc
3142

3243

3344
global _bCollisionModuleAvailable # pylint: disable=invalid-name

tests/testsPotentialTools/test_IntegralInterpolation.py

+13-15
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
import pytest
55

66
from WallGo import InterpolatableFunction, EExtrapolationType
7-
87
from WallGo import PotentialTools
9-
from WallGo.PotentialTools import JbIntegral, JfIntegral
108

119
### Test real parts of Jb, Jf integrals
1210

@@ -20,7 +18,7 @@
2018
)
2119
def test_directJb(x: float, expectedResult: np.array) -> None:
2220

23-
Jb = JbIntegral(bUseAdaptiveInterpolation=False)
21+
Jb = PotentialTools.JbIntegral(bUseAdaptiveInterpolation=False)
2422
assert Jb(x) == pytest.approx(expectedResult, rel=1e-6)
2523

2624

@@ -30,7 +28,7 @@ def test_directJb(x: float, expectedResult: np.array) -> None:
3028
)
3129
def test_directJb_derivative(x: float, expectedResult: float) -> None:
3230

33-
Jb = JbIntegral(bUseAdaptiveInterpolation=False)
31+
Jb = PotentialTools.JbIntegral(bUseAdaptiveInterpolation=False)
3432
assert Jb.derivative(x, 1, False) == pytest.approx(expectedResult, rel=1e-6)
3533

3634

@@ -40,7 +38,7 @@ def test_directJb_derivative(x: float, expectedResult: float) -> None:
4038
)
4139
def test_directJf(x: float, expectedResult: float) -> None:
4240

43-
Jf = JfIntegral(bUseAdaptiveInterpolation=False)
41+
Jf = PotentialTools.JfIntegral(bUseAdaptiveInterpolation=False)
4442
assert Jf(x) == pytest.approx(expectedResult, rel=1e-6)
4543

4644

@@ -50,21 +48,21 @@ def test_directJf(x: float, expectedResult: float) -> None:
5048
)
5149
def test_directJf_derivative(x: float, expectedResult: float) -> None:
5250

53-
Jf = JfIntegral(bUseAdaptiveInterpolation=False)
51+
Jf = PotentialTools.JfIntegral(bUseAdaptiveInterpolation=False)
5452
assert Jf.derivative(x, 1, False) == pytest.approx(expectedResult, rel=1e-6)
5553

5654

5755
## Interpolated Jb integral fixture, no extrapolation. The interpolation here is very rough to make this run fast
5856
@pytest.fixture()
5957
def Jb_interpolated() -> InterpolatableFunction:
60-
Jb = JbIntegral(bUseAdaptiveInterpolation=False)
58+
Jb = PotentialTools.JbIntegral(bUseAdaptiveInterpolation=False)
6159
Jb.newInterpolationTable(1.0, 10.0, 100)
6260
return Jb
6361

6462

6563
@pytest.fixture()
6664
def Jf_interpolated() -> InterpolatableFunction:
67-
Jf = JfIntegral(bUseAdaptiveInterpolation=False)
65+
Jf = PotentialTools.JfIntegral(bUseAdaptiveInterpolation=False)
6866
Jf.newInterpolationTable(1.0, 10.0, 100)
6967
return Jf
7068

@@ -94,7 +92,7 @@ def Jf_interpolated() -> InterpolatableFunction:
9492
],
9593
)
9694
def test_Jb_interpolated(
97-
Jb_interpolated: JbIntegral,
95+
Jb_interpolated: PotentialTools.JbIntegral,
9896
x: Union[float, np.array],
9997
expectedResult: Union[float, np.array],
10098
) -> None:
@@ -103,7 +101,7 @@ def test_Jb_interpolated(
103101

104102

105103
@pytest.mark.parametrize("x", [-5, -1, 0, 0.5, 1, 5, 10])
106-
def test_Jb_derivative_interpolated(Jb_interpolated: JbIntegral, x: float) -> None:
104+
def test_Jb_derivative_interpolated(Jb_interpolated: PotentialTools.JbIntegral, x: float) -> None:
107105
np.testing.assert_allclose(
108106
Jb_interpolated.derivative(x, 1, True),
109107
Jb_interpolated.derivative(x, 1, False),
@@ -113,7 +111,7 @@ def test_Jb_derivative_interpolated(Jb_interpolated: JbIntegral, x: float) -> No
113111

114112
@pytest.mark.parametrize("x", [-5, -1, 0, 0.5, 1, 5, 10])
115113
def test_Jb_second_derivative_interpolated(
116-
Jb_interpolated: JbIntegral, x: float
114+
Jb_interpolated: PotentialTools.JbIntegral, x: float
117115
) -> None:
118116
np.testing.assert_allclose(
119117
Jb_interpolated.derivative(x, 2, True),
@@ -124,7 +122,7 @@ def test_Jb_second_derivative_interpolated(
124122

125123

126124
@pytest.mark.parametrize("x", [-5, -1, 0, 0.5, 1, 5, 10])
127-
def test_Jf_derivative_interpolated(Jf_interpolated: JfIntegral, x: float) -> None:
125+
def test_Jf_derivative_interpolated(Jf_interpolated: PotentialTools.JfIntegral, x: float) -> None:
128126
np.testing.assert_allclose(
129127
Jf_interpolated.derivative(x, 1, True),
130128
Jf_interpolated.derivative(x, 1, False),
@@ -134,7 +132,7 @@ def test_Jf_derivative_interpolated(Jf_interpolated: JfIntegral, x: float) -> No
134132

135133
@pytest.mark.parametrize("x", [-5, -1, 0, 0.5, 1, 5, 10])
136134
def test_Jf_second_derivative_interpolated(
137-
Jf_interpolated: JfIntegral, x: float
135+
Jf_interpolated: PotentialTools.JfIntegral, x: float
138136
) -> None:
139137
np.testing.assert_allclose(
140138
Jf_interpolated.derivative(x, 2, True),
@@ -148,7 +146,7 @@ def test_Jf_second_derivative_interpolated(
148146

149147

150148
## Got lazy with parametrization here, so this is just one big function now
151-
def test_Jb_extrapolation_constant(Jb_interpolated: JbIntegral) -> None:
149+
def test_Jb_extrapolation_constant(Jb_interpolated: PotentialTools.JbIntegral) -> None:
152150

153151
Jb = Jb_interpolated
154152
Jb.setExtrapolationType(
@@ -197,7 +195,7 @@ def test_Jb_extrapolation_constant(Jb_interpolated: JbIntegral) -> None:
197195

198196

199197
##
200-
def test_Jb_extend_range(Jb_interpolated: JbIntegral) -> None:
198+
def test_Jb_extend_range(Jb_interpolated: PotentialTools.JbIntegral) -> None:
201199

202200
Jb = Jb_interpolated
203201
relativeTolerance = 1e-6

tests/testsPotentialTools/test_setup.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33

44
def test_importPotentialTools() -> None:
5-
"""Testing import of WallGo"""
6-
from WallGo import PotentialTools
5+
"""Testing import of PotentialTools from WallGo"""
6+
from WallGo import PotentialTools

0 commit comments

Comments
 (0)