Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Refactor unnecessary `else` / `elif` when `if` block has a `return` statement (#1072)

deepsource-autofix[bot]0245954633e9
diff --git a/kornia/augmentation/augmentation.py b/kornia/augmentation/augmentation.py
index 966950a53..3754fa88b 100644
--- a/kornia/augmentation/augmentation.py
+++ b/kornia/augmentation/augmentation.py
@@ -794,7 +794,7 @@ class CenterCrop(GeometricAugmentationBase2D):
return crop_by_transform_mat(
input, transform[:, :2, :], self.size, self.resample.name.lower(), 'zeros', self.align_corners
)
- elif self.cropping_mode == 'slice': # uses advanced slicing to crop
+ if self.cropping_mode == 'slice': # uses advanced slicing to crop
# TODO: implement as separated function `crop_and_resize_iterative`
B, C, _, _ = input.shape
H, W = self.size
@@ -806,8 +806,7 @@ class CenterCrop(GeometricAugmentationBase2D):
y2 = int(params['src'][i, 3, 1]) + 1
out[i] = input[i : i + 1, :, y1:y2, x1:x2]
return out
- else:
- raise NotImplementedError(f"Not supported type: {self.cropping_mode}.")
+ raise NotImplementedError(f"Not supported type: {self.cropping_mode}.")
def inverse_transform(
self,
@@ -1078,7 +1077,7 @@ class RandomCrop(GeometricAugmentationBase2D):
padding_mode='zeros',
align_corners=self.align_corners,
)
- elif self.cropping_mode == 'slice': # uses advanced slicing to crop
+ if self.cropping_mode == 'slice': # uses advanced slicing to crop
B, C, _, _ = input.shape
out = torch.empty(B, C, *self.size, device=input.device, dtype=input.dtype)
for i in range(B):
@@ -1088,8 +1087,7 @@ class RandomCrop(GeometricAugmentationBase2D):
y2 = int(params['src'][i, 3, 1]) + 1
out[i] = input[i : i + 1, :, y1:y2, x1:x2]
return out
- else:
- raise NotImplementedError(f"Not supported type: {self.flags['mode']}.")
+ raise NotImplementedError(f"Not supported type: {self.flags['mode']}.")
def inverse_transform(
self,
@@ -1153,10 +1151,9 @@ class RandomCrop(GeometricAugmentationBase2D):
# undo the pre-crop if nothing happened.
if isinstance(out, tuple) and isinstance(input, tuple):
return input[0], out[1]
- elif isinstance(out, tuple) and not isinstance(input, tuple):
+ if isinstance(out, tuple) and not isinstance(input, tuple):
return input, out[1]
- else:
- return input
+ return input
return out
@@ -1279,7 +1276,7 @@ class RandomResizedCrop(GeometricAugmentationBase2D):
padding_mode='zeros',
align_corners=self.align_corners,
)
- elif self.cropping_mode == 'slice': # uses advanced slicing to crop
+ if self.cropping_mode == 'slice': # uses advanced slicing to crop
B, C, _, _ = input.shape
out = torch.empty(B, C, *self.size, device=input.device, dtype=input.dtype)
for i in range(B):
@@ -1294,8 +1291,7 @@ class RandomResizedCrop(GeometricAugmentationBase2D):
align_corners=self.align_corners,
)
return out
- else:
- raise NotImplementedError(f"Not supported type: {self.cropping_mode}.")
+ raise NotImplementedError(f"Not supported type: {self.cropping_mode}.")
def inverse_transform(
self,
diff --git a/kornia/augmentation/base.py b/kornia/augmentation/base.py
index b83a0f294..c5a7cd96b 100644
--- a/kornia/augmentation/base.py
+++ b/kornia/augmentation/base.py
@@ -186,9 +186,8 @@ class _AugmentationBase(_BasicAugmentationBase):
in_tensor = input[0]
in_transformation = input[1]
return in_tensor, in_transformation
- else:
- in_tensor = input
- return in_tensor, None
+ in_tensor = input
+ return in_tensor, None
def apply_func( # type: ignore
self,
diff --git a/kornia/augmentation/utils/helpers.py b/kornia/augmentation/utils/helpers.py
index fcb6bcdca..95060b767 100644
--- a/kornia/augmentation/utils/helpers.py
+++ b/kornia/augmentation/utils/helpers.py
@@ -203,8 +203,7 @@ def _adapted_rsampling(
"""
if same_on_batch:
return dist.rsample((1, *shape[1:])).repeat(shape[0], *[1] * (len(shape) - 1))
- else:
- return dist.rsample(shape)
+ return dist.rsample(shape)
def _adapted_sampling(
@@ -217,8 +216,7 @@ def _adapted_sampling(
"""
if same_on_batch:
return dist.sample((1, *shape[1:])).repeat(shape[0], *[1] * (len(shape) - 1))
- else:
- return dist.sample(shape)
+ return dist.sample(shape)
def _adapted_uniform(
diff --git a/kornia/geometry/conversions.py b/kornia/geometry/conversions.py
index 10aaa063e..54c191ee7 100644
--- a/kornia/geometry/conversions.py
+++ b/kornia/geometry/conversions.py
@@ -368,8 +368,7 @@ def rotation_matrix_to_quaternion(
qz = safe_zero_division(m10 - m01, sq)
if order == QuaternionCoeffOrder.XYZW:
return torch.cat((qx, qy, qz, qw), dim=-1)
- else:
- return torch.cat((qw, qx, qy, qz), dim=-1)
+ return torch.cat((qw, qx, qy, qz), dim=-1)
def cond_1():
sq = torch.sqrt(1.0 + m00 - m11 - m22 + eps) * 2.0 # sq = 4 * qx.
@@ -379,8 +378,7 @@ def rotation_matrix_to_quaternion(
qz = safe_zero_division(m02 + m20, sq)
if order == QuaternionCoeffOrder.XYZW:
return torch.cat((qx, qy, qz, qw), dim=-1)
- else:
- return torch.cat((qw, qx, qy, qz), dim=-1)
+ return torch.cat((qw, qx, qy, qz), dim=-1)
def cond_2():
sq = torch.sqrt(1.0 + m11 - m00 - m22 + eps) * 2.0 # sq = 4 * qy.
@@ -390,8 +388,7 @@ def rotation_matrix_to_quaternion(
qz = safe_zero_division(m12 + m21, sq)
if order == QuaternionCoeffOrder.XYZW:
return torch.cat((qx, qy, qz, qw), dim=-1)
- else:
- return torch.cat((qw, qx, qy, qz), dim=-1)
+ return torch.cat((qw, qx, qy, qz), dim=-1)
def cond_3():
sq = torch.sqrt(1.0 + m22 - m00 - m11 + eps) * 2.0 # sq = 4 * qz.
@@ -401,8 +398,7 @@ def rotation_matrix_to_quaternion(
qz = 0.25 * sq
if order == QuaternionCoeffOrder.XYZW:
return torch.cat((qx, qy, qz, qw), dim=-1)
- else:
- return torch.cat((qw, qx, qy, qz), dim=-1)
+ return torch.cat((qw, qx, qy, qz), dim=-1)
where_2 = torch.where(m11 > m22, cond_2(), cond_3())
where_1 = torch.where((m00 > m11) & (m00 > m22), cond_1(), where_2)
diff --git a/kornia/geometry/transform/affwarp.py b/kornia/geometry/transform/affwarp.py
index cf759c11f..6ea582534 100644
--- a/kornia/geometry/transform/affwarp.py
+++ b/kornia/geometry/transform/affwarp.py
@@ -506,12 +506,11 @@ def _side_to_image_size(side_size: int, aspect_ratio: float, side: str = "short"
raise ValueError(f"side can be one of 'short', 'long', 'vert', and 'horz'. Got '{side}'")
if side == "vert":
return side_size, int(side_size * aspect_ratio)
- elif side == "horz":
+ if side == "horz":
return int(side_size / aspect_ratio), side_size
- elif (side == "short") ^ (aspect_ratio < 1.0):
+ if (side == "short") ^ (aspect_ratio < 1.0):
return side_size, int(side_size * aspect_ratio)
- else:
- return int(side_size / aspect_ratio), side_size
+ return int(side_size / aspect_ratio), side_size
@perform_keep_shape
diff --git a/kornia/testing/__init__.py b/kornia/testing/__init__.py
index e8964fe78..850bc2a27 100644
--- a/kornia/testing/__init__.py
+++ b/kornia/testing/__init__.py
@@ -119,7 +119,7 @@ def default_with_one_parameter_changed(*, default={}, **possible_parameters):
def _get_precision(device: torch.device, dtype: torch.dtype) -> float:
if 'xla' in device.type:
return 1e-2
- elif dtype == torch.float16:
+ if dtype == torch.float16:
return 1e-3
return 1e-4
diff --git a/test/geometry/test_conversions.py b/test/geometry/test_conversions.py
index cd4d5f5e8..e060b47c1 100644
--- a/test/geometry/test_conversions.py
+++ b/test/geometry/test_conversions.py
@@ -16,8 +16,7 @@ def atol(device, dtype):
"""Lower tolerance for cuda-float16 only"""
if 'cuda' in device.type and dtype == torch.float16:
return 1.0e-3
- else:
- return 1.0e-4
+ return 1.0e-4
@pytest.fixture
@@ -25,8 +24,7 @@ def rtol(device, dtype):
"""Lower tolerance for cuda-float16 only"""
if 'cuda' in device.type and dtype == torch.float16:
return 1.0e-3
- else:
- return 1.0e-4
+ return 1.0e-4
# based on:
diff --git a/test/integration/test_conversions.py b/test/integration/test_conversions.py
index 4e4cbc5c9..3cbb26cf7 100644
--- a/test/integration/test_conversions.py
+++ b/test/integration/test_conversions.py
@@ -12,8 +12,7 @@ def atol(device, dtype):
"""Lower tolerance for cuda-float16 only"""
if 'cuda' in device.type and dtype == torch.float16:
return 1.0e-3
- else:
- return 1.0e-4
+ return 1.0e-4
@pytest.fixture
@@ -21,8 +20,7 @@ def rtol(device, dtype):
"""Lower tolerance for cuda-float16 only"""
if 'cuda' in device.type and dtype == torch.float16:
return 1.0e-3
- else:
- return 1.0e-4
+ return 1.0e-4
class TestAngleAxisToQuaternionToAngleAxis: