From 527c727be4bae73c4e17de45788fd3745ded8d99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Rapin?= Date: Mon, 14 Mar 2022 13:02:01 +0100 Subject: [PATCH] Use try/finally in context manager (#1383) --- nevergrad/parametrization/helpers.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/nevergrad/parametrization/helpers.py b/nevergrad/parametrization/helpers.py index eb727b597..3142edfe0 100644 --- a/nevergrad/parametrization/helpers.py +++ b/nevergrad/parametrization/helpers.py @@ -135,11 +135,13 @@ def deterministic_sampling(parameter: core.Parameter) -> tp.Iterator[None]: deterministic = [lay.deterministic for lay in int_layers] for lay in int_layers: lay.deterministic = True - yield - # sample and reset the previous behavior - parameter.value # pylint: disable=pointless-statement - for lay, det in zip(int_layers, deterministic): - lay.deterministic = det + try: + yield + finally: + # sample and reset the previous behavior + parameter.value # pylint: disable=pointless-statement + for lay, det in zip(int_layers, deterministic): + lay.deterministic = det def _fully_bounded_layers(data: pdata.Data) -> tp.List[_datalayers.BoundLayer]: