Skip to content

Commit 3fc57f8

Browse files
[3.12] gh-116608: undeprecate functional importlib.resources API (#132206)
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
1 parent 4c5e84d commit 3fc57f8

File tree

3 files changed

+18
-56
lines changed

3 files changed

+18
-56
lines changed

Doc/library/importlib.resources.rst

+17-34
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,10 @@ for example, a package and its resources can be imported from a zip file using
9999
Added support for *traversable* representing a directory.
100100

101101

102-
Deprecated functions
103-
^^^^^^^^^^^^^^^^^^^^
102+
Functional API
103+
^^^^^^^^^^^^^^
104104

105-
An older, deprecated set of functions is still available, but is
106-
scheduled for removal in a future version of Python.
105+
An older, previously deprecated set of functions is still available.
107106
The main drawback of these functions is that they do not support
108107
directories: they assume all resources are located directly within a *package*.
109108

@@ -116,8 +115,6 @@ directories: they assume all resources are located directly within a *package*.
116115

117116
The ``Package`` type is defined as ``Union[str, ModuleType]``.
118117

119-
.. deprecated:: 3.12
120-
121118

122119
.. data:: Resource
123120

@@ -138,11 +135,9 @@ directories: they assume all resources are located directly within a *package*.
138135
sub-resources (i.e. it cannot be a directory). This function returns a
139136
``typing.BinaryIO`` instance, a binary I/O stream open for reading.
140137

141-
.. deprecated:: 3.11
142-
143-
Calls to this function can be replaced by::
138+
This function is roughly equivalent to::
144139

145-
files(package).joinpath(resource).open('rb')
140+
files(package).joinpath(resource).open('rb')
146141

147142

148143
.. function:: open_text(package, resource, encoding='utf-8', errors='strict')
@@ -159,11 +154,9 @@ directories: they assume all resources are located directly within a *package*.
159154
This function returns a ``typing.TextIO`` instance, a text I/O stream open
160155
for reading.
161156

162-
.. deprecated:: 3.11
157+
This function is roughly equivalent to::
163158

164-
Calls to this function can be replaced by::
165-
166-
files(package).joinpath(resource).open('r', encoding=encoding)
159+
files(package).joinpath(resource).open('r', encoding=encoding)
167160

168161

169162
.. function:: read_binary(package, resource)
@@ -177,11 +170,9 @@ directories: they assume all resources are located directly within a *package*.
177170
sub-resources (i.e. it cannot be a directory). This function returns the
178171
contents of the resource as :class:`bytes`.
179172

180-
.. deprecated:: 3.11
181-
182-
Calls to this function can be replaced by::
173+
This function is roughly equivalent to::
183174

184-
files(package).joinpath(resource).read_bytes()
175+
files(package).joinpath(resource).read_bytes()
185176

186177

187178
.. function:: read_text(package, resource, encoding='utf-8', errors='strict')
@@ -196,11 +187,9 @@ directories: they assume all resources are located directly within a *package*.
196187
have the same meaning as with built-in :func:`open`. This function
197188
returns the contents of the resource as :class:`str`.
198189

199-
.. deprecated:: 3.11
190+
This function is roughly equivalent to::
200191

201-
Calls to this function can be replaced by::
202-
203-
files(package).joinpath(resource).read_text(encoding=encoding)
192+
files(package).joinpath(resource).read_text(encoding=encoding)
204193

205194

206195
.. function:: path(package, resource)
@@ -217,11 +206,9 @@ directories: they assume all resources are located directly within a *package*.
217206
within *package*; it may not contain path separators and it may not have
218207
sub-resources (i.e. it cannot be a directory).
219208

220-
.. deprecated:: 3.11
221-
222-
Calls to this function can be replaced using :func:`as_file`::
209+
This function is roughly equivalent to ::
223210

224-
as_file(files(package).joinpath(resource))
211+
as_file(files(package).joinpath(resource))
225212

226213

227214
.. function:: is_resource(package, name)
@@ -232,11 +219,9 @@ directories: they assume all resources are located directly within a *package*.
232219
*package* is either a name or a module object which conforms to the
233220
``Package`` requirements.
234221

235-
.. deprecated:: 3.11
222+
This function is roughly equivalent to::
236223

237-
Calls to this function can be replaced by::
238-
239-
files(package).joinpath(resource).is_file()
224+
files(package).joinpath(resource).is_file()
240225

241226

242227
.. function:: contents(package)
@@ -248,8 +233,6 @@ directories: they assume all resources are located directly within a *package*.
248233
*package* is either a name or a module object which conforms to the
249234
``Package`` requirements.
250235

251-
.. deprecated:: 3.11
252-
253-
Calls to this function can be replaced by::
236+
This function is roughly equivalent to::
254237

255-
(resource.name for resource in files(package).iterdir() if resource.is_file())
238+
(resource.name for resource in files(package).iterdir() if resource.is_file())

Lib/importlib/resources/_legacy.py

-22
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,6 @@
1212
Resource = str
1313

1414

15-
def deprecated(func):
16-
@functools.wraps(func)
17-
def wrapper(*args, **kwargs):
18-
warnings.warn(
19-
f"{func.__name__} is deprecated. Use files() instead. "
20-
"Refer to https://importlib-resources.readthedocs.io"
21-
"/en/latest/using.html#migrating-from-legacy for migration advice.",
22-
DeprecationWarning,
23-
stacklevel=2,
24-
)
25-
return func(*args, **kwargs)
26-
27-
return wrapper
28-
29-
3015
def normalize_path(path: Any) -> str:
3116
"""Normalize a path by ensuring it is a string.
3217
@@ -39,19 +24,16 @@ def normalize_path(path: Any) -> str:
3924
return file_name
4025

4126

42-
@deprecated
4327
def open_binary(package: Package, resource: Resource) -> BinaryIO:
4428
"""Return a file-like object opened for binary reading of the resource."""
4529
return (_common.files(package) / normalize_path(resource)).open('rb')
4630

4731

48-
@deprecated
4932
def read_binary(package: Package, resource: Resource) -> bytes:
5033
"""Return the binary contents of the resource."""
5134
return (_common.files(package) / normalize_path(resource)).read_bytes()
5235

5336

54-
@deprecated
5537
def open_text(
5638
package: Package,
5739
resource: Resource,
@@ -64,7 +46,6 @@ def open_text(
6446
)
6547

6648

67-
@deprecated
6849
def read_text(
6950
package: Package,
7051
resource: Resource,
@@ -80,7 +61,6 @@ def read_text(
8061
return fp.read()
8162

8263

83-
@deprecated
8464
def contents(package: Package) -> Iterable[str]:
8565
"""Return an iterable of entries in `package`.
8666
@@ -91,7 +71,6 @@ def contents(package: Package) -> Iterable[str]:
9171
return [path.name for path in _common.files(package).iterdir()]
9272

9373

94-
@deprecated
9574
def is_resource(package: Package, name: str) -> bool:
9675
"""True if `name` is a resource inside `package`.
9776
@@ -104,7 +83,6 @@ def is_resource(package: Package, name: str) -> bool:
10483
)
10584

10685

107-
@deprecated
10886
def path(
10987
package: Package,
11088
resource: Resource,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
undeprecate functional API for ``importlib.resources``

0 commit comments

Comments
 (0)