From 9b81f101972881e5a25ba75256076f7820103cf4 Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Fri, 22 Sep 2023 09:43:19 +0000 Subject: [PATCH] refactor: remove assert statement from non-test files (#7029) Usage of `assert` statement in application logic is discouraged. `assert` is removed with compiling to optimized byte code. Consider raising an exception instead. Ideally, `assert` statement should be used only in tests. --------- Co-authored-by: deepsource-autofix[bot] <62050782+deepsource-autofix[bot]@users.noreply.github.com> --- monai/apps/utils.py | 2 +- monai/networks/blocks/pos_embed_utils.py | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/monai/apps/utils.py b/monai/apps/utils.py index 35322be2a8..eee004f27d 100644 --- a/monai/apps/utils.py +++ b/monai/apps/utils.py @@ -208,7 +208,7 @@ def download_url( with urlopen(url) as response: code = response.getcode() if code == 200: - download_url = json.loads(response.read())["href"] + download_url = json.load(response)["href"] _download_with_progress(download_url, tmp_name, progress=progress) else: raise RuntimeError( diff --git a/monai/networks/blocks/pos_embed_utils.py b/monai/networks/blocks/pos_embed_utils.py index e1f47cd7e9..138149cac6 100644 --- a/monai/networks/blocks/pos_embed_utils.py +++ b/monai/networks/blocks/pos_embed_utils.py @@ -57,7 +57,8 @@ def build_sincos_position_embedding( grid_h, grid_w = torch.meshgrid(grid_h, grid_w, indexing="ij") - assert embed_dim % 4 == 0, "Embed dimension must be divisible by 4 for 2D sin-cos position embedding" + if embed_dim % 4 != 0: + raise AssertionError("Embed dimension must be divisible by 4 for 2D sin-cos position embedding") pos_dim = embed_dim // 4 omega = torch.arange(pos_dim, dtype=torch.float32) / pos_dim @@ -75,7 +76,8 @@ def build_sincos_position_embedding( grid_h, grid_w, grid_d = torch.meshgrid(grid_h, grid_w, grid_d, indexing="ij") - assert embed_dim % 6 == 0, "Embed dimension must be divisible by 6 for 3D sin-cos position embedding" + if embed_dim % 6 != 0: + raise AssertionError("Embed dimension must be divisible by 6 for 3D sin-cos position embedding") pos_dim = embed_dim // 6 omega = torch.arange(pos_dim, dtype=torch.float32) / pos_dim