diff --git a/stdlib/importlib/abc.pyi b/stdlib/importlib/abc.pyi index 47f7f071a6c0..6b4ef5cedd27 100644 --- a/stdlib/importlib/abc.pyi +++ b/stdlib/importlib/abc.pyi @@ -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: ... diff --git a/stdlib/sys.pyi b/stdlib/sys.pyi index 9052ce32b765..d431d2733b1c 100644 --- a/stdlib/sys.pyi +++ b/stdlib/sys.pyi @@ -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