Skip to content

Commit

Permalink
Update argument type hints where the default is set to None to use Op…
Browse files Browse the repository at this point in the history
…tional (#1495)

Signed-off-by: Otavio Napoli <[email protected]>
  • Loading branch information
otavioon authored Feb 5, 2024
1 parent 5b48fea commit e84f858
Show file tree
Hide file tree
Showing 20 changed files with 49 additions and 49 deletions.
2 changes: 1 addition & 1 deletion lightly/api/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions lightly/api/swagger_api_client.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions lightly/api/swagger_rest_client.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions lightly/data/collate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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__(
Expand Down Expand Up @@ -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__(
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down
6 changes: 3 additions & 3 deletions lightly/data/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions lightly/loss/msn_loss.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import math
import warnings
from typing import Union
from typing import Optional

import torch
import torch.distributed as dist
Expand Down Expand Up @@ -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__()
Expand Down
6 changes: 3 additions & 3 deletions lightly/models/modules/memory_bank.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions lightly/transforms/byol_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions lightly/transforms/dino_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion lightly/transforms/fast_siam_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
6 changes: 3 additions & 3 deletions lightly/transforms/gaussian_blur.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion lightly/transforms/mmcr_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions lightly/transforms/moco_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__(
Expand Down Expand Up @@ -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__(
Expand Down
4 changes: 2 additions & 2 deletions lightly/transforms/simclr_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions lightly/transforms/simsiam_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions lightly/transforms/swav_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions lightly/transforms/vicreg_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions lightly/transforms/wmse_transform.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Dict, List, Tuple, Union
from typing import Dict, List, Optional, Tuple

import torchvision.transforms as T

Expand Down Expand Up @@ -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,
):
Expand Down
6 changes: 3 additions & 3 deletions lightly/utils/benchmarking/knn_classifier.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Tuple, Union
from typing import Optional, Tuple

import torch
import torch.nn.functional as F
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions lightly/utils/hipify.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import copy
import warnings
from typing import Type, Union
from typing import Optional, Type, Union


class bcolors:
Expand All @@ -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"
Expand Down

0 comments on commit e84f858

Please sign in to comment.