use protocol for sys.meta_path (#5349)

This commit is contained in:
Akuli
2021-05-06 17:12:34 +03:00
committed by GitHub
parent d16017ba0d
commit bb5fb845f2
2 changed files with 13 additions and 2 deletions

View File

@@ -38,6 +38,7 @@ class SourceLoader(ResourceLoader, ExecutionLoader, metaclass=ABCMeta):
def get_source(self, fullname: str) -> Optional[str]: ...
def path_stats(self, path: _Path) -> Mapping[str, Any]: ...
# Please keep in sync with sys._MetaPathFinder
class MetaPathFinder(Finder):
def find_module(self, fullname: str, path: Optional[Sequence[_Path]]) -> Optional[Loader]: ...
def invalidate_caches(self) -> None: ...

View File

@@ -1,6 +1,7 @@
import sys
from builtins import object as _object
from importlib.abc import MetaPathFinder, PathEntryFinder
from importlib.abc import Loader, PathEntryFinder
from importlib.machinery import ModuleSpec
from types import FrameType, ModuleType, TracebackType
from typing import (
Any,
@@ -10,6 +11,7 @@ from typing import (
List,
NoReturn,
Optional,
Protocol,
Sequence,
TextIO,
Tuple,
@@ -24,6 +26,14 @@ _T = TypeVar("_T")
# The following type alias are stub-only and do not exist during runtime
_ExcInfo = Tuple[Type[BaseException], BaseException, TracebackType]
_OptExcInfo = Union[_ExcInfo, Tuple[None, None, None]]
_PathSequence = Sequence[Union[bytes, str]]
# Unlike importlib.abc.MetaPathFinder, invalidate_caches() might not exist (see python docs)
class _MetaPathFinder(Protocol):
def find_module(self, fullname: str, path: Optional[_PathSequence]) -> Optional[Loader]: ...
def find_spec(
self, fullname: str, path: Optional[_PathSequence], target: Optional[ModuleType] = ...
) -> Optional[ModuleSpec]: ...
# ----- sys variables -----
if sys.platform != "win32":
@@ -48,7 +58,7 @@ last_value: Optional[BaseException]
last_traceback: Optional[TracebackType]
maxsize: int
maxunicode: int
meta_path: List[MetaPathFinder]
meta_path: List[_MetaPathFinder]
modules: Dict[str, ModuleType]
path: List[str]
path_hooks: List[Any] # TODO precise type; function, path to finder