From e84f85896bcaedeb1ff63b9c4bcf7c9d2885bcdf Mon Sep 17 00:00:00 2001 From: otavioon Date: Mon, 5 Feb 2024 12:16:57 -0300 Subject: [PATCH] Update argument type hints where the default is set to None to use Optional (#1495) Signed-off-by: Otavio Napoli --- lightly/api/download.py | 2 +- lightly/api/swagger_api_client.py | 8 ++++---- lightly/api/swagger_rest_client.py | 4 ++-- lightly/data/collate.py | 12 ++++++------ lightly/data/dataset.py | 6 +++--- lightly/loss/msn_loss.py | 4 ++-- lightly/models/modules/memory_bank.py | 6 +++--- lightly/transforms/byol_transform.py | 8 ++++---- lightly/transforms/dino_transform.py | 4 ++-- lightly/transforms/fast_siam_transform.py | 2 +- lightly/transforms/gaussian_blur.py | 6 +++--- lightly/transforms/mmcr_transform.py | 2 +- lightly/transforms/moco_transform.py | 4 ++-- lightly/transforms/simclr_transform.py | 4 ++-- lightly/transforms/simsiam_transform.py | 4 ++-- lightly/transforms/swav_transform.py | 4 ++-- lightly/transforms/vicreg_transform.py | 4 ++-- lightly/transforms/wmse_transform.py | 4 ++-- lightly/utils/benchmarking/knn_classifier.py | 6 +++--- lightly/utils/hipify.py | 4 ++-- 20 files changed, 49 insertions(+), 49 deletions(-) diff --git a/lightly/api/download.py b/lightly/api/download.py index 924dc7214..88b8d2a33 100644 --- a/lightly/api/download.py +++ b/lightly/api/download.py @@ -72,7 +72,7 @@ def load_image(url, req, request_kwargs): def download_all_video_frames( url: str, - timestamp: Union[int, None] = None, + timestamp: Optional[int] = None, as_pil_image: int = True, thread_type: av.codec.context.ThreadType = av.codec.context.ThreadType.AUTO, video_channel: int = 0, diff --git a/lightly/api/swagger_api_client.py b/lightly/api/swagger_api_client.py index 4414d64d9..750843094 100644 --- a/lightly/api/swagger_api_client.py +++ b/lightly/api/swagger_api_client.py @@ -1,4 +1,4 @@ -from typing import Any, Dict, Tuple, Union +from typing import Any, Dict, Optional, Tuple, Union from lightly.api.swagger_rest_client import LightlySwaggerRESTClientObject from lightly.openapi_generated.swagger_client.api_client import ApiClient, Configuration @@ -49,9 +49,9 @@ def __init__( self, configuration: Configuration, timeout: Union[None, int, Tuple[int, int]] = DEFAULT_API_TIMEOUT, - header_name: Union[str, None] = None, - header_value: Union[str, None] = None, - cookie: Union[str, None] = None, + header_name: Optional[str] = None, + header_value: Optional[str] = None, + cookie: Optional[str] = None, ): super().__init__( configuration=configuration, diff --git a/lightly/api/swagger_rest_client.py b/lightly/api/swagger_rest_client.py index 4bd3cd399..31f40cc61 100644 --- a/lightly/api/swagger_rest_client.py +++ b/lightly/api/swagger_rest_client.py @@ -1,4 +1,4 @@ -from typing import Any, Dict, Tuple, Union +from typing import Any, Dict, Optional, Tuple, Union from lightly.openapi_generated.swagger_client.api_client import Configuration from lightly.openapi_generated.swagger_client.rest import RESTClientObject @@ -29,7 +29,7 @@ def __init__( configuration: Configuration, timeout: Union[None, int, Tuple[int, int]], pools_size: int = 4, - maxsize: Union[None, int] = None, + maxsize: Optional[int] = None, ): # Save args as attributes to make the class picklable. self.configuration = configuration diff --git a/lightly/data/collate.py b/lightly/data/collate.py index 3a935951f..04eb574c7 100644 --- a/lightly/data/collate.py +++ b/lightly/data/collate.py @@ -159,7 +159,7 @@ def __init__( vf_prob: float = 0.0, hf_prob: float = 0.5, rr_prob: float = 0.0, - rr_degrees: Union[None, float, Tuple[float, float]] = None, + rr_degrees: Optional[Union[float, Tuple[float, float]]] = None, normalize: dict = imagenet_normalize, ): if isinstance(input_size, tuple): @@ -289,7 +289,7 @@ def __init__( vf_prob: float = 0.0, hf_prob: float = 0.5, rr_prob: float = 0.0, - rr_degrees: Union[None, float, Tuple[float, float]] = None, + rr_degrees: Optional[Union[float, Tuple[float, float]]] = None, normalize: dict = imagenet_normalize, ): super(SimCLRCollateFunction, self).__init__( @@ -376,7 +376,7 @@ def __init__( vf_prob: float = 0.0, hf_prob: float = 0.5, rr_prob: float = 0.0, - rr_degrees: Union[None, float, Tuple[float, float]] = None, + rr_degrees: Optional[Union[float, Tuple[float, float]]] = None, normalize: dict = imagenet_normalize, ): super(MoCoCollateFunction, self).__init__( @@ -523,7 +523,7 @@ def __init__( hf_prob: float = 0.5, vf_prob: float = 0.0, rr_prob: float = 0.0, - rr_degrees: Union[None, float, Tuple[float, float]] = None, + rr_degrees: Optional[Union[float, Tuple[float, float]]] = None, cj_prob: float = 0.8, cj_strength: float = 0.8, random_gray_scale: float = 0.2, @@ -638,7 +638,7 @@ def __init__( hf_prob=0.5, vf_prob=0, rr_prob=0, - rr_degrees: Union[None, float, Tuple[float, float]] = None, + rr_degrees: Optional[Union[float, Tuple[float, float]]] = None, cj_prob=0.8, cj_bright=0.4, cj_contrast=0.4, @@ -1148,7 +1148,7 @@ def __init__( vf_prob: float = 0.0, hf_prob: float = 0.5, rr_prob: float = 0.0, - rr_degrees: Union[None, float, Tuple[float, float]] = None, + rr_degrees: Optional[Union[float, Tuple[float, float]]] = None, normalize: dict = imagenet_normalize, ): if isinstance(input_size, tuple): diff --git a/lightly/data/dataset.py b/lightly/data/dataset.py index da7fd24b1..005da8766 100644 --- a/lightly/data/dataset.py +++ b/lightly/data/dataset.py @@ -5,7 +5,7 @@ import os import shutil import tempfile -from typing import Any, Callable, Dict, List, Union +from typing import Any, Callable, Dict, List, Optional, Union import torchvision.datasets as datasets from PIL import Image @@ -254,8 +254,8 @@ def get_filenames(self) -> List[str]: def dump( self, output_dir: str, - filenames: Union[List[str], None] = None, - format: Union[str, None] = None, + filenames: Optional[List[str]] = None, + format: Optional[str] = None, ): """Saves images in the dataset to the output directory. diff --git a/lightly/loss/msn_loss.py b/lightly/loss/msn_loss.py index 47e05f6db..744714304 100644 --- a/lightly/loss/msn_loss.py +++ b/lightly/loss/msn_loss.py @@ -1,6 +1,6 @@ import math import warnings -from typing import Union +from typing import Optional import torch import torch.distributed as dist @@ -152,7 +152,7 @@ def __init__( temperature: float = 0.1, sinkhorn_iterations: int = 3, regularization_weight: float = 1.0, - me_max_weight: Union[float, None] = None, + me_max_weight: Optional[float] = None, gather_distributed: bool = False, ): super().__init__() diff --git a/lightly/models/modules/memory_bank.py b/lightly/models/modules/memory_bank.py index db7139b8c..aa126a46f 100644 --- a/lightly/models/modules/memory_bank.py +++ b/lightly/models/modules/memory_bank.py @@ -4,7 +4,7 @@ # All Rights Reserved import warnings -from typing import Sequence, Tuple, Union +from typing import Optional, Sequence, Tuple, Union import torch from torch import Tensor @@ -44,7 +44,7 @@ class MemoryBankModule(Module): >>> def __init__(self, memory_bank_size: Tuple[int, int] = (2 ** 16, 128)): >>> super().__init__(memory_bank_size) >>> - >>> def forward(self, output: Tensor, labels: Union[Tensor, None] = None): + >>> def forward(self, output: Tensor, labels: Optional[Tensor] = None): >>> output, negatives = super().forward(output) >>> >>> if negatives is not None: @@ -134,7 +134,7 @@ def _dequeue_and_enqueue(self, batch: Tensor) -> None: def forward( self, output: Tensor, - labels: Union[Tensor, None] = None, + labels: Optional[Tensor] = None, update: bool = False, ) -> Tuple[Tensor, Union[Tensor, None]]: """Query memory bank for additional negative samples diff --git a/lightly/transforms/byol_transform.py b/lightly/transforms/byol_transform.py index 26d9f1360..c728cd0eb 100644 --- a/lightly/transforms/byol_transform.py +++ b/lightly/transforms/byol_transform.py @@ -30,7 +30,7 @@ def __init__( vf_prob: float = 0.0, hf_prob: float = 0.5, rr_prob: float = 0.0, - rr_degrees: Union[None, float, Tuple[float, float]] = None, + rr_degrees: Optional[Union[float, Tuple[float, float]]] = None, normalize: Union[None, Dict[str, List[float]]] = IMAGENET_NORMALIZE, ): color_jitter = T.ColorJitter( @@ -90,7 +90,7 @@ def __init__( vf_prob: float = 0.0, hf_prob: float = 0.5, rr_prob: float = 0.0, - rr_degrees: Union[None, float, Tuple[float, float]] = None, + rr_degrees: Optional[Union[float, Tuple[float, float]]] = None, normalize: Union[None, Dict[str, List[float]]] = IMAGENET_NORMALIZE, ): color_jitter = T.ColorJitter( @@ -168,8 +168,8 @@ class BYOLTransform(MultiViewTransform): def __init__( self, - view_1_transform: Union[BYOLView1Transform, None] = None, - view_2_transform: Union[BYOLView2Transform, None] = None, + view_1_transform: Optional[BYOLView1Transform] = None, + view_2_transform: Optional[BYOLView2Transform] = None, ): # We need to initialize the transforms here view_1_transform = view_1_transform or BYOLView1Transform() diff --git a/lightly/transforms/dino_transform.py b/lightly/transforms/dino_transform.py index 38e719fbd..b88624890 100644 --- a/lightly/transforms/dino_transform.py +++ b/lightly/transforms/dino_transform.py @@ -104,7 +104,7 @@ def __init__( hf_prob: float = 0.5, vf_prob: float = 0, rr_prob: float = 0, - rr_degrees: Union[None, float, Tuple[float, float]] = None, + rr_degrees: Optional[Union[float, Tuple[float, float]]] = None, cj_prob: float = 0.8, cj_strength: float = 0.5, cj_bright: float = 0.8, @@ -200,7 +200,7 @@ def __init__( hf_prob: float = 0.5, vf_prob: float = 0, rr_prob: float = 0, - rr_degrees: Union[None, float, Tuple[float, float]] = None, + rr_degrees: Optional[Union[float, Tuple[float, float]]] = None, cj_prob: float = 0.8, cj_strength: float = 0.5, cj_bright: float = 0.8, diff --git a/lightly/transforms/fast_siam_transform.py b/lightly/transforms/fast_siam_transform.py index c70b77de1..4da4c0fa6 100644 --- a/lightly/transforms/fast_siam_transform.py +++ b/lightly/transforms/fast_siam_transform.py @@ -88,7 +88,7 @@ def __init__( vf_prob: float = 0.0, hf_prob: float = 0.5, rr_prob: float = 0.0, - rr_degrees: Union[None, float, Tuple[float, float]] = None, + rr_degrees: Optional[Union[float, Tuple[float, float]]] = None, normalize: Union[None, Dict[str, List[float]]] = IMAGENET_NORMALIZE, ): transforms = [ diff --git a/lightly/transforms/gaussian_blur.py b/lightly/transforms/gaussian_blur.py index 3f8f9e2d7..cbcabc52d 100644 --- a/lightly/transforms/gaussian_blur.py +++ b/lightly/transforms/gaussian_blur.py @@ -1,7 +1,7 @@ # Copyright (c) 2020. Lightly AG and its affiliates. # All Rights Reserved -from typing import Tuple, Union +from typing import Optional, Tuple from warnings import warn import numpy as np @@ -34,9 +34,9 @@ class GaussianBlur: def __init__( self, - kernel_size: Union[float, None] = None, + kernel_size: Optional[float] = None, prob: float = 0.5, - scale: Union[float, None] = None, + scale: Optional[float] = None, sigmas: Tuple[float, float] = (0.2, 2), ): if scale != None or kernel_size != None: diff --git a/lightly/transforms/mmcr_transform.py b/lightly/transforms/mmcr_transform.py index 4d47a3e23..de156e7fb 100644 --- a/lightly/transforms/mmcr_transform.py +++ b/lightly/transforms/mmcr_transform.py @@ -61,7 +61,7 @@ def __init__( vf_prob: float = 0.0, hf_prob: float = 0.5, rr_prob: float = 0.0, - rr_degrees: Union[None, float, Tuple[float, float]] = None, + rr_degrees: Optional[Union[float, Tuple[float, float]]] = None, normalize: Union[None, Dict[str, List[float]]] = IMAGENET_NORMALIZE, ): if k < 1: diff --git a/lightly/transforms/moco_transform.py b/lightly/transforms/moco_transform.py index e6f06f300..0ff2252e6 100644 --- a/lightly/transforms/moco_transform.py +++ b/lightly/transforms/moco_transform.py @@ -82,7 +82,7 @@ def __init__( vf_prob: float = 0.0, hf_prob: float = 0.5, rr_prob: float = 0.0, - rr_degrees: Union[None, float, Tuple[float, float]] = None, + rr_degrees: Optional[Union[float, Tuple[float, float]]] = None, normalize: Union[None, Dict[str, List[float]]] = IMAGENET_NORMALIZE, ): super().__init__( @@ -191,7 +191,7 @@ def __init__( vf_prob: float = 0.0, hf_prob: float = 0.5, rr_prob: float = 0.0, - rr_degrees: Union[None, float, Tuple[float, float]] = None, + rr_degrees: Optional[Union[float, Tuple[float, float]]] = None, normalize: Union[None, Dict[str, List[float]]] = IMAGENET_NORMALIZE, ): super().__init__( diff --git a/lightly/transforms/simclr_transform.py b/lightly/transforms/simclr_transform.py index d975b0771..6fd5e7a30 100644 --- a/lightly/transforms/simclr_transform.py +++ b/lightly/transforms/simclr_transform.py @@ -101,7 +101,7 @@ def __init__( vf_prob: float = 0.0, hf_prob: float = 0.5, rr_prob: float = 0.0, - rr_degrees: Union[None, float, Tuple[float, float]] = None, + rr_degrees: Optional[Union[float, Tuple[float, float]]] = None, normalize: Union[None, Dict[str, List[float]]] = IMAGENET_NORMALIZE, ): view_transform = SimCLRViewTransform( @@ -144,7 +144,7 @@ def __init__( vf_prob: float = 0.0, hf_prob: float = 0.5, rr_prob: float = 0.0, - rr_degrees: Union[None, float, Tuple[float, float]] = None, + rr_degrees: Optional[Union[float, Tuple[float, float]]] = None, normalize: Union[None, Dict[str, List[float]]] = IMAGENET_NORMALIZE, ): color_jitter = T.ColorJitter( diff --git a/lightly/transforms/simsiam_transform.py b/lightly/transforms/simsiam_transform.py index 4f2607480..5121ffc1a 100644 --- a/lightly/transforms/simsiam_transform.py +++ b/lightly/transforms/simsiam_transform.py @@ -90,7 +90,7 @@ def __init__( vf_prob: float = 0.0, hf_prob: float = 0.5, rr_prob: float = 0.0, - rr_degrees: Union[None, float, Tuple[float, float]] = None, + rr_degrees: Optional[Union[float, Tuple[float, float]]] = None, normalize: Union[None, Dict[str, List[float]]] = IMAGENET_NORMALIZE, ): view_transform = SimSiamViewTransform( @@ -133,7 +133,7 @@ def __init__( vf_prob: float = 0.0, hf_prob: float = 0.5, rr_prob: float = 0.0, - rr_degrees: Union[None, float, Tuple[float, float]] = None, + rr_degrees: Optional[Union[float, Tuple[float, float]]] = None, normalize: Union[None, Dict[str, List[float]]] = IMAGENET_NORMALIZE, ): color_jitter = T.ColorJitter( diff --git a/lightly/transforms/swav_transform.py b/lightly/transforms/swav_transform.py index f7000f945..a34c73647 100644 --- a/lightly/transforms/swav_transform.py +++ b/lightly/transforms/swav_transform.py @@ -85,7 +85,7 @@ def __init__( hf_prob: float = 0.5, vf_prob: float = 0.0, rr_prob: float = 0.0, - rr_degrees: Union[None, float, Tuple[float, float]] = None, + rr_degrees: Optional[Union[float, Tuple[float, float]]] = None, cj_prob: float = 0.8, cj_strength: float = 1.0, cj_bright: float = 0.8, @@ -131,7 +131,7 @@ def __init__( hf_prob: float = 0.5, vf_prob: float = 0.0, rr_prob: float = 0.0, - rr_degrees: Union[None, float, Tuple[float, float]] = None, + rr_degrees: Optional[Union[float, Tuple[float, float]]] = None, cj_prob: float = 0.8, cj_strength: float = 1.0, cj_bright: float = 0.8, diff --git a/lightly/transforms/vicreg_transform.py b/lightly/transforms/vicreg_transform.py index c7cc0b270..1ebe6e04e 100644 --- a/lightly/transforms/vicreg_transform.py +++ b/lightly/transforms/vicreg_transform.py @@ -96,7 +96,7 @@ def __init__( vf_prob: float = 0.0, hf_prob: float = 0.5, rr_prob: float = 0.0, - rr_degrees: Union[None, float, Tuple[float, float]] = None, + rr_degrees: Optional[Union[float, Tuple[float, float]]] = None, normalize: Union[None, Dict[str, List[float]]] = IMAGENET_NORMALIZE, ): view_transform = VICRegViewTransform( @@ -141,7 +141,7 @@ def __init__( vf_prob: float = 0.0, hf_prob: float = 0.5, rr_prob: float = 0.0, - rr_degrees: Union[None, float, Tuple[float, float]] = None, + rr_degrees: Optional[Union[float, Tuple[float, float]]] = None, normalize: Union[None, Dict[str, List[float]]] = IMAGENET_NORMALIZE, ): color_jitter = T.ColorJitter( diff --git a/lightly/transforms/wmse_transform.py b/lightly/transforms/wmse_transform.py index 87403133f..2d7d2d6dc 100644 --- a/lightly/transforms/wmse_transform.py +++ b/lightly/transforms/wmse_transform.py @@ -1,4 +1,4 @@ -from typing import Dict, List, Tuple, Union +from typing import Dict, List, Optional, Tuple import torchvision.transforms as T @@ -78,7 +78,7 @@ def __init__( random_gray_scale: float = 0.1, hf_prob: float = 0.5, gaussian_blur: float = 0.5, - kernel_size: Union[int, None] = None, + kernel_size: Optional[int] = None, sigmas: Tuple[float, float] = (0.1, 2.0), normalize: Dict[str, List[float]] = IMAGENET_NORMALIZE, ): diff --git a/lightly/utils/benchmarking/knn_classifier.py b/lightly/utils/benchmarking/knn_classifier.py index 93298611c..62049878a 100644 --- a/lightly/utils/benchmarking/knn_classifier.py +++ b/lightly/utils/benchmarking/knn_classifier.py @@ -1,4 +1,4 @@ -from typing import Tuple, Union +from typing import Optional, Tuple import torch import torch.nn.functional as F @@ -97,8 +97,8 @@ def __init__( self._train_features = [] self._train_targets = [] - self._train_features_tensor: Union[Tensor, None] = None - self._train_targets_tensor: Union[Tensor, None] = None + self._train_features_tensor: Optional[Tensor] = None + self._train_targets_tensor: Optional[Tensor] = None @torch.no_grad() def training_step(self, batch, batch_idx) -> None: diff --git a/lightly/utils/hipify.py b/lightly/utils/hipify.py index 44dc32aed..760ed0407 100644 --- a/lightly/utils/hipify.py +++ b/lightly/utils/hipify.py @@ -1,6 +1,6 @@ import copy import warnings -from typing import Type, Union +from typing import Optional, Type, Union class bcolors: @@ -19,7 +19,7 @@ def _custom_formatwarning( category: Type[Warning], filename: str, lineno: int, - line: Union[str, None] = None, + line: Optional[str] = None, ) -> str: # ignore everything except the message return f"{bcolors.WARNING}{message}{bcolors.WARNING}\n"