From 5c22b7a0f3ee9f64c85357fc11c995284732739d Mon Sep 17 00:00:00 2001 From: frgfm Date: Thu, 29 Jul 2021 17:50:37 +0200 Subject: [PATCH 1/2] style: Added typing annotations --- torchvision/io/__init__.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/torchvision/io/__init__.py b/torchvision/io/__init__.py index 742344e6b0f..9db7160e6e9 100644 --- a/torchvision/io/__init__.py +++ b/torchvision/io/__init__.py @@ -1,4 +1,5 @@ import torch +from typing import Any, Dict from ._video_opt import ( Timebase, @@ -33,13 +34,13 @@ if _HAS_VIDEO_OPT: - def _has_video_opt(): + def _has_video_opt() -> bool: return True else: - def _has_video_opt(): + def _has_video_opt() -> bool: return False @@ -99,7 +100,7 @@ class VideoReader: Currently available options include ``['video', 'audio']`` """ - def __init__(self, path, stream="video"): + def __init__(self, path: str, stream: str = "video") -> None: if not _has_video_opt(): raise RuntimeError( "Not compiled with video_reader support, " @@ -109,7 +110,7 @@ def __init__(self, path, stream="video"): ) self._c = torch.classes.torchvision.Video(path, stream) - def __next__(self): + def __next__(self) -> Dict[str, Any]: """Decodes and returns the next frame of the current stream. Frames are encoded as a dict with mandatory data and pts fields, where data is a tensor, and pts is a @@ -126,10 +127,10 @@ def __next__(self): raise StopIteration return {"data": frame, "pts": pts} - def __iter__(self): + def __iter__(self) -> Any: return self - def seek(self, time_s: float): + def seek(self, time_s: float) -> Any: """Seek within current stream. Args: @@ -144,7 +145,7 @@ def seek(self, time_s: float): self._c.seek(time_s) return self - def get_metadata(self): + def get_metadata(self) -> Dict[str, Any]: """Returns video metadata Returns: @@ -152,7 +153,7 @@ def get_metadata(self): """ return self._c.get_metadata() - def set_current_stream(self, stream: str): + def set_current_stream(self, stream: str) -> bool: """Set current stream. Explicitly define the stream we are operating on. From 5e50c9d68688439d55fa8987b067ef28a5816d3e Mon Sep 17 00:00:00 2001 From: Vasilis Vryniotis Date: Mon, 16 Aug 2021 10:29:37 +0100 Subject: [PATCH 2/2] Specified types for iter and seek. --- torchvision/io/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/torchvision/io/__init__.py b/torchvision/io/__init__.py index 9db7160e6e9..d0ec1b406f3 100644 --- a/torchvision/io/__init__.py +++ b/torchvision/io/__init__.py @@ -1,5 +1,5 @@ import torch -from typing import Any, Dict +from typing import Any, Dict, Iterator from ._video_opt import ( Timebase, @@ -127,10 +127,10 @@ def __next__(self) -> Dict[str, Any]: raise StopIteration return {"data": frame, "pts": pts} - def __iter__(self) -> Any: + def __iter__(self) -> Iterator['VideoReader']: return self - def seek(self, time_s: float) -> Any: + def seek(self, time_s: float) -> 'VideoReader': """Seek within current stream. Args: