mirror of
https://github.com/davidhalter/typeshed.git
synced 2025-12-31 16:44:25 +08:00
Attempt to reduce cyclical dependencies between types and importlib. (#335)
- Move Loader and ModuleType into _importlib_modulespec.pyi. - Add "import X as X" for these to types.pyi and importlib/abc.pyi. The goal is to ensure mypy -i still works, to fix https://github.com/python/mypy/issues/1797.
This commit is contained in:
44
stdlib/3/_importlib_modulespec.pyi
Normal file
44
stdlib/3/_importlib_modulespec.pyi
Normal file
@@ -0,0 +1,44 @@
|
||||
# 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)
|
||||
|
||||
import abc
|
||||
import sys
|
||||
from typing import Any, Optional
|
||||
|
||||
if sys.version_info >= (3, 4):
|
||||
class ModuleSpec:
|
||||
def __init__(self, name: str, loader: Optional['Loader'], *,
|
||||
origin: str = None, loader_state: Any = None,
|
||||
is_package: bool = None) -> None: ...
|
||||
name = ... # type: str
|
||||
loader = ... # type: Optional[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
|
||||
|
||||
class ModuleType:
|
||||
__name__ = ... # type: str
|
||||
__file__ = ... # type: str
|
||||
__doc__ = ... # type: Optional[str]
|
||||
if sys.version_info >= (3, 4):
|
||||
__loader__ = ... # type: Optional[Loader]
|
||||
__package__ = ... # type: Optional[str]
|
||||
__spec__ = ... # type: Optional[ModuleSpec]
|
||||
def __init__(self, name: str, doc: str) -> None: ...
|
||||
|
||||
class Loader(metaclass=abc.ABCMeta):
|
||||
def load_module(self, fullname: str) -> ModuleType: ...
|
||||
if sys.version_info >= (3, 3):
|
||||
def module_repr(self, module: ModuleType) -> str: ...
|
||||
if sys.version_info >= (3, 4):
|
||||
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: ...
|
||||
@@ -1,19 +0,0 @@
|
||||
# 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
|
||||
@@ -1,21 +1,14 @@
|
||||
import abc
|
||||
from importlib._modulespec import ModuleSpec
|
||||
from _importlib_modulespec import ModuleSpec
|
||||
import sys
|
||||
import types
|
||||
from typing import Mapping, Optional, Sequence, Union
|
||||
from typing import Any, Mapping, Optional, Sequence, Tuple, Union
|
||||
|
||||
_Path = Union[bytes, str]
|
||||
|
||||
class Loader(metaclass=abc.ABCMeta):
|
||||
def load_module(self, fullname: str) -> types.ModuleType: ...
|
||||
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: 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: ...
|
||||
# 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 # Exported
|
||||
|
||||
class Finder(metaclass=abc.ABCMeta): ...
|
||||
# Technically this class defines the following method, but its subclasses
|
||||
|
||||
@@ -3,9 +3,9 @@ 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
|
||||
# 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 ModuleSpec # Exported
|
||||
|
||||
class BuiltinImporter(importlib.abc.MetaPathFinder,
|
||||
importlib.abc.InspectLoader):
|
||||
|
||||
@@ -3,8 +3,6 @@
|
||||
|
||||
# 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,
|
||||
@@ -108,17 +106,9 @@ class BuiltinFunctionType:
|
||||
def __call__(self, *args: Any, **kwargs: Any) -> Any: ...
|
||||
BuiltinMethodType = BuiltinFunctionType
|
||||
|
||||
class ModuleType:
|
||||
__name__ = ... # type: str
|
||||
__file__ = ... # type: str
|
||||
__doc__ = ... # type: Optional[str]
|
||||
if sys.version_info >= (3, 4):
|
||||
__loader__ = ... # type: Optional[importlib.abc.Loader]
|
||||
__package__ = ... # type: Optional[str]
|
||||
# Should be Optional[ModuleSpec], but importlib.machinery has no stub
|
||||
# yet.
|
||||
__spec__ = ... # type: Optional[ModuleSpec]
|
||||
def __init__(self, name: str, doc: str) -> None: ...
|
||||
# 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
|
||||
|
||||
class TracebackType:
|
||||
tb_frame = ... # type: FrameType
|
||||
|
||||
Reference in New Issue
Block a user