π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(augmentation): resolve Ruff RET505 warnings (#4618)
Daniele Loru34e57c912a08
diff --git a/kornia/augmentation/_2d/base.py b/kornia/augmentation/_2d/base.pyindex d29fda88b..32f282884 100644--- a/kornia/augmentation/_2d/base.py+++ b/kornia/augmentation/_2d/base.py@@ -169,8 +169,8 @@ class AugmentationBase2D(_AugmentationBase):if shape is None:return _transform_input(input)- else:- return _transform_input_by_shape(input, reference_shape=shape, match_channel=match_channel)++ return _transform_input_by_shape(input, reference_shape=shape, match_channel=match_channel)class RigidAffineAugmentationBase2D(AugmentationBase2D):diff --git a/kornia/augmentation/_2d/mix/base.py b/kornia/augmentation/_2d/mix/base.pyindex 59bd870e9..18e34e504 100644--- a/kornia/augmentation/_2d/mix/base.py+++ b/kornia/augmentation/_2d/mix/base.py@@ -70,8 +70,8 @@ class MixAugmentationBaseV2(_BasicAugmentationBase):if shape is None:return _transform_input(input)- else:- return _transform_input_by_shape(input, reference_shape=shape, match_channel=match_channel)++ return _transform_input_by_shape(input, reference_shape=shape, match_channel=match_channel)def apply_transform(self, input: torch.Tensor, params: Dict[str, torch.Tensor], flags: Dict[str, Any]diff --git a/kornia/augmentation/_2d/mix/transplantation.py b/kornia/augmentation/_2d/mix/transplantation.pyindex 602f79a7a..73d9e7596 100644--- a/kornia/augmentation/_2d/mix/transplantation.py+++ b/kornia/augmentation/_2d/mix/transplantation.py@@ -352,5 +352,5 @@ class RandomTransplantation(MixAugmentationBaseV2):if len(outputs) == 1:return outputs[0]- else:- return outputs++ return outputsdiff --git a/kornia/augmentation/_3d/base.py b/kornia/augmentation/_3d/base.pyindex 718a6c843..47c3489fc 100644--- a/kornia/augmentation/_3d/base.py+++ b/kornia/augmentation/_3d/base.py@@ -52,8 +52,8 @@ class AugmentationBase3D(_AugmentationBase):_validate_input_dtype(input, accepted_dtypes=[torch.bfloat16, float16, float32, float64])if shape is None:return _transform_input3d(input)- else:- return _transform_input3d_by_shape(input, reference_shape=shape, match_channel=match_channel)++ return _transform_input3d_by_shape(input, reference_shape=shape, match_channel=match_channel)def identity_matrix(self, input: torch.Tensor) -> torch.Tensor:"""Return 4x4 identity matrix."""diff --git a/kornia/augmentation/container/augment.py b/kornia/augmentation/container/augment.pyindex c24d28541..6f1f70a8e 100644--- a/kornia/augmentation/container/augment.py+++ b/kornia/augmentation/container/augment.py@@ -396,8 +396,8 @@ class AugmentationSequential(TransformMatrixMinIn, ImageSequential):"""Return identity matrix."""if self.contains_3d_augmentation:return eye_like(4, input)- else:- return eye_like(3, input)++ return eye_like(3, input)def inverse( # type: ignore[override]self,@@ -705,8 +705,7 @@ class AugmentationSequential(TransformMatrixMinIn, ImageSequential):new_arg.append(a_new)return new_arg- else:- arg = arg.to(self.input_dtype) if self.input_dtype else arg.to(torch.float)+ arg = arg.to(self.input_dtype) if self.input_dtype else arg.to(torch.float)return argdef _postproc_mask(self, arg: MaskDataType) -> MaskDataType:@@ -717,8 +716,7 @@ class AugmentationSequential(TransformMatrixMinIn, ImageSequential):new_arg.append(a_new)return new_arg- else:- arg = arg.to(self.mask_dtype) if self.mask_dtype else arg.to(torch.float)+ arg = arg.to(self.mask_dtype) if self.mask_dtype else arg.to(torch.float)return argdef _preproc_boxes(self, arg: DataType, dcate: DataKey) -> Boxes:@@ -732,14 +730,13 @@ class AugmentationSequential(TransformMatrixMinIn, ImageSequential):raise ValueError(f"Unsupported mode `{DataKey.get(dcate).name}`.")if isinstance(arg, Boxes):return arg- elif self.contains_video_sequential:+ if self.contains_video_sequential:arg = cast(torch.Tensor, arg)return VideoBoxes.from_tensor(arg)- elif self.contains_3d_augmentation:+ if self.contains_3d_augmentation:raise NotImplementedError("3D box handlers are not yet supported.")- else:- arg = cast(torch.Tensor, arg)- return Boxes.from_tensor(arg, mode=mode)+ arg = cast(torch.Tensor, arg)+ return Boxes.from_tensor(arg, mode=mode)def _postproc_boxes(self, in_arg: DataType, out_arg: Boxes, dcate: DataKey@@ -756,8 +753,8 @@ class AugmentationSequential(TransformMatrixMinIn, ImageSequential):# TODO: handle 3d scenariosif isinstance(in_arg, Boxes):return out_arg- else:- return out_arg.to_tensor(mode=mode)++ return out_arg.to_tensor(mode=mode)def _preproc_keypoints(self, arg: DataType, dcate: DataKey) -> Keypoints:dtype = None@@ -773,23 +770,22 @@ class AugmentationSequential(TransformMatrixMinIn, ImageSequential):arg = arg.float()video_result = VideoKeypoints.from_tensor(arg)return video_result.type(dtype) if dtype else video_result- elif self.contains_3d_augmentation:+ if self.contains_3d_augmentation:raise NotImplementedError("3D keypoint handlers are not yet supported.")- elif isinstance(arg, Keypoints):+ if isinstance(arg, Keypoints):return arg- else:- arg = cast(torch.Tensor, arg)- if not torch.is_floating_point(arg):- dtype = arg.dtype- arg = arg.float()- # TODO: Add List[torch.Tensor] in the future.- result = Keypoints.from_tensor(arg)- return result.type(dtype) if dtype else result+ arg = cast(torch.Tensor, arg)+ if not torch.is_floating_point(arg):+ dtype = arg.dtype+ arg = arg.float()+ # TODO: Add List[torch.Tensor] in the future.+ result = Keypoints.from_tensor(arg)+ return result.type(dtype) if dtype else resultdef _postproc_keypoint(self, in_arg: DataType, out_arg: Keypoints, dcate: DataKey) -> Union[torch.Tensor, List[torch.Tensor], Keypoints]:if isinstance(in_arg, Keypoints):return out_arg- else:- return out_arg.to_tensor()++ return out_arg.to_tensor()diff --git a/kornia/augmentation/container/image.py b/kornia/augmentation/container/image.pyindex e4fa18038..f51b84731 100644--- a/kornia/augmentation/container/image.py+++ b/kornia/augmentation/container/image.py@@ -378,7 +378,7 @@ class ImageSequential(ImageSequentialBase, ImageModuleForSequentialMixIn):for arg in self.children():if isinstance(arg, ImageSequential) and not arg.is_intensity_only(strict):return False- elif isinstance(arg, ImageSequential):+ if isinstance(arg, ImageSequential):passelif isinstance(arg, K.IntensityAugmentationBase2D):passdiff --git a/kornia/augmentation/container/video.py b/kornia/augmentation/container/video.pyindex c94fb9792..b92dda12c 100644--- a/kornia/augmentation/container/video.py+++ b/kornia/augmentation/container/video.py@@ -179,9 +179,9 @@ class VideoSequential(ImageSequential):if same_on_frame and same_on_batch:return v.repeat(batch_shape[0] * frame_num, *([1] * (v.ndim - 1)))- elif same_on_frame:+ if same_on_frame:return self.__repeat_param_across_channels__(v, frame_num)- elif same_on_batch:+ if same_on_batch:return v.unsqueeze(1).repeat(1, batch_shape[0], *([1] * (v.ndim - 1))).reshape(-1, *v.shape[1:])return vdiff --git a/kornia/augmentation/presets/ada.py b/kornia/augmentation/presets/ada.pyindex 8aa73d038..efcd919de 100644--- a/kornia/augmentation/presets/ada.py+++ b/kornia/augmentation/presets/ada.py@@ -227,8 +227,8 @@ class AdaptiveDiscriminatorAugmentation(AugmentationSequential):def _sample_inputs(self, inputs: _inputs_type, data_keys: _data_keys_type, p_tensor: torch.Tensor) -> _inputs_type:if isinstance(inputs, dict):return {key: inputs[key][p_tensor] for key in data_keys}- else:- return inputs[p_tensor]++ return inputs[p_tensor]def _merge_inputs(self,diff --git a/pyproject.toml b/pyproject.tomlindex 72ca50714..b558fc579 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -213,6 +213,7 @@ select = ["PIE", # flake8-pie"PL", # Pylint"PYI", # flake8-pyi+ "RET506", # unnecessary branch after raise"RSE", # flake8-raise"RUF", # Ruff-specific rules"S", # flake8-bandit