Add type hints to test.test_no_ssl
MODIFY
noxfile.py
noxfile.py
@@ -179,6 +179,7 @@ def mypy(session: nox.Session) -> None:
"test/test_exceptions.py",
"test/test_fields.py",
"test/test_filepost.py",
+ "test/test_no_ssl.py",
"test/test_poolmanager.py",
"test/test_proxymanager.py",
"test/test_queue_monkeypatch.py",

MODIFY
test/__init__.py
test/__init__.py
@@ -5,6 +5,7 @@ import platform
import socket
import sys
import warnings
+from importlib.abc import Loader, MetaPathFinder
from types import ModuleType, TracebackType
from typing import (
TYPE_CHECKING,
@@ -13,8 +14,10 @@ from typing import (
Dict,
List,
Optional,
+ Sequence,
Type,
TypeVar,
+ Union,
cast,
)
@@ -273,7 +276,12 @@ class LogRecorder:
return False
-class ImportBlocker:
+class ImportBlockerLoader(Loader):
+ def load_module(self, fullname: str) -> ModuleType:
+ raise ImportError(f"import of {fullname} is blocked")
+
+
+class ImportBlocker(MetaPathFinder):
"""
Block Imports
@@ -285,17 +293,14 @@ class ImportBlocker:
self.namestoblock = namestoblock
def find_module(
- self, fullname: str, path: Optional[str] = None
- ) -> Optional["ImportBlocker"]:
+ self, fullname: str, path: Optional[Sequence[Union[bytes, str]]] = None
+ ) -> Optional[Loader]:
if fullname in self.namestoblock:
- return self
+ return ImportBlockerLoader()
return None
- def load_module(self, fullname: str) -> None:
- raise ImportError(f"import of {fullname} is blocked")
-
-class ModuleStash:
+class ModuleStash(MetaPathFinder):
"""
Stashes away previously imported modules

MODIFY
test/test_no_ssl.py
test/test_no_ssl.py
@@ -16,7 +16,7 @@ module_stash = ModuleStash("urllib3")
class TestWithoutSSL:
@classmethod
- def setup_class(cls):
+ def setup_class(cls) -> None:
sys.modules.pop("ssl", None)
sys.modules.pop("_ssl", None)
@@ -24,15 +24,15 @@ class TestWithoutSSL:
sys.meta_path.insert(0, ssl_blocker)
@classmethod
- def teardown_class(cls):
+ def teardown_class(cls) -> None:
sys.meta_path.remove(ssl_blocker)
module_stash.pop()
class TestImportWithoutSSL(TestWithoutSSL):
- def test_cannot_import_ssl(self):
+ def test_cannot_import_ssl(self) -> None:
with pytest.raises(ImportError):
import ssl # noqa: F401
- def test_import_urllib3(self):
+ def test_import_urllib3(self) -> None:
import urllib3 # noqa: F401

Add type hints to test.test_no_ssl