Skip to content

Add missing None type hint to init functions #6354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions torchvision/datasets/_optical_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def __getitem__(self, index):
else:
return img1, img2, flow

def __len__(self):
def __len__(self) -> int:
return len(self._image_list)

def __rmul__(self, v):
Expand Down Expand Up @@ -118,7 +118,7 @@ class Sintel(FlowDataset):
return a built-in valid mask, such as :class:`~torchvision.datasets.KittiFlow`.
"""

def __init__(self, root, split="train", pass_name="clean", transforms=None):
def __init__(self, root, split="train", pass_name="clean", transforms=None) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well let's type this correctly?

Suggested change
def __init__(self, root, split="train", pass_name="clean", transforms=None) -> None:
def __init__(self, root: str, split: str = "train", pass_name: str ="clean", transforms: Optional[Callable] = None) -> None:

super().__init__(root=root, transforms=transforms)

verify_str_arg(split, "split", valid_values=("train", "test"))
Expand Down Expand Up @@ -180,7 +180,7 @@ class KittiFlow(FlowDataset):

_has_builtin_flow_mask = True

def __init__(self, root, split="train", transforms=None):
def __init__(self, root, split="train", transforms=None) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same flows for all comments

Suggested change
def __init__(self, root, split="train", transforms=None) -> None:
def __init__(self, root: str, split: str = "train", transforms: Optional[Callable] = None) -> None:

super().__init__(root=root, transforms=transforms)

verify_str_arg(split, "split", valid_values=("train", "test"))
Expand Down Expand Up @@ -245,7 +245,7 @@ class FlyingChairs(FlowDataset):
return a built-in valid mask, such as :class:`~torchvision.datasets.KittiFlow`.
"""

def __init__(self, root, split="train", transforms=None):
def __init__(self, root, split="train", transforms=None) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def __init__(self, root, split="train", transforms=None) -> None:
def __init__(self, root: str, split: str = "train", transforms: Optional[Callable] = None) -> None:

super().__init__(root=root, transforms=transforms)

verify_str_arg(split, "split", valid_values=("train", "val"))
Expand Down Expand Up @@ -316,7 +316,7 @@ class FlyingThings3D(FlowDataset):
return a built-in valid mask, such as :class:`~torchvision.datasets.KittiFlow`.
"""

def __init__(self, root, split="train", pass_name="clean", camera="left", transforms=None):
def __init__(self, root, split="train", pass_name="clean", camera="left", transforms=None) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def __init__(self, root, split="train", pass_name="clean", camera="left", transforms=None) -> None:
def __init__(self, root: str, split: str = "train", pass_name:str = "clean", camera: str = "left", transforms: Optional[Callable] = None) -> None:

super().__init__(root=root, transforms=transforms)

verify_str_arg(split, "split", valid_values=("train", "test"))
Expand Down Expand Up @@ -401,7 +401,7 @@ class HD1K(FlowDataset):

_has_builtin_flow_mask = True

def __init__(self, root, split="train", transforms=None):
def __init__(self, root, split="train", transforms=None) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def __init__(self, root, split="train", transforms=None) -> None:
def __init__(self, root: str, split: str = "train", transforms: Optional[Callable] = None) -> None:

super().__init__(root=root, transforms=transforms)

verify_str_arg(split, "split", valid_values=("train", "test"))
Expand Down
8 changes: 4 additions & 4 deletions torchvision/datasets/_stereo_matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class StereoMatchingDataset(ABC, VisionDataset):

_has_built_in_disparity_mask = False

def __init__(self, root: str, transforms: Optional[Callable] = None):
def __init__(self, root: str, transforms: Optional[Callable] = None) -> None:
"""
Args:
root(str): Root directory of the dataset.
Expand Down Expand Up @@ -152,7 +152,7 @@ class CarlaStereo(StereoMatchingDataset):
transforms (callable, optional): A function/transform that takes in a sample and returns a transformed version.
"""

def __init__(self, root: str, transforms: Optional[Callable] = None):
def __init__(self, root: str, transforms: Optional[Callable] = None) -> None:
super().__init__(root, transforms)

root = Path(root) / "carla-highres"
Expand Down Expand Up @@ -229,7 +229,7 @@ class Kitti2012Stereo(StereoMatchingDataset):

_has_built_in_disparity_mask = True

def __init__(self, root: str, split: str = "train", transforms: Optional[Callable] = None):
def __init__(self, root: str, split: str = "train", transforms: Optional[Callable] = None) -> None:
super().__init__(root, transforms)

verify_str_arg(split, "split", valid_values=("train", "test"))
Expand Down Expand Up @@ -317,7 +317,7 @@ class Kitti2015Stereo(StereoMatchingDataset):

_has_built_in_disparity_mask = True

def __init__(self, root: str, split: str = "train", transforms: Optional[Callable] = None):
def __init__(self, root: str, split: str = "train", transforms: Optional[Callable] = None) -> None:
super().__init__(root, transforms)

verify_str_arg(split, "split", valid_values=("train", "test"))
Expand Down
2 changes: 1 addition & 1 deletion torchvision/datasets/folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ def __init__(
target_transform: Optional[Callable] = None,
loader: Callable[[str], Any] = default_loader,
is_valid_file: Optional[Callable[[str], bool]] = None,
):
) -> None:
super().__init__(
root,
loader,
Expand Down
2 changes: 1 addition & 1 deletion torchvision/datasets/kitti.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def __init__(
target_transform: Optional[Callable] = None,
transforms: Optional[Callable] = None,
download: bool = False,
):
) -> None:
super().__init__(
root,
transform=transform,
Expand Down
6 changes: 3 additions & 3 deletions torchvision/datasets/lfw.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def __init__(
transform: Optional[Callable] = None,
target_transform: Optional[Callable] = None,
download: bool = False,
):
) -> None:
super().__init__(os.path.join(root, self.base_folder), transform=transform, target_transform=target_transform)

self.image_set = verify_str_arg(image_set.lower(), "image_set", self.file_dict.keys())
Expand Down Expand Up @@ -119,7 +119,7 @@ def __init__(
transform: Optional[Callable] = None,
target_transform: Optional[Callable] = None,
download: bool = False,
):
) -> None:
super().__init__(root, split, image_set, "people", transform, target_transform, download)

self.class_to_idx = self._get_classes()
Expand Down Expand Up @@ -201,7 +201,7 @@ def __init__(
transform: Optional[Callable] = None,
target_transform: Optional[Callable] = None,
download: bool = False,
):
) -> None:
super().__init__(root, split, image_set, "pairs", transform, target_transform, download)

self.pair_names, self.data, self.targets = self._get_pairs(self.images_dir)
Expand Down
2 changes: 1 addition & 1 deletion torchvision/datasets/oxford_iiit_pet.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def __init__(
transform: Optional[Callable] = None,
target_transform: Optional[Callable] = None,
download: bool = False,
):
) -> None:
self._split = verify_str_arg(split, "split", ("trainval", "test"))
if isinstance(target_types, str):
target_types = [target_types]
Expand Down
2 changes: 1 addition & 1 deletion torchvision/datasets/pcam.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def __init__(
transform: Optional[Callable] = None,
target_transform: Optional[Callable] = None,
download: bool = False,
):
) -> None:
try:
import h5py

Expand Down
2 changes: 1 addition & 1 deletion torchvision/datasets/voc.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def __init__(
transform: Optional[Callable] = None,
target_transform: Optional[Callable] = None,
transforms: Optional[Callable] = None,
):
) -> None:
super().__init__(root, transforms, transform, target_transform)
if year == "2007-test":
if image_set == "test":
Expand Down
2 changes: 1 addition & 1 deletion torchvision/models/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class WeightsEnum(StrEnum):
value (Weights): The data class entry with the weight information.
"""

def __init__(self, value: Weights):
def __init__(self, value: Weights) -> None:
self._value_ = value

@classmethod
Expand Down
2 changes: 1 addition & 1 deletion torchvision/models/detection/anchor_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def __init__(
scales: Optional[List[float]] = None,
steps: Optional[List[int]] = None,
clip: bool = True,
):
) -> None:
super().__init__()
if steps is not None and len(aspect_ratios) != len(steps):
raise ValueError("aspect_ratios and steps should have the same length")
Expand Down
8 changes: 4 additions & 4 deletions torchvision/models/detection/faster_rcnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def __init__(
box_positive_fraction=0.25,
bbox_reg_weights=None,
**kwargs,
):
) -> None:

if not hasattr(backbone, "out_channels"):
raise ValueError(
Expand Down Expand Up @@ -289,7 +289,7 @@ class TwoMLPHead(nn.Module):
representation_size (int): size of the intermediate representation
"""

def __init__(self, in_channels, representation_size):
def __init__(self, in_channels, representation_size) -> None:
super().__init__()

self.fc6 = nn.Linear(in_channels, representation_size)
Expand All @@ -311,7 +311,7 @@ def __init__(
conv_layers: List[int],
fc_layers: List[int],
norm_layer: Optional[Callable[..., nn.Module]] = None,
):
) -> None:
"""
Args:
input_size (Tuple[int, int, int]): the input size in CHW format.
Expand Down Expand Up @@ -351,7 +351,7 @@ class FastRCNNPredictor(nn.Module):
num_classes (int): number of output classes (including background)
"""

def __init__(self, in_channels, num_classes):
def __init__(self, in_channels, num_classes) -> None:
super().__init__()
self.cls_score = nn.Linear(in_channels, num_classes)
self.bbox_pred = nn.Linear(in_channels, num_classes * 4)
Expand Down
4 changes: 2 additions & 2 deletions torchvision/models/detection/fcos.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def __init__(
num_anchors: int,
num_convs: int = 4,
norm_layer: Optional[Callable[..., nn.Module]] = None,
):
) -> None:
super().__init__()

if norm_layer is None:
Expand Down Expand Up @@ -376,7 +376,7 @@ def __init__(
detections_per_img: int = 100,
topk_candidates: int = 1000,
**kwargs,
):
) -> None:
super().__init__()
_log_api_usage_once(self)

Expand Down
6 changes: 3 additions & 3 deletions torchvision/models/detection/keypoint_rcnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def __init__(
keypoint_predictor=None,
num_keypoints=None,
**kwargs,
):
) -> None:

if not isinstance(keypoint_roi_pool, (MultiScaleRoIAlign, type(None))):
raise TypeError(
Expand Down Expand Up @@ -269,7 +269,7 @@ def __init__(


class KeypointRCNNHeads(nn.Sequential):
def __init__(self, in_channels, layers):
def __init__(self, in_channels, layers) -> None:
d = []
next_feature = in_channels
for out_channels in layers:
Expand All @@ -284,7 +284,7 @@ def __init__(self, in_channels, layers):


class KeypointRCNNPredictor(nn.Module):
def __init__(self, in_channels, num_keypoints):
def __init__(self, in_channels, num_keypoints) -> None:
super().__init__()
input_features = in_channels
deconv_kernel = 4
Expand Down
6 changes: 3 additions & 3 deletions torchvision/models/detection/mask_rcnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def __init__(
mask_head=None,
mask_predictor=None,
**kwargs,
):
) -> None:

if not isinstance(mask_roi_pool, (MultiScaleRoIAlign, type(None))):
raise TypeError(
Expand Down Expand Up @@ -268,7 +268,7 @@ def __init__(
class MaskRCNNHeads(nn.Sequential):
_version = 2

def __init__(self, in_channels, layers, dilation, norm_layer: Optional[Callable[..., nn.Module]] = None):
def __init__(self, in_channels, layers, dilation, norm_layer: Optional[Callable[..., nn.Module]] = None) -> None:
"""
Args:
in_channels (int): number of input channels
Expand Down Expand Up @@ -332,7 +332,7 @@ def _load_from_state_dict(


class MaskRCNNPredictor(nn.Sequential):
def __init__(self, in_channels, dim_reduced, num_classes):
def __init__(self, in_channels, dim_reduced, num_classes) -> None:
super().__init__(
OrderedDict(
[
Expand Down
14 changes: 10 additions & 4 deletions torchvision/models/detection/retinanet.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ class RetinaNetHead(nn.Module):
norm_layer (callable, optional): Module specifying the normalization layer to use. Default: None
"""

def __init__(self, in_channels, num_anchors, num_classes, norm_layer: Optional[Callable[..., nn.Module]] = None):
def __init__(
self,
in_channels,
num_anchors,
num_classes,
norm_layer: Optional[Callable[..., nn.Module]] = None,
) -> None:
super().__init__()
self.classification_head = RetinaNetClassificationHead(
in_channels, num_anchors, num_classes, norm_layer=norm_layer
Expand Down Expand Up @@ -104,7 +110,7 @@ def __init__(
num_classes,
prior_probability=0.01,
norm_layer: Optional[Callable[..., nn.Module]] = None,
):
) -> None:
super().__init__()

conv = []
Expand Down Expand Up @@ -223,7 +229,7 @@ class RetinaNetRegressionHead(nn.Module):
"box_coder": det_utils.BoxCoder,
}

def __init__(self, in_channels, num_anchors, norm_layer: Optional[Callable[..., nn.Module]] = None):
def __init__(self, in_channels, num_anchors, norm_layer: Optional[Callable[..., nn.Module]] = None) -> None:
super().__init__()

conv = []
Expand Down Expand Up @@ -430,7 +436,7 @@ def __init__(
bg_iou_thresh=0.4,
topk_candidates=1000,
**kwargs,
):
) -> None:
super().__init__()
_log_api_usage_once(self)

Expand Down
2 changes: 1 addition & 1 deletion torchvision/models/detection/roi_heads.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ def __init__(
keypoint_roi_pool=None,
keypoint_head=None,
keypoint_predictor=None,
):
) -> None:
super().__init__()

self.box_similarity = box_ops.box_iou
Expand Down
12 changes: 6 additions & 6 deletions torchvision/models/detection/ssd.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def _xavier_init(conv: nn.Module):


class SSDHead(nn.Module):
def __init__(self, in_channels: List[int], num_anchors: List[int], num_classes: int):
def __init__(self, in_channels: List[int], num_anchors: List[int], num_classes: int) -> None:
super().__init__()
self.classification_head = SSDClassificationHead(in_channels, num_anchors, num_classes)
self.regression_head = SSDRegressionHead(in_channels, num_anchors)
Expand All @@ -67,7 +67,7 @@ def forward(self, x: List[Tensor]) -> Dict[str, Tensor]:


class SSDScoringHead(nn.Module):
def __init__(self, module_list: nn.ModuleList, num_columns: int):
def __init__(self, module_list: nn.ModuleList, num_columns: int) -> None:
super().__init__()
self.module_list = module_list
self.num_columns = num_columns
Expand Down Expand Up @@ -104,7 +104,7 @@ def forward(self, x: List[Tensor]) -> Tensor:


class SSDClassificationHead(SSDScoringHead):
def __init__(self, in_channels: List[int], num_anchors: List[int], num_classes: int):
def __init__(self, in_channels: List[int], num_anchors: List[int], num_classes: int) -> None:
cls_logits = nn.ModuleList()
for channels, anchors in zip(in_channels, num_anchors):
cls_logits.append(nn.Conv2d(channels, num_classes * anchors, kernel_size=3, padding=1))
Expand All @@ -113,7 +113,7 @@ def __init__(self, in_channels: List[int], num_anchors: List[int], num_classes:


class SSDRegressionHead(SSDScoringHead):
def __init__(self, in_channels: List[int], num_anchors: List[int]):
def __init__(self, in_channels: List[int], num_anchors: List[int]) -> None:
bbox_reg = nn.ModuleList()
for channels, anchors in zip(in_channels, num_anchors):
bbox_reg.append(nn.Conv2d(channels, 4 * anchors, kernel_size=3, padding=1))
Expand Down Expand Up @@ -197,7 +197,7 @@ def __init__(
topk_candidates: int = 400,
positive_fraction: float = 0.25,
**kwargs: Any,
):
) -> None:
super().__init__()
_log_api_usage_once(self)

Expand Down Expand Up @@ -462,7 +462,7 @@ def postprocess_detections(


class SSDFeatureExtractorVGG(nn.Module):
def __init__(self, backbone: nn.Module, highres: bool):
def __init__(self, backbone: nn.Module, highres: bool) -> None:
super().__init__()

_, _, maxpool3_pos, maxpool4_pos, _ = (i for i, layer in enumerate(backbone) if isinstance(layer, nn.MaxPool2d))
Expand Down
Loading