Skip to content

Commit 43c3406

Browse files
brettcannongvanrossum
authored andcommitted
Add stubs for importlib.machinery (#323)
* Fix some misspelled method names that were also missing 'self' * Initial stubs for importlib.machinery * Use importlib.machinery.ModuleSpec everywhere
1 parent 05e02c1 commit 43c3406

File tree

4 files changed

+154
-6
lines changed

4 files changed

+154
-6
lines changed

stdlib/3/importlib/_modulespec.pyi

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# ModuleSpec is in its own file to deal with import loops; defined in
2+
# importlib.machinery.
3+
import importlib.abc
4+
import sys
5+
from typing import Any, Optional
6+
7+
if sys.version_info >= (3, 4):
8+
class ModuleSpec:
9+
def __init__(self, name: str, loader: Optional[importlib.abc.Loader], *,
10+
origin: str = None, loader_state: Any = None,
11+
is_package: bool = None) -> None: ...
12+
name = ... # type: str
13+
loader = ... # type: Optional[importlib.abc.Loader]
14+
origin = ... # type: Optional[str]
15+
submodule_search_locations = ... # type: Optional[List[str]]
16+
loader_state = ... # type: Any
17+
cached = ... # type: Optional[str]
18+
parent = ... # type: Optional[str]
19+
has_location = ... # type: bool

stdlib/3/importlib/abc.pyi

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import abc
2+
from importlib._modulespec import ModuleSpec
23
import sys
34
import types
45
from typing import Mapping, Optional, Sequence, Union
@@ -10,7 +11,8 @@ class Loader(metaclass=abc.ABCMeta):
1011
if sys.version_info >= (3, 3):
1112
def module_repr(self, module: types.ModuleType) -> str: ...
1213
if sys.version_info >= (3, 4):
13-
def create_module(self, spec: Any) -> Optional[types.ModuleType]: ...
14+
def create_module(self, spec: ModuleSpec) -> Optional[types.ModuleType]:
15+
...
1416
# Not defined on the actual class for backwards-compatibility reasons,
1517
# but expected in new code.
1618
def exec_module(self, module: types.ModuleType) -> None: ...
@@ -64,8 +66,9 @@ if sys.version_info >= (3, 3):
6466
def invalidate_caches(self) -> None: ...
6567
if sys.version_info >= (3, 4):
6668
# Not defined on the actual class, but expected to exist.
67-
def findAny(fullname: str, path: Optional[Sequence[_Path]],
68-
target: types.ModuleType = None) -> Optional[Any]:
69+
def find_spec(self, fullname: str, path: Optional[Sequence[_Path]],
70+
target: types.ModuleType = None
71+
) -> Optional[ModuleSpec]:
6972
...
7073

7174
class PathEntryFinder(Finder):
@@ -75,8 +78,9 @@ if sys.version_info >= (3, 3):
7578
def invalidate_caches(self) -> None: ...
7679
if sys.version_info >= (3, 4):
7780
# Not defined on the actual class, but expected to exist.
78-
def findAny(fullname: str,
79-
target: types.ModuleType = None) -> Optional[Any]:
81+
def find_spec(self, fullname: str,
82+
target: types.ModuleType = None
83+
) -> Optional[ModuleSpec]:
8084
...
8185

8286
class FileLoader(ResourceLoader, ExecutionLoader):

stdlib/3/importlib/machinery.pyi

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import importlib.abc
2+
import sys
3+
import types
4+
from typing import Any, Callable, List, Optional, Sequence, Tuple, Union
5+
6+
# ModuleSpec is defined in this module, but for circular import reasons exists
7+
# in its own stub file.
8+
from importlib._modulespec import ModuleSpec
9+
10+
class BuiltinImporter(importlib.abc.MetaPathFinder,
11+
importlib.abc.InspectLoader):
12+
# MetaPathFinder
13+
@classmethod
14+
def find_module(cls, fullname: str,
15+
path: Optional[Sequence[importlib.abc._Path]]
16+
) -> Optional[importlib.abc.Loader]:
17+
...
18+
if sys.version_info >= (3, 4):
19+
@classmethod
20+
def find_spec(cls, fullname: str,
21+
path: Optional[Sequence[importlib.abc._Path]],
22+
target: types.ModuleType = None) -> Optional[ModuleSpec]:
23+
...
24+
# InspectLoader
25+
@classmethod
26+
def is_package(cls, fullname: str) -> bool: ...
27+
@classmethod
28+
def load_module(cls, fullname: str) -> types.ModuleType: ...
29+
@classmethod
30+
def get_code(cls, fullname: str) -> None: ... # type: ignore
31+
@classmethod
32+
def get_source(cls, fullname: str) -> None: ... # type: ignore
33+
# Loader
34+
@classmethod
35+
def load_module(cls, fullname: str) -> types.ModuleType: ...
36+
if sys.version_info >= (3, 3):
37+
@staticmethod
38+
def module_repr(module: types.ModuleType) -> str: ... # type: ignore
39+
if sys.version_info >= (3, 4):
40+
@classmethod
41+
def create_module(cls, spec: ModuleSpec) -> Optional[types.ModuleType]:
42+
...
43+
@classmethod
44+
def exec_module(cls, module: types.ModuleType) -> None: ...
45+
46+
class FrozenImporter(importlib.abc.MetaPathFinder, importlib.abc.InspectLoader):
47+
# MetaPathFinder
48+
@classmethod
49+
def find_module(cls, fullname: str,
50+
path: Optional[Sequence[importlib.abc._Path]]
51+
) -> Optional[importlib.abc.Loader]:
52+
...
53+
if sys.version_info >= (3, 4):
54+
@classmethod
55+
def find_spec(cls, fullname: str,
56+
path: Optional[Sequence[importlib.abc._Path]],
57+
target: types.ModuleType = None) -> Optional[ModuleSpec]:
58+
...
59+
# InspectLoader
60+
@classmethod
61+
def is_package(cls, fullname: str) -> bool: ...
62+
@classmethod
63+
def load_module(cls, fullname: str) -> types.ModuleType: ...
64+
@classmethod
65+
def get_code(cls, fullname: str) -> None: ... # type: ignore
66+
@classmethod
67+
def get_source(cls, fullname: str) -> None: ... # type: ignore
68+
# Loader
69+
@classmethod
70+
def load_module(cls, fullname: str) -> types.ModuleType: ...
71+
if sys.version_info >= (3, 3):
72+
@staticmethod
73+
def module_repr(module: types.ModuleType) -> str: ... # type: ignore
74+
if sys.version_info >= (3, 4):
75+
@classmethod
76+
def create_module(cls, spec: ModuleSpec) -> Optional[types.ModuleType]:
77+
...
78+
@staticmethod
79+
def exec_module(module: types.ModuleType) -> None: ... # type: ignore
80+
81+
class WindowsRegisteryFinder(importlib.abc.MetaPathFinder):
82+
@classmethod
83+
def find_module(cls, fullname: str,
84+
path: Optional[Sequence[importlib.abc._Path]]
85+
) -> Optional[importlib.abc.Loader]:
86+
...
87+
if sys.version_info >= (3, 4):
88+
@classmethod
89+
def find_spec(cls, fullname: str,
90+
path: Optional[Sequence[importlib.abc._Path]],
91+
target: types.ModuleType = None) -> Optional[ModuleSpec]:
92+
...
93+
94+
class PathFinder(importlib.abc.MetaPathFinder): ...
95+
96+
if sys.version_info >= (3, 3):
97+
SOURCE_SUFFIXES = ... # type: List[str]
98+
DEBUG_BYTECODE_SUFFIXES = ... # type: List[str]
99+
OPTIMIZED_BYTECODE_SUFFIXES = ... # type: List[str]
100+
BYTECODE_SUFFIXES = ... # type: List[str]
101+
EXTENSION_SUFFIXES = ... # type: List[str]
102+
103+
def all_suffixes() -> List[str]: ...
104+
105+
class FileFinder(importlib.abc.PathEntryFinder):
106+
path = ... # type: str
107+
def __init__(self, path: str,
108+
*loader_details: Tuple[importlib.abc.Loader, List[str]]
109+
) -> None: ...
110+
@classmethod
111+
def path_hook(*loader_details: Tuple[importlib.abc.Loader, List[str]]
112+
) -> Callable[[str], importlib.abc.PathEntryFinder]: ...
113+
114+
class SourceFileLoader(importlib.abc.FileLoader,
115+
importlib.abc.SourceLoader):
116+
...
117+
118+
class SourcelessFileLoader(importlib.abc.FileLoader,
119+
importlib.abc.SourceLoader):
120+
...
121+
122+
class ExtensionFileLoader(importlib.abc.ExecutionLoader):
123+
def get_filename(self, fullname: str) -> importlib.abc._Path: ...
124+
def get_source(self, fullname: str) -> None: ... # type: ignore

stdlib/3/types.pyi

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# TODO parts of this should be conditional on version
55

66
import importlib.abc
7+
from importlib._modulespec import ModuleSpec
78
import sys
89
from typing import (
910
Any, Callable, Dict, Generic, Iterator, Mapping, Optional, Tuple, TypeVar,
@@ -115,7 +116,7 @@ class ModuleType:
115116
__package__ = ... # type: Optional[str]
116117
# Should be Optional[ModuleSpec], but importlib.machinery has no stub
117118
# yet.
118-
__spec__ = ... # type: Optional[Any]
119+
__spec__ = ... # type: Optional[ModuleSpec]
119120
def __init__(self, name: str, doc: str) -> None: ...
120121

121122
class TracebackType:

0 commit comments

Comments
 (0)