From 3b5e4606b67314c8cb15d9b188d7c08a9ad18123 Mon Sep 17 00:00:00 2001
From: Zaida Zhou <58739961+zhouzaida@users.noreply.github.com>
Date: Tue, 15 Feb 2022 20:39:43 +0800
Subject: [PATCH] [Fix] Fix the type hint of fileio module (#20)

---
 mmengine/utils/__init__.py           |  1 -
 mmengine/utils/fileio/__init__.py    |  1 -
 mmengine/utils/fileio/file_client.py | 30 ++++++++++++++++++----------
 mmengine/utils/fileio/io.py          |  1 -
 mmengine/utils/fileio/parse.py       |  1 -
 5 files changed, 20 insertions(+), 14 deletions(-)

diff --git a/mmengine/utils/__init__.py b/mmengine/utils/__init__.py
index b830b224..e709a4ef 100644
--- a/mmengine/utils/__init__.py
+++ b/mmengine/utils/__init__.py
@@ -1,5 +1,4 @@
 # Copyright (c) OpenMMLab. All rights reserved.
-# type: ignore
 from .fileio import (FileClient, dict_from_file, dump, list_from_file, load,
                      register_handler)
 from .misc import (check_prerequisites, concat_list, deprecated_api_warning,
diff --git a/mmengine/utils/fileio/__init__.py b/mmengine/utils/fileio/__init__.py
index 77c35a1b..2051b85f 100644
--- a/mmengine/utils/fileio/__init__.py
+++ b/mmengine/utils/fileio/__init__.py
@@ -1,5 +1,4 @@
 # Copyright (c) OpenMMLab. All rights reserved.
-# type: ignore
 from .file_client import BaseStorageBackend, FileClient
 from .handlers import BaseFileHandler, JsonHandler, PickleHandler, YamlHandler
 from .io import dump, load, register_handler
diff --git a/mmengine/utils/fileio/file_client.py b/mmengine/utils/fileio/file_client.py
index af2e5f09..afc19bb7 100644
--- a/mmengine/utils/fileio/file_client.py
+++ b/mmengine/utils/fileio/file_client.py
@@ -1,5 +1,4 @@
 # Copyright (c) OpenMMLab. All rights reserved.
-# type: ignore
 import inspect
 import os
 import os.path as osp
@@ -9,7 +8,7 @@ import warnings
 from abc import ABCMeta, abstractmethod
 from contextlib import contextmanager
 from pathlib import Path
-from typing import Iterable, Iterator, Optional, Tuple, Union
+from typing import Any, Generator, Iterator, Optional, Tuple, Union
 from urllib.request import urlopen
 
 import mmengine
@@ -298,7 +297,10 @@ class PetrelBackend(BaseStorageBackend):
         return '/'.join(formatted_paths)
 
     @contextmanager
-    def get_local_path(self, filepath: Union[str, Path]) -> Iterable[str]:
+    def get_local_path(
+            self,
+            filepath: Union[str,
+                            Path]) -> Generator[Union[str, Path], None, None]:
         """Download a file from ``filepath`` and return a temporary path.
 
         ``get_local_path`` is decorated by :meth:`contxtlib.contextmanager`. It
@@ -632,7 +634,9 @@ class HardDiskBackend(BaseStorageBackend):
 
     @contextmanager
     def get_local_path(
-            self, filepath: Union[str, Path]) -> Iterable[Union[str, Path]]:
+            self,
+            filepath: Union[str,
+                            Path]) -> Generator[Union[str, Path], None, None]:
         """Only for unified API and do nothing."""
         yield filepath
 
@@ -701,7 +705,8 @@ class HTTPBackend(BaseStorageBackend):
         return value_buf.decode(encoding)
 
     @contextmanager
-    def get_local_path(self, filepath: str) -> Iterable[str]:
+    def get_local_path(
+            self, filepath: str) -> Generator[Union[str, Path], None, None]:
         """Download a file from ``filepath``.
 
         ``get_local_path`` is decorated by :meth:`contxtlib.contextmanager`. It
@@ -775,15 +780,17 @@ class FileClient:
     # backend appears in the collection, the singleton pattern is disabled for
     # that backend, because if the singleton pattern is used, then the object
     # returned will be the backend before overwriting
-    _overridden_backends = set()
-    _prefix_to_backends = {
+    _overridden_backends: set = set()
+    _prefix_to_backends: dict = {
         's3': PetrelBackend,
         'http': HTTPBackend,
         'https': HTTPBackend,
     }
-    _overridden_prefixes = set()
+    _overridden_prefixes: set = set()
 
-    _instances = {}
+    _instances: dict = {}
+
+    client: Any
 
     def __new__(cls, backend=None, prefix=None, **kwargs):
         if backend is None and prefix is None:
@@ -1093,7 +1100,10 @@ class FileClient:
         return self.client.join_path(filepath, *filepaths)
 
     @contextmanager
-    def get_local_path(self, filepath: Union[str, Path]) -> Iterable[str]:
+    def get_local_path(
+            self,
+            filepath: Union[str,
+                            Path]) -> Generator[Union[str, Path], None, None]:
         """Download data from ``filepath`` and write the data to local path.
 
         ``get_local_path`` is decorated by :meth:`contxtlib.contextmanager`. It
diff --git a/mmengine/utils/fileio/io.py b/mmengine/utils/fileio/io.py
index e2226efd..fbfa93d3 100644
--- a/mmengine/utils/fileio/io.py
+++ b/mmengine/utils/fileio/io.py
@@ -1,5 +1,4 @@
 # Copyright (c) OpenMMLab. All rights reserved.
-# type: ignore
 from io import BytesIO, StringIO
 from pathlib import Path
 
diff --git a/mmengine/utils/fileio/parse.py b/mmengine/utils/fileio/parse.py
index fe424594..582221a3 100644
--- a/mmengine/utils/fileio/parse.py
+++ b/mmengine/utils/fileio/parse.py
@@ -1,5 +1,4 @@
 # Copyright (c) OpenMMLab. All rights reserved.
-# type: ignore
 from io import StringIO
 
 from .file_client import FileClient
-- 
GitLab