diff --git a/stdlib/VERSIONS b/stdlib/VERSIONS index 872219720..63f46c6ba 100644 --- a/stdlib/VERSIONS +++ b/stdlib/VERSIONS @@ -13,7 +13,6 @@ _dummy_thread: 3.6 _dummy_threading: 2.7 _heapq: 2.7 _imp: 3.6 -_importlib_modulespec: 3.6 _json: 3.6 _markupbase: 3.6 _msi: 2.7 diff --git a/stdlib/_importlib_modulespec.pyi b/stdlib/_importlib_modulespec.pyi deleted file mode 100644 index 114b78e10..000000000 --- a/stdlib/_importlib_modulespec.pyi +++ /dev/null @@ -1,50 +0,0 @@ -# ModuleSpec, ModuleType, Loader are part of a dependency cycle. -# They are officially defined/exported in other places: -# -# - ModuleType in types -# - Loader in importlib.abc -# - ModuleSpec in importlib.machinery (3.4 and later only) -# -# _Loader is the PEP-451-defined interface for a loader type/object. - -from abc import ABCMeta -from typing import Any, Dict, List, Optional, Protocol - -class _Loader(Protocol): - def load_module(self, fullname: str) -> ModuleType: ... - -class ModuleSpec: - def __init__( - self, - name: str, - loader: Optional[Loader], - *, - origin: Optional[str] = ..., - loader_state: Any = ..., - is_package: Optional[bool] = ..., - ) -> None: ... - name: str - loader: Optional[_Loader] - origin: Optional[str] - submodule_search_locations: Optional[List[str]] - loader_state: Any - cached: Optional[str] - parent: Optional[str] - has_location: bool - -class ModuleType: - __name__: str - __file__: str - __dict__: Dict[str, Any] - __loader__: Optional[_Loader] - __package__: Optional[str] - __spec__: Optional[ModuleSpec] - def __init__(self, name: str, doc: Optional[str] = ...) -> None: ... - -class Loader(metaclass=ABCMeta): - def load_module(self, fullname: str) -> ModuleType: ... - def module_repr(self, module: ModuleType) -> str: ... - def create_module(self, spec: ModuleSpec) -> Optional[ModuleType]: ... - # Not defined on the actual class for backwards-compatibility reasons, - # but expected in new code. - def exec_module(self, module: ModuleType) -> None: ... diff --git a/stdlib/importlib/abc.pyi b/stdlib/importlib/abc.pyi index 6b4ef5ced..62b391e21 100644 --- a/stdlib/importlib/abc.pyi +++ b/stdlib/importlib/abc.pyi @@ -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 diff --git a/stdlib/importlib/machinery.pyi b/stdlib/importlib/machinery.pyi index 31ffdf7a4..beae43bd3 100644 --- a/stdlib/importlib/machinery.pyi +++ b/stdlib/importlib/machinery.pyi @@ -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] diff --git a/stdlib/types.pyi b/stdlib/types.pyi index 7fb0de7ac..2b4d32392 100644 --- a/stdlib/types.pyi +++ b/stdlib/types.pyi @@ -1,12 +1,10 @@ import sys import typing +from importlib.abc import _LoaderProtocol +from importlib.machinery import ModuleSpec from typing import Any, Awaitable, Callable, Dict, Generic, Iterable, Iterator, Mapping, Optional, Tuple, Type, TypeVar, overload from typing_extensions import Literal, final -# ModuleType is exported from this module, but for circular import -# reasons exists in its own stub file (with ModuleSpec and Loader). -from _importlib_modulespec import ModuleType as ModuleType # Exported - # Note, all classes "defined" here require special handling. _T = TypeVar("_T") @@ -135,6 +133,15 @@ class SimpleNamespace: def __setattr__(self, name: str, value: Any) -> None: ... def __delattr__(self, name: str) -> None: ... +class ModuleType: + __name__: str + __file__: str + __dict__: Dict[str, Any] + __loader__: Optional[_LoaderProtocol] + __package__: Optional[str] + __spec__: Optional[ModuleSpec] + def __init__(self, name: str, doc: Optional[str] = ...) -> None: ... + class GeneratorType: gi_code: CodeType gi_frame: FrameType diff --git a/tests/stubtest_whitelists/py3_common.txt b/tests/stubtest_whitelists/py3_common.txt index 4571f14d9..37559610b 100644 --- a/tests/stubtest_whitelists/py3_common.txt +++ b/tests/stubtest_whitelists/py3_common.txt @@ -12,7 +12,6 @@ _collections_abc.Mapping.get # Adding None to the Union messed up mypy _collections_abc.Sequence.index # Supporting None in end is not mandatory _csv.Dialect.__init__ # C __init__ signature is inaccurate _dummy_threading -_importlib_modulespec _threading_local.local.__new__ _typeshed.* # Utility types for typeshed, doesn't exist at runtime abc.abstractclassmethod