From 5c08f4624650352adbde5fea433681323f424e7f Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Mon, 4 Jul 2016 15:27:23 -0700 Subject: [PATCH] Attempt to reduce cyclical dependencies between types and importlib. - 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 #1797. --- stdlib/3/_importlib_modulespec.pyi | 44 ++++++++++++++++++++++++++++++ stdlib/3/importlib/_modulespec.pyi | 19 ------------- stdlib/3/importlib/abc.pyi | 17 ++++-------- stdlib/3/importlib/machinery.pyi | 6 ++-- stdlib/3/types.pyi | 16 ++--------- 5 files changed, 55 insertions(+), 47 deletions(-) create mode 100644 stdlib/3/_importlib_modulespec.pyi delete mode 100644 stdlib/3/importlib/_modulespec.pyi diff --git a/stdlib/3/_importlib_modulespec.pyi b/stdlib/3/_importlib_modulespec.pyi new file mode 100644 index 000000000000..20d889471400 --- /dev/null +++ b/stdlib/3/_importlib_modulespec.pyi @@ -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: ... diff --git a/stdlib/3/importlib/_modulespec.pyi b/stdlib/3/importlib/_modulespec.pyi deleted file mode 100644 index 3d27ec280ddc..000000000000 --- a/stdlib/3/importlib/_modulespec.pyi +++ /dev/null @@ -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 diff --git a/stdlib/3/importlib/abc.pyi b/stdlib/3/importlib/abc.pyi index 9920965814c5..9600492c5922 100644 --- a/stdlib/3/importlib/abc.pyi +++ b/stdlib/3/importlib/abc.pyi @@ -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 diff --git a/stdlib/3/importlib/machinery.pyi b/stdlib/3/importlib/machinery.pyi index fa6554f0b991..d50b4a56da23 100644 --- a/stdlib/3/importlib/machinery.pyi +++ b/stdlib/3/importlib/machinery.pyi @@ -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): diff --git a/stdlib/3/types.pyi b/stdlib/3/types.pyi index ffb1fd6c641f..1026cb1c7743 100644 --- a/stdlib/3/types.pyi +++ b/stdlib/3/types.pyi @@ -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