From 9b56110eed06d480a203a26a343fb6531378fc05 Mon Sep 17 00:00:00 2001 From: David-Wu1119 <133224895+David-Wu1119@users.noreply.github.com> Date: Mon, 28 Sep 2026 16:29:02 -0400 Subject: [PATCH] [BugFix] Format the key and class name into two error messages DiscreteCQLLoss.cql_loss and ParallelEnv._shutdown_workers built their error messages without an f-prefix, so users saw the literal "{self.tensor_keys.pred_val}" and "{self.__class__.__name__}". Co-Authored-By: Claude Opus 5.5 --- test/objectives/test_cql.py | 8 ++++++++ torchrl/envs/batched_envs.py | 2 +- torchrl/objectives/cql.py | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/test/objectives/test_cql.py b/test/objectives/test_cql.py index 02d12721eec..94866cf4dc9 100644 --- a/test/objectives/test_cql.py +++ b/test/objectives/test_cql.py @@ -1063,6 +1063,14 @@ def test_dcql_tensordict_run(self, action_spec_type, td_est): _ = loss_fn(td) assert loss_fn.tensor_keys.priority in td.keys() + def test_dcql_cql_loss_before_value_loss(self): + actor = self._create_mock_actor(action_spec_type="one_hot") + loss_fn = DiscreteCQLLoss(actor) + loss_fn.set_keys(pred_val="custom_pred_val") + td = self._create_mock_data_dcql(action_spec_type="one_hot") + with pytest.raises(KeyError, match="with key custom_pred_val in"): + loss_fn.cql_loss(td) + @pytest.mark.parametrize("observation_key", ["observation", "observation2"]) @pytest.mark.parametrize("reward_key", ["reward", "reward2"]) @pytest.mark.parametrize("done_key", ["done", "done2"]) diff --git a/torchrl/envs/batched_envs.py b/torchrl/envs/batched_envs.py index 9c410ebd2f2..fffa730eadc 100644 --- a/torchrl/envs/batched_envs.py +++ b/torchrl/envs/batched_envs.py @@ -3213,7 +3213,7 @@ def _shutdown_workers(self) -> None: try: if self.is_closed: raise RuntimeError( - "calling {self.__class__.__name__}._shutdown_workers only allowed when env.is_closed = False" + f"calling {self.__class__.__name__}._shutdown_workers only allowed when env.is_closed = False" ) if self._metadata_from_workers: # Expensive native environments may retain renderer resources diff --git a/torchrl/objectives/cql.py b/torchrl/objectives/cql.py index 0948a07ee43..f599eb0bb9d 100644 --- a/torchrl/objectives/cql.py +++ b/torchrl/objectives/cql.py @@ -1337,7 +1337,7 @@ def cql_loss(self, tensordict): qvalues = tensordict.get(self.tensor_keys.pred_val, default=None) if qvalues is None: raise KeyError( - "Couldn't find the predicted qvalue with key {self.tensor_keys.pred_val} in the input tensordict. " + f"Couldn't find the predicted qvalue with key {self.tensor_keys.pred_val} in the input tensordict. " "This could be caused by calling cql_loss method before value_loss." )