Skip to content

Commit 908993a

Browse files
AlexWaygoodsrittau
andauthored
Add stubs for importlib.(resources.)readers (#10928)
Co-authored-by: Sebastian Rittau <[email protected]>
1 parent 17a8c92 commit 908993a

File tree

8 files changed

+86
-11
lines changed

8 files changed

+86
-11
lines changed

pyrightconfig.stricter.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"stubs/**/@tests/test_cases",
1111
"stdlib/distutils/command",
1212
"stdlib/distutils/dist.pyi",
13+
"stdlib/importlib/readers.pyi",
1314
"stdlib/lib2to3/fixes/*.pyi",
1415
"stdlib/_tkinter.pyi",
1516
"stdlib/tkinter/__init__.pyi",

stdlib/VERSIONS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,10 @@ imp: 2.7-3.11
152152
importlib: 2.7-
153153
importlib.metadata: 3.8-
154154
importlib.metadata._meta: 3.10-
155+
importlib.readers: 3.10-
155156
importlib.resources: 3.7-
156157
importlib.resources.abc: 3.11-
158+
importlib.resources.readers: 3.11-
157159
inspect: 2.7-
158160
io: 2.7-
159161
ipaddress: 3.3-

stdlib/importlib/abc.pyi

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ from abc import ABCMeta, abstractmethod
1414
from collections.abc import Iterator, Mapping, Sequence
1515
from importlib.machinery import ModuleSpec
1616
from io import BufferedRandom, BufferedReader, BufferedWriter, FileIO, TextIOWrapper
17-
from typing import IO, Any, BinaryIO, NoReturn, Protocol, overload, runtime_checkable
17+
from typing import IO, Any, BinaryIO, Protocol, overload, runtime_checkable
1818
from typing_extensions import Literal
1919

2020
if sys.version_info >= (3, 11):
@@ -203,8 +203,12 @@ if sys.version_info >= (3, 9):
203203
@property
204204
@abstractmethod
205205
def name(self) -> str: ...
206-
@abstractmethod
207-
def __truediv__(self, child: str) -> Traversable: ...
206+
if sys.version_info >= (3, 10):
207+
def __truediv__(self, child: str) -> Traversable: ...
208+
else:
209+
@abstractmethod
210+
def __truediv__(self, child: str) -> Traversable: ...
211+
208212
@abstractmethod
209213
def read_bytes(self) -> bytes: ...
210214
@abstractmethod
@@ -214,6 +218,6 @@ if sys.version_info >= (3, 9):
214218
@abstractmethod
215219
def files(self) -> Traversable: ...
216220
def open_resource(self, resource: str) -> BufferedReader: ...
217-
def resource_path(self, resource: Any) -> NoReturn: ...
221+
def resource_path(self, resource: Any) -> str: ...
218222
def is_resource(self, path: str) -> bool: ...
219223
def contents(self) -> Iterator[str]: ...

stdlib/importlib/readers.pyi

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# On py311+, things are actually defined in importlib.resources.readers,
2+
# and re-exported here,
3+
# but doing it this way leads to less code duplication for us
4+
5+
import pathlib
6+
import sys
7+
from _typeshed import Incomplete, StrPath
8+
from collections.abc import Iterable, Iterator
9+
from io import BufferedReader
10+
from typing import NoReturn, TypeVar
11+
from typing_extensions import Literal, Never
12+
13+
if sys.version_info >= (3, 11):
14+
import importlib.resources.abc as abc
15+
else:
16+
import importlib.abc as abc
17+
18+
if sys.version_info >= (3, 10):
19+
if sys.version_info >= (3, 11):
20+
__all__ = ["FileReader", "ZipReader", "MultiplexedPath", "NamespaceReader"]
21+
22+
if sys.version_info < (3, 11):
23+
_T = TypeVar("_T")
24+
25+
def remove_duplicates(items: Iterable[_T]) -> Iterator[_T]: ...
26+
27+
class FileReader(abc.TraversableResources):
28+
path: pathlib.Path
29+
def __init__(self, loader) -> None: ...
30+
def resource_path(self, resource: StrPath) -> str: ...
31+
def files(self) -> pathlib.Path: ...
32+
33+
class ZipReader(abc.TraversableResources):
34+
prefix: str
35+
archive: Incomplete
36+
def __init__(self, loader, module: str) -> None: ...
37+
def open_resource(self, resource: str) -> BufferedReader: ...
38+
def is_resource(self, path: StrPath) -> bool: ...
39+
def files(self): ...
40+
41+
class MultiplexedPath(abc.Traversable):
42+
def __init__(self, *paths: abc.Traversable) -> None: ...
43+
def iterdir(self) -> Iterator[abc.Traversable]: ...
44+
def read_bytes(self) -> NoReturn: ...
45+
def read_text(self, *args: Never, **kwargs: Never) -> NoReturn: ... # type: ignore[override]
46+
def is_dir(self) -> Literal[True]: ...
47+
def is_file(self) -> Literal[False]: ...
48+
if sys.version_info >= (3, 12):
49+
def joinpath(self, *descendants: str) -> abc.Traversable: ...
50+
else:
51+
def joinpath(self, child: str) -> abc.Traversable: ... # type: ignore[override]
52+
__truediv__ = joinpath
53+
def open(self, *args: Never, **kwargs: Never) -> NoReturn: ... # type: ignore[override]
54+
@property
55+
def name(self) -> str: ...
56+
57+
class NamespaceReader(abc.TraversableResources):
58+
path: MultiplexedPath
59+
def __init__(self, namespace_path) -> None: ...
60+
def resource_path(self, resource: str) -> str: ...
61+
def files(self) -> MultiplexedPath: ...
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# On py311+, things are actually defined here
2+
# and re-exported from importlib.readers,
3+
# but doing it this way leads to less code duplication for us
4+
5+
import sys
6+
from collections.abc import Iterable, Iterator
7+
from typing import TypeVar
8+
9+
if sys.version_info >= (3, 11):
10+
from importlib.readers import *
11+
12+
_T = TypeVar("_T")
13+
14+
def remove_duplicates(items: Iterable[_T]) -> Iterator[_T]: ...

tests/stubtest_allowlists/py310.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ types.GenericAlias.__call__ # Would be complicated to fix properly, Any could s
2424
typing._SpecialForm.__mro_entries__
2525
weakref.ProxyType.__reversed__ # Doesn't really exist
2626

27-
# Modules that exist at runtime, but are missing from typeshed
28-
importlib.readers
29-
3027
# Modules that exist at runtime, but shouldn't be added to typeshed
3128
ctypes.test
3229
ctypes\.test\..+

tests/stubtest_allowlists/py311.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ typing.NewType.__call__
1818
typing.NewType.__mro_entries__
1919

2020
# Modules that exist at runtime, but are missing from typeshed
21-
importlib.readers
22-
importlib.resources.readers
2321
importlib.resources.simple
2422
importlib.simple
2523

tests/stubtest_allowlists/py312.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# Modules that exist at runtime, but are missing from typeshed
22
zipfile._path.glob
3-
importlib.readers
4-
importlib.resources.readers
53
importlib.resources.simple
64
importlib.simple
75

0 commit comments

Comments
 (0)