Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e66b7fa

Browse files
authored
delete _importlib_modulespec (#5350)
* delete _importlib_modulespec * use typing_extensions.runtime_checkable
1 parent bb5fb84 commit e66b7fa

6 files changed

Lines changed: 46 additions & 68 deletions

File tree

stdlib/VERSIONS

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ _dummy_thread: 3.6
1313
_dummy_threading: 2.7
1414
_heapq: 2.7
1515
_imp: 3.6
16-
_importlib_modulespec: 3.6
1716
_json: 3.6
1817
_markupbase: 3.6
1918
_msi: 2.7

stdlib/_importlib_modulespec.pyi

Lines changed: 0 additions & 50 deletions
This file was deleted.

stdlib/importlib/abc.pyi

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@ import sys
22
import types
33
from _typeshed import AnyPath
44
from abc import ABCMeta, abstractmethod
5-
from typing import IO, Any, Iterator, Mapping, Optional, Sequence, Tuple, Union
6-
from typing_extensions import Literal
7-
8-
# Loader is exported from this module, but for circular import reasons
9-
# exists in its own stub file (with ModuleSpec and ModuleType).
10-
from _importlib_modulespec import Loader as Loader, ModuleSpec # Exported
5+
from importlib.machinery import ModuleSpec
6+
from typing import IO, Any, Iterator, Mapping, Optional, Protocol, Sequence, Tuple, Union
7+
from typing_extensions import Literal, runtime_checkable
118

129
_Path = Union[bytes, str]
1310

@@ -54,6 +51,17 @@ class PathEntryFinder(Finder):
5451
# Not defined on the actual class, but expected to exist.
5552
def find_spec(self, fullname: str, target: Optional[types.ModuleType] = ...) -> Optional[ModuleSpec]: ...
5653

54+
class Loader(metaclass=ABCMeta):
55+
def load_module(self, fullname: str) -> types.ModuleType: ...
56+
def module_repr(self, module: types.ModuleType) -> str: ...
57+
def create_module(self, spec: ModuleSpec) -> Optional[types.ModuleType]: ...
58+
# Not defined on the actual class for backwards-compatibility reasons,
59+
# but expected in new code.
60+
def exec_module(self, module: types.ModuleType) -> None: ...
61+
62+
class _LoaderProtocol(Protocol):
63+
def load_module(self, fullname: str) -> types.ModuleType: ...
64+
5765
class FileLoader(ResourceLoader, ExecutionLoader, metaclass=ABCMeta):
5866
name: str
5967
path: _Path
@@ -74,7 +82,6 @@ if sys.version_info >= (3, 7):
7482
def contents(self) -> Iterator[str]: ...
7583

7684
if sys.version_info >= (3, 9):
77-
from typing import Protocol, runtime_checkable
7885
@runtime_checkable
7986
class Traversable(Protocol):
8087
@abstractmethod

stdlib/importlib/machinery.pyi

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
11
import importlib.abc
22
import types
3-
from typing import Callable, List, Optional, Sequence, Tuple, Union
3+
from typing import Any, Callable, List, Optional, Sequence, Tuple, Union
44

5-
# ModuleSpec is exported from this module, but for circular import
6-
# reasons exists in its own stub file (with Loader and ModuleType).
7-
from _importlib_modulespec import Loader, ModuleSpec as ModuleSpec # Exported
5+
# TODO: the loaders seem a bit backwards, attribute is protocol but __init__ arg isn't?
6+
class ModuleSpec:
7+
def __init__(
8+
self,
9+
name: str,
10+
loader: Optional[importlib.abc.Loader],
11+
*,
12+
origin: Optional[str] = ...,
13+
loader_state: Any = ...,
14+
is_package: Optional[bool] = ...,
15+
) -> None: ...
16+
name: str
17+
loader: Optional[importlib.abc._LoaderProtocol]
18+
origin: Optional[str]
19+
submodule_search_locations: Optional[List[str]]
20+
loader_state: Any
21+
cached: Optional[str]
22+
parent: Optional[str]
23+
has_location: bool
824

925
class BuiltinImporter(importlib.abc.MetaPathFinder, importlib.abc.InspectLoader):
1026
# MetaPathFinder
@@ -78,7 +94,7 @@ class PathFinder:
7894
cls, fullname: str, path: Optional[Sequence[Union[bytes, str]]] = ..., target: Optional[types.ModuleType] = ...
7995
) -> Optional[ModuleSpec]: ...
8096
@classmethod
81-
def find_module(cls, fullname: str, path: Optional[Sequence[Union[bytes, str]]] = ...) -> Optional[Loader]: ...
97+
def find_module(cls, fullname: str, path: Optional[Sequence[Union[bytes, str]]] = ...) -> Optional[importlib.abc.Loader]: ...
8298

8399
SOURCE_SUFFIXES: List[str]
84100
DEBUG_BYTECODE_SUFFIXES: List[str]

stdlib/types.pyi

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import sys
22
import typing
3+
from importlib.abc import _LoaderProtocol
4+
from importlib.machinery import ModuleSpec
35
from typing import Any, Awaitable, Callable, Dict, Generic, Iterable, Iterator, Mapping, Optional, Tuple, Type, TypeVar, overload
46
from typing_extensions import Literal, final
57

6-
# ModuleType is exported from this module, but for circular import
7-
# reasons exists in its own stub file (with ModuleSpec and Loader).
8-
from _importlib_modulespec import ModuleType as ModuleType # Exported
9-
108
# Note, all classes "defined" here require special handling.
119

1210
_T = TypeVar("_T")
@@ -135,6 +133,15 @@ class SimpleNamespace:
135133
def __setattr__(self, name: str, value: Any) -> None: ...
136134
def __delattr__(self, name: str) -> None: ...
137135

136+
class ModuleType:
137+
__name__: str
138+
__file__: str
139+
__dict__: Dict[str, Any]
140+
__loader__: Optional[_LoaderProtocol]
141+
__package__: Optional[str]
142+
__spec__: Optional[ModuleSpec]
143+
def __init__(self, name: str, doc: Optional[str] = ...) -> None: ...
144+
138145
class GeneratorType:
139146
gi_code: CodeType
140147
gi_frame: FrameType

tests/stubtest_whitelists/py3_common.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ _collections_abc.Mapping.get # Adding None to the Union messed up mypy
1212
_collections_abc.Sequence.index # Supporting None in end is not mandatory
1313
_csv.Dialect.__init__ # C __init__ signature is inaccurate
1414
_dummy_threading
15-
_importlib_modulespec
1615
_threading_local.local.__new__
1716
_typeshed.* # Utility types for typeshed, doesn't exist at runtime
1817
abc.abstractclassmethod

0 commit comments

Comments
 (0)