From 25c905235dbcc369bc94c6487c1df7a06c32e362 Mon Sep 17 00:00:00 2001 From: HGSilveri Date: Tue, 15 Oct 2024 13:35:21 +0200 Subject: [PATCH] Improve the NoiseModel unused parameters warning message --- pulser-core/pulser/noise_model.py | 6 +++++- tests/test_noise_model.py | 11 ++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/pulser-core/pulser/noise_model.py b/pulser-core/pulser/noise_model.py index 36fad053f..e46a9533b 100644 --- a/pulser-core/pulser/noise_model.py +++ b/pulser-core/pulser/noise_model.py @@ -258,12 +258,16 @@ def to_tuple(obj: tuple) -> tuple: object.__setattr__( self, "noise_types", tuple(sorted(true_noise_types)) ) + non_zero_relevant_params = [ + p for p in relevant_params if param_vals[p] + ] for param_, val_ in param_vals.items(): object.__setattr__(self, param_, val_) if val_ and param_ not in relevant_params: warnings.warn( f"{param_!r} is not used by any active noise type " - f"{self.noise_types}.", + f"in {self.noise_types} when the only defined parameters " + f"are {non_zero_relevant_params}.", stacklevel=2, ) diff --git a/tests/test_noise_model.py b/tests/test_noise_model.py index 9977dcf36..c61463e93 100644 --- a/tests/test_noise_model.py +++ b/tests/test_noise_model.py @@ -13,6 +13,8 @@ # limitations under the License. from __future__ import annotations +import re + import numpy as np import pytest @@ -86,7 +88,14 @@ def test_init(self, params, noise_types): ) @pytest.mark.parametrize("unused_param", ["runs", "samples_per_run"]) def test_unused_params(self, unused_param, noise_param): - with pytest.warns(UserWarning, match=f"'{unused_param}' is not used"): + with pytest.warns( + UserWarning, + match=re.escape( + f"'{unused_param}' is not used by any active noise type in" + f" {(_PARAM_TO_NOISE_TYPE[noise_param],)} when the only " + f"defined parameters are {[noise_param]}" + ), + ): NoiseModel(**{unused_param: 100, noise_param: 1.0}) @pytest.mark.parametrize(