Skip to content

Commit bfa0c4c

Browse files
author
Han Wang
committed
test(pt_expt): make the tf32 warn-once test immune to global logging state
set_log_handles (run by any earlier test calling main(), e.g. test_dp_freeze's dispatcher tests) sets the 'deepmd' logger's propagate=False process-wide, which silently empties caplog.records (caplog captures via a root-logger handler and relies on propagation), failing test_enable_tf32_warns_once[True] in ordering-dependent runs. Restore propagation with monkeypatch for the test's duration. Also move TestNativeSpinErrorTranslation above the __main__ block.
1 parent ea00029 commit bfa0c4c

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

source/tests/pt_expt/model/test_get_model_dpa4.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,13 @@ def test_enable_tf32_warns_once(enable_tf32, caplog, monkeypatch) -> None:
285285
# test ordering (other get_sezm_model calls may have already warned)
286286
monkeypatch.setattr(gm_mod, "_WARNED_ONCE", set())
287287

288+
# caplog captures via a ROOT-logger handler and relies on propagation,
289+
# but any earlier test that ran main() (e.g. test_dp_freeze's dispatcher
290+
# tests) leaves set_log_handles' global ``deepmd``-logger
291+
# propagate=False behind (deepmd/loggers/loggers.py), silently emptying
292+
# caplog.records. monkeypatch restores the attribute afterwards.
293+
monkeypatch.setattr(logging.getLogger("deepmd"), "propagate", True)
294+
288295
raw = _make_raw_model_config(enable_tf32=enable_tf32)
289296

290297
with caplog.at_level(logging.WARNING, logger=gm_mod.log.name):
@@ -301,10 +308,6 @@ def test_enable_tf32_warns_once(enable_tf32, caplog, monkeypatch) -> None:
301308
assert not matches, caplog.text
302309

303310

304-
if __name__ == "__main__":
305-
unittest.main()
306-
307-
308311
class TestNativeSpinErrorTranslation(unittest.TestCase):
309312
"""Only the unexpected-``use_spin`` TypeError becomes the capability error."""
310313

@@ -316,3 +319,7 @@ def test_unrelated_construction_error_propagates(self) -> None:
316319
raw["fitting_net"]["bogus_option"] = 1
317320
with self.assertRaisesRegex(TypeError, "bogus_option"):
318321
get_model(raw)
322+
323+
324+
if __name__ == "__main__":
325+
unittest.main()

0 commit comments

Comments
 (0)