Skip to content

Commit 3102314

Browse files
committed
qspace_light: explain the CellConstructor requirement instead of a TypeError
The light mode calls DiagonalizeSupercell(q_only=True), which only newer CellConstructor releases provide. Against an older one the flag failed with 'DiagonalizeSupercell() got an unexpected keyword argument q_only', raised from inside __setattr__ and pointing at neither the flag nor the requirement. A module-level probe now reports the capability, so the constructor raises an explanatory error, and tests/test_qspace_light skips instead of failing when the installed CellConstructor cannot support it. Nothing else changes: the default path never touches q_only.
1 parent 07b346d commit 3102314

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

Modules/Ensemble.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,17 @@ def Main(self):
107107
# Deprecated alias kept for backward compatibility: it only tells whether a
108108
# Julia backend is installed, the runtime is not initialized at import time.
109109
__JULIA_EXT__ = JuliaExt.available()
110+
111+
# The qspace_light path is built on DiagonalizeSupercell(q_only=True), which
112+
# only newer CellConstructor releases provide. Probe it once, so that asking
113+
# for the flag against an older one gives an explanatory error instead of a
114+
# TypeError raised from inside __setattr__.
115+
import inspect as _inspect
116+
try:
117+
_CC_HAS_Q_ONLY = "q_only" in _inspect.signature(
118+
CC.Phonons.Phonons.DiagonalizeSupercell).parameters
119+
except (AttributeError, TypeError, ValueError):
120+
_CC_HAS_Q_ONLY = False
110121
__JULIA_ERROR__ = ""
111122

112123

@@ -351,6 +362,14 @@ def __setattr__(self, name, value):
351362

352363
if name == "dyn_0":
353364
if self.__dict__.get("qspace_light", False):
365+
if not _CC_HAS_Q_ONLY:
366+
raise RuntimeError(
367+
"Ensemble(qspace_light=True) requires a CellConstructor "
368+
"providing DiagonalizeSupercell(q_only=True); the "
369+
"installed one does not have it, so the light mode "
370+
"cannot avoid the dense (3N,3N) allocation it exists "
371+
"to avoid. Use qspace_light=False (the default), or "
372+
"install the CellConstructor that provides q_only.")
354373
# q-space light mode: q_only=True never assembles the (3N,3N)
355374
# supercell polarization matrix. Only the O(N) frequencies,
356375
# the q-space pols and the sorted-mode -> (iq, band) maps are

tests/test_qspace_light/test_qspace_light.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@
2424
import cellconstructor.Phonons
2525
import sscha, sscha.Ensemble
2626

27+
# The light mode is built on DiagonalizeSupercell(q_only=True), which only
28+
# newer CellConstructor releases provide. Skip the whole module rather than
29+
# fail when it is missing: nothing here can run, and the failure would say
30+
# nothing about this package.
31+
if not bool(getattr(sscha.Ensemble, "_CC_HAS_Q_ONLY", False)):
32+
pytest.skip(
33+
"the installed CellConstructor has no "
34+
"DiagonalizeSupercell(q_only=True), which qspace_light requires",
35+
allow_module_level=True)
36+
2737
_JULIA = bool(getattr(sscha.Ensemble, "__JULIA_EXT__", False))
2838
needs_julia = pytest.mark.skipif(
2939
not _JULIA, reason="Julia Fourier backend not available")

0 commit comments

Comments
 (0)