delete _importlib_modulespec (#5350)

* delete _importlib_modulespec

* use typing_extensions.runtime_checkable
This commit is contained in:
Akuli
2021-05-06 17:13:35 +03:00
committed by GitHub
parent bb5fb845f2
commit e66b7fa660
6 changed files with 46 additions and 68 deletions

View File

@@ -2,12 +2,9 @@ import sys
import types
from _typeshed import AnyPath
from abc import ABCMeta, abstractmethod
from typing import IO, Any, Iterator, Mapping, Optional, Sequence, Tuple, Union
from typing_extensions import Literal
# Loader is exported from this module, but for circular import reasons
# exists in its own stub file (with ModuleSpec and ModuleType).
from _importlib_modulespec import Loader as Loader, ModuleSpec # Exported
from importlib.machinery import ModuleSpec
from typing import IO, Any, Iterator, Mapping, Optional, Protocol, Sequence, Tuple, Union
from typing_extensions import Literal, runtime_checkable
_Path = Union[bytes, str]
@@ -54,6 +51,17 @@ class PathEntryFinder(Finder):
# Not defined on the actual class, but expected to exist.
def find_spec(self, fullname: str, target: Optional[types.ModuleType] = ...) -> Optional[ModuleSpec]: ...
class Loader(metaclass=ABCMeta):
def load_module(self, fullname: str) -> types.ModuleType: ...
def module_repr(self, module: types.ModuleType) -> str: ...
def create_module(self, spec: ModuleSpec) -> Optional[types.ModuleType]: ...
# Not defined on the actual class for backwards-compatibility reasons,
# but expected in new code.
def exec_module(self, module: types.ModuleType) -> None: ...
class _LoaderProtocol(Protocol):
def load_module(self, fullname: str) -> types.ModuleType: ...
class FileLoader(ResourceLoader, ExecutionLoader, metaclass=ABCMeta):
name: str
path: _Path
@@ -74,7 +82,6 @@ if sys.version_info >= (3, 7):
def contents(self) -> Iterator[str]: ...
if sys.version_info >= (3, 9):
from typing import Protocol, runtime_checkable
@runtime_checkable
class Traversable(Protocol):
@abstractmethod

View File

@@ -1,10 +1,26 @@
import importlib.abc
import types
from typing import Callable, List, Optional, Sequence, Tuple, Union
from typing import Any, Callable, List, Optional, Sequence, Tuple, Union
# ModuleSpec is exported from this module, but for circular import
# reasons exists in its own stub file (with Loader and ModuleType).
from _importlib_modulespec import Loader, ModuleSpec as ModuleSpec # Exported
# TODO: the loaders seem a bit backwards, attribute is protocol but __init__ arg isn't?
class ModuleSpec:
def __init__(
self,
name: str,
loader: Optional[importlib.abc.Loader],
*,
origin: Optional[str] = ...,
loader_state: Any = ...,
is_package: Optional[bool] = ...,
) -> None: ...
name: str
loader: Optional[importlib.abc._LoaderProtocol]
origin: Optional[str]
submodule_search_locations: Optional[List[str]]
loader_state: Any
cached: Optional[str]
parent: Optional[str]
has_location: bool
class BuiltinImporter(importlib.abc.MetaPathFinder, importlib.abc.InspectLoader):
# MetaPathFinder
@@ -78,7 +94,7 @@ class PathFinder:
cls, fullname: str, path: Optional[Sequence[Union[bytes, str]]] = ..., target: Optional[types.ModuleType] = ...
) -> Optional[ModuleSpec]: ...
@classmethod
def find_module(cls, fullname: str, path: Optional[Sequence[Union[bytes, str]]] = ...) -> Optional[Loader]: ...
def find_module(cls, fullname: str, path: Optional[Sequence[Union[bytes, str]]] = ...) -> Optional[importlib.abc.Loader]: ...
SOURCE_SUFFIXES: List[str]
DEBUG_BYTECODE_SUFFIXES: List[str]