Skip to content

Commit

Permalink
Add stubs for importlib.machinery (#323)
Browse files Browse the repository at this point in the history
* Fix some misspelled method names that were also missing 'self'

* Initial stubs for importlib.machinery

* Use importlib.machinery.ModuleSpec everywhere
  • Loading branch information
brettcannon authored and gvanrossum committed Jul 1, 2016
1 parent 05e02c1 commit 43c3406
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 6 deletions.
19 changes: 19 additions & 0 deletions stdlib/3/importlib/_modulespec.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# ModuleSpec is in its own file to deal with import loops; defined in
# importlib.machinery.
import importlib.abc
import sys
from typing import Any, Optional

if sys.version_info >= (3, 4):
class ModuleSpec:
def __init__(self, name: str, loader: Optional[importlib.abc.Loader], *,
origin: str = None, loader_state: Any = None,
is_package: bool = None) -> None: ...
name = ... # type: str
loader = ... # type: Optional[importlib.abc.Loader]
origin = ... # type: Optional[str]
submodule_search_locations = ... # type: Optional[List[str]]
loader_state = ... # type: Any
cached = ... # type: Optional[str]
parent = ... # type: Optional[str]
has_location = ... # type: bool
14 changes: 9 additions & 5 deletions stdlib/3/importlib/abc.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import abc
from importlib._modulespec import ModuleSpec
import sys
import types
from typing import Mapping, Optional, Sequence, Union
Expand All @@ -10,7 +11,8 @@ class Loader(metaclass=abc.ABCMeta):
if sys.version_info >= (3, 3):
def module_repr(self, module: types.ModuleType) -> str: ...
if sys.version_info >= (3, 4):
def create_module(self, spec: Any) -> Optional[types.ModuleType]: ...
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: ...
Expand Down Expand Up @@ -64,8 +66,9 @@ if sys.version_info >= (3, 3):
def invalidate_caches(self) -> None: ...
if sys.version_info >= (3, 4):
# Not defined on the actual class, but expected to exist.
def findAny(fullname: str, path: Optional[Sequence[_Path]],
target: types.ModuleType = None) -> Optional[Any]:
def find_spec(self, fullname: str, path: Optional[Sequence[_Path]],
target: types.ModuleType = None
) -> Optional[ModuleSpec]:
...

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

class FileLoader(ResourceLoader, ExecutionLoader):
Expand Down
124 changes: 124 additions & 0 deletions stdlib/3/importlib/machinery.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import importlib.abc
import sys
import types
from typing import Any, Callable, List, Optional, Sequence, Tuple, Union

# ModuleSpec is defined in this module, but for circular import reasons exists
# in its own stub file.
from importlib._modulespec import ModuleSpec

class BuiltinImporter(importlib.abc.MetaPathFinder,
importlib.abc.InspectLoader):
# MetaPathFinder
@classmethod
def find_module(cls, fullname: str,
path: Optional[Sequence[importlib.abc._Path]]
) -> Optional[importlib.abc.Loader]:
...
if sys.version_info >= (3, 4):
@classmethod
def find_spec(cls, fullname: str,
path: Optional[Sequence[importlib.abc._Path]],
target: types.ModuleType = None) -> Optional[ModuleSpec]:
...
# InspectLoader
@classmethod
def is_package(cls, fullname: str) -> bool: ...
@classmethod
def load_module(cls, fullname: str) -> types.ModuleType: ...
@classmethod
def get_code(cls, fullname: str) -> None: ... # type: ignore
@classmethod
def get_source(cls, fullname: str) -> None: ... # type: ignore
# Loader
@classmethod
def load_module(cls, fullname: str) -> types.ModuleType: ...
if sys.version_info >= (3, 3):
@staticmethod
def module_repr(module: types.ModuleType) -> str: ... # type: ignore
if sys.version_info >= (3, 4):
@classmethod
def create_module(cls, spec: ModuleSpec) -> Optional[types.ModuleType]:
...
@classmethod
def exec_module(cls, module: types.ModuleType) -> None: ...

class FrozenImporter(importlib.abc.MetaPathFinder, importlib.abc.InspectLoader):
# MetaPathFinder
@classmethod
def find_module(cls, fullname: str,
path: Optional[Sequence[importlib.abc._Path]]
) -> Optional[importlib.abc.Loader]:
...
if sys.version_info >= (3, 4):
@classmethod
def find_spec(cls, fullname: str,
path: Optional[Sequence[importlib.abc._Path]],
target: types.ModuleType = None) -> Optional[ModuleSpec]:
...
# InspectLoader
@classmethod
def is_package(cls, fullname: str) -> bool: ...
@classmethod
def load_module(cls, fullname: str) -> types.ModuleType: ...
@classmethod
def get_code(cls, fullname: str) -> None: ... # type: ignore
@classmethod
def get_source(cls, fullname: str) -> None: ... # type: ignore
# Loader
@classmethod
def load_module(cls, fullname: str) -> types.ModuleType: ...
if sys.version_info >= (3, 3):
@staticmethod
def module_repr(module: types.ModuleType) -> str: ... # type: ignore
if sys.version_info >= (3, 4):
@classmethod
def create_module(cls, spec: ModuleSpec) -> Optional[types.ModuleType]:
...
@staticmethod
def exec_module(module: types.ModuleType) -> None: ... # type: ignore

class WindowsRegisteryFinder(importlib.abc.MetaPathFinder):
@classmethod
def find_module(cls, fullname: str,
path: Optional[Sequence[importlib.abc._Path]]
) -> Optional[importlib.abc.Loader]:
...
if sys.version_info >= (3, 4):
@classmethod
def find_spec(cls, fullname: str,
path: Optional[Sequence[importlib.abc._Path]],
target: types.ModuleType = None) -> Optional[ModuleSpec]:
...

class PathFinder(importlib.abc.MetaPathFinder): ...

if sys.version_info >= (3, 3):
SOURCE_SUFFIXES = ... # type: List[str]
DEBUG_BYTECODE_SUFFIXES = ... # type: List[str]
OPTIMIZED_BYTECODE_SUFFIXES = ... # type: List[str]
BYTECODE_SUFFIXES = ... # type: List[str]
EXTENSION_SUFFIXES = ... # type: List[str]

def all_suffixes() -> List[str]: ...

class FileFinder(importlib.abc.PathEntryFinder):
path = ... # type: str
def __init__(self, path: str,
*loader_details: Tuple[importlib.abc.Loader, List[str]]
) -> None: ...
@classmethod
def path_hook(*loader_details: Tuple[importlib.abc.Loader, List[str]]
) -> Callable[[str], importlib.abc.PathEntryFinder]: ...

class SourceFileLoader(importlib.abc.FileLoader,
importlib.abc.SourceLoader):
...

class SourcelessFileLoader(importlib.abc.FileLoader,
importlib.abc.SourceLoader):
...

class ExtensionFileLoader(importlib.abc.ExecutionLoader):
def get_filename(self, fullname: str) -> importlib.abc._Path: ...
def get_source(self, fullname: str) -> None: ... # type: ignore
3 changes: 2 additions & 1 deletion stdlib/3/types.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# TODO parts of this should be conditional on version

import importlib.abc
from importlib._modulespec import ModuleSpec
import sys
from typing import (
Any, Callable, Dict, Generic, Iterator, Mapping, Optional, Tuple, TypeVar,
Expand Down Expand Up @@ -115,7 +116,7 @@ class ModuleType:
__package__ = ... # type: Optional[str]
# Should be Optional[ModuleSpec], but importlib.machinery has no stub
# yet.
__spec__ = ... # type: Optional[Any]
__spec__ = ... # type: Optional[ModuleSpec]
def __init__(self, name: str, doc: str) -> None: ...

class TracebackType:
Expand Down

0 comments on commit 43c3406

Please sign in to comment.