3 from _typeshed import (
6 OpenBinaryModeUpdating,
12 from abc import ABCMeta, abstractmethod
13 from importlib.machinery import ModuleSpec
14 from io import BufferedRandom, BufferedReader, BufferedWriter, FileIO, TextIOWrapper
15 from typing import IO, Any, BinaryIO, Iterator, Mapping, Protocol, Sequence, Union, overload
16 from typing_extensions import Literal, runtime_checkable
18 _Path = Union[bytes, str]
20 class Finder(metaclass=ABCMeta): ...
22 class ResourceLoader(Loader):
24 def get_data(self, path: _Path) -> bytes: ...
26 class InspectLoader(Loader):
27 def is_package(self, fullname: str) -> bool: ...
28 def get_code(self, fullname: str) -> types.CodeType | None: ...
29 def load_module(self, fullname: str) -> types.ModuleType: ...
31 def get_source(self, fullname: str) -> str | None: ...
32 def exec_module(self, module: types.ModuleType) -> None: ...
34 def source_to_code(data: bytes | str, path: str = ...) -> types.CodeType: ...
36 class ExecutionLoader(InspectLoader):
38 def get_filename(self, fullname: str) -> _Path: ...
39 def get_code(self, fullname: str) -> types.CodeType | None: ...
41 class SourceLoader(ResourceLoader, ExecutionLoader, metaclass=ABCMeta):
42 def path_mtime(self, path: _Path) -> float: ...
43 def set_data(self, path: _Path, data: bytes) -> None: ...
44 def get_source(self, fullname: str) -> str | None: ...
45 def path_stats(self, path: _Path) -> Mapping[str, Any]: ...
47 # Please keep in sync with sys._MetaPathFinder
48 class MetaPathFinder(Finder):
49 def find_module(self, fullname: str, path: Sequence[_Path] | None) -> Loader | None: ...
50 def invalidate_caches(self) -> None: ...
51 # Not defined on the actual class, but expected to exist.
53 self, fullname: str, path: Sequence[_Path] | None, target: types.ModuleType | None = ...
54 ) -> ModuleSpec | None: ...
56 class PathEntryFinder(Finder):
57 def find_module(self, fullname: str) -> Loader | None: ...
58 def find_loader(self, fullname: str) -> tuple[Loader | None, Sequence[_Path]]: ...
59 def invalidate_caches(self) -> None: ...
60 # Not defined on the actual class, but expected to exist.
61 def find_spec(self, fullname: str, target: types.ModuleType | None = ...) -> ModuleSpec | None: ...
63 class Loader(metaclass=ABCMeta):
64 def load_module(self, fullname: str) -> types.ModuleType: ...
65 def module_repr(self, module: types.ModuleType) -> str: ...
66 def create_module(self, spec: ModuleSpec) -> types.ModuleType | None: ...
67 # Not defined on the actual class for backwards-compatibility reasons,
68 # but expected in new code.
69 def exec_module(self, module: types.ModuleType) -> None: ...
71 class _LoaderProtocol(Protocol):
72 def load_module(self, fullname: str) -> types.ModuleType: ...
74 class FileLoader(ResourceLoader, ExecutionLoader, metaclass=ABCMeta):
77 def __init__(self, fullname: str, path: _Path) -> None: ...
78 def get_data(self, path: _Path) -> bytes: ...
79 def get_filename(self, name: str | None = ...) -> _Path: ...
80 def load_module(self, name: str | None = ...) -> types.ModuleType: ...
82 if sys.version_info >= (3, 7):
83 class ResourceReader(metaclass=ABCMeta):
85 def open_resource(self, resource: StrOrBytesPath) -> IO[bytes]: ...
87 def resource_path(self, resource: StrOrBytesPath) -> str: ...
89 def is_resource(self, name: str) -> bool: ...
91 def contents(self) -> Iterator[str]: ...
93 if sys.version_info >= (3, 9):
95 class Traversable(Protocol):
97 def is_dir(self) -> bool: ...
99 def is_file(self) -> bool: ...
101 def iterdir(self) -> Iterator[Traversable]: ...
103 def joinpath(self, child: StrPath) -> Traversable: ...
104 # The .open method comes from pathlib.pyi and should be kept in sync.
109 mode: OpenTextMode = ...,
110 buffering: int = ...,
111 encoding: str | None = ...,
112 errors: str | None = ...,
113 newline: str | None = ...,
114 ) -> TextIOWrapper: ...
115 # Unbuffered binary mode: returns a FileIO
119 self, mode: OpenBinaryMode, buffering: Literal[0], encoding: None = ..., errors: None = ..., newline: None = ...
121 # Buffering is on: return BufferedRandom, BufferedReader, or BufferedWriter
126 mode: OpenBinaryModeUpdating,
127 buffering: Literal[-1, 1] = ...,
128 encoding: None = ...,
131 ) -> BufferedRandom: ...
136 mode: OpenBinaryModeWriting,
137 buffering: Literal[-1, 1] = ...,
138 encoding: None = ...,
141 ) -> BufferedWriter: ...
146 mode: OpenBinaryModeReading,
147 buffering: Literal[-1, 1] = ...,
148 encoding: None = ...,
151 ) -> BufferedReader: ...
152 # Buffering cannot be determined: fall back to BinaryIO
156 self, mode: OpenBinaryMode, buffering: int, encoding: None = ..., errors: None = ..., newline: None = ...
158 # Fallback if mode is not specified
162 self, mode: str, buffering: int = ..., encoding: str | None = ..., errors: str | None = ..., newline: str | None = ...
165 def name(self) -> str: ...
167 def __truediv__(self, key: StrPath) -> Traversable: ...
169 def read_bytes(self) -> bytes: ...
171 def read_text(self, encoding: str | None = ...) -> str: ...