bpo-45514: Deprecate importlib resources legacy functions. (GH-29036)

* bpo-45514: Apply changes from importlib_resources@a3ef4128c6

* Mark legacy functions as deprecated in the docs and link to the migration docs in importlib_resources docs.

* Apply changes from importlib_resources@329ae9d5f2c.

* Indicate importlib.resources as a module.

Co-authored-by: Filipe Laíns <lains@riseup.net>
This commit is contained in:
Jason R. Coombs 2021-11-24 02:51:37 -05:00 committed by GitHub
parent 324527012f
commit d5cd2effa6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 204 additions and 84 deletions

View File

@ -917,7 +917,9 @@ not** have to exist as physical files and directories on the file system.
on `using importlib.resources
<http://importlib-resources.readthedocs.io/en/latest/using.html>`_ and
`migrating from pkg_resources to importlib.resources
<http://importlib-resources.readthedocs.io/en/latest/migration.html>`_.
<http://importlib-resources.readthedocs.io/en/latest/migration.html>`_
and
`migrating legacy usage <https://importlib-resources.readthedocs.io/en/latest/using.html#migrating-from-legacy>`_.
Loaders that wish to support resource reading should implement a
``get_resource_reader(fullname)`` method as specified by
@ -979,6 +981,8 @@ The following functions are available.
sub-resources (i.e. it cannot be a directory). This function returns a
``typing.BinaryIO`` instance, a binary I/O stream open for reading.
.. deprecated:: 3.11
.. function:: open_text(package, resource, encoding='utf-8', errors='strict')
@ -994,6 +998,8 @@ The following functions are available.
This function returns a ``typing.TextIO`` instance, a text I/O stream open
for reading.
.. deprecated:: 3.11
.. function:: read_binary(package, resource)
@ -1006,6 +1012,8 @@ The following functions are available.
sub-resources (i.e. it cannot be a directory). This function returns the
contents of the resource as :class:`bytes`.
.. deprecated:: 3.11
.. function:: read_text(package, resource, encoding='utf-8', errors='strict')
@ -1019,6 +1027,8 @@ The following functions are available.
have the same meaning as with built-in :func:`open`. This function
returns the contents of the resource as :class:`str`.
.. deprecated:: 3.11
.. function:: path(package, resource)
@ -1034,6 +1044,8 @@ The following functions are available.
within *package*; it may not contain path separators and it may not have
sub-resources (i.e. it cannot be a directory).
.. deprecated:: 3.11
.. function:: is_resource(package, name)
@ -1042,6 +1054,8 @@ The following functions are available.
*package* is either a name or a module object which conforms to the
``Package`` requirements.
.. deprecated:: 3.11
.. function:: contents(package)
@ -1052,6 +1066,8 @@ The following functions are available.
*package* is either a name or a module object which conforms to the
``Package`` requirements.
.. deprecated:: 3.11
:mod:`importlib.machinery` -- Importers and path hooks
------------------------------------------------------

View File

@ -1,11 +1,27 @@
from itertools import filterfalse
from typing import (
Callable,
Iterable,
Iterator,
Optional,
Set,
TypeVar,
Union,
)
def unique_everseen(iterable, key=None):
# Type and type variable definitions
_T = TypeVar('_T')
_U = TypeVar('_U')
def unique_everseen(
iterable: Iterable[_T], key: Optional[Callable[[_T], _U]] = None
) -> Iterator[_T]:
"List unique elements, preserving order. Remember all elements ever seen."
# unique_everseen('AAAABBBCCDAABBB') --> A B C D
# unique_everseen('ABBCcAD', str.lower) --> A B C D
seen = set()
seen: Set[Union[_T, _U]] = set()
seen_add = seen.add
if key is None:
for element in filterfalse(seen.__contains__, iterable):

View File

@ -1,6 +1,8 @@
import functools
import os
import pathlib
import types
import warnings
from typing import Union, Iterable, ContextManager, BinaryIO, TextIO
@ -10,16 +12,34 @@ Package = Union[types.ModuleType, str]
Resource = Union[str, os.PathLike]
def deprecated(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
warnings.warn(
f"{func.__name__} is deprecated. Use files() instead. "
"Refer to https://importlib-resources.readthedocs.io"
"/en/latest/using.html#migrating-from-legacy for migration advice.",
DeprecationWarning,
stacklevel=2,
)
return func(*args, **kwargs)
return wrapper
@deprecated
def open_binary(package: Package, resource: Resource) -> BinaryIO:
"""Return a file-like object opened for binary reading of the resource."""
return (_common.files(package) / _common.normalize_path(resource)).open('rb')
@deprecated
def read_binary(package: Package, resource: Resource) -> bytes:
"""Return the binary contents of the resource."""
return (_common.files(package) / _common.normalize_path(resource)).read_bytes()
@deprecated
def open_text(
package: Package,
resource: Resource,
@ -32,6 +52,7 @@ def open_text(
)
@deprecated
def read_text(
package: Package,
resource: Resource,
@ -47,6 +68,7 @@ def read_text(
return fp.read()
@deprecated
def contents(package: Package) -> Iterable[str]:
"""Return an iterable of entries in `package`.
@ -57,6 +79,7 @@ def contents(package: Package) -> Iterable[str]:
return [path.name for path in _common.files(package).iterdir()]
@deprecated
def is_resource(package: Package, name: str) -> bool:
"""True if `name` is a resource inside `package`.
@ -69,6 +92,7 @@ def is_resource(package: Package, name: str) -> bool:
)
@deprecated
def path(
package: Package,
resource: Resource,

View File

@ -1,8 +1,10 @@
import abc
import contextlib
import importlib
import io
import sys
import types
import warnings
from pathlib import Path, PurePath
from .. import data01
@ -67,6 +69,13 @@ def create_package(file=None, path=None, is_package=True, contents=()):
)
@contextlib.contextmanager
def suppress_known_deprecation():
with warnings.catch_warnings(record=True) as ctx:
warnings.simplefilter('default', category=DeprecationWarning)
yield ctx
class CommonTests(metaclass=abc.ABCMeta):
"""
Tests shared by test_open, test_path, and test_read.

View File

@ -15,6 +15,7 @@ class ContentsTests:
}
def test_contents(self):
with util.suppress_known_deprecation():
assert self.expected <= set(resources.contents(self.data))

View File

@ -7,36 +7,45 @@ from .resources import util
class CommonBinaryTests(util.CommonTests, unittest.TestCase):
def execute(self, package, path):
with util.suppress_known_deprecation():
with resources.open_binary(package, path):
pass
class CommonTextTests(util.CommonTests, unittest.TestCase):
def execute(self, package, path):
with util.suppress_known_deprecation():
with resources.open_text(package, path):
pass
class OpenTests:
def test_open_binary(self):
with util.suppress_known_deprecation():
with resources.open_binary(self.data, 'binary.file') as fp:
result = fp.read()
self.assertEqual(result, b'\x00\x01\x02\x03')
def test_open_text_default_encoding(self):
with util.suppress_known_deprecation():
with resources.open_text(self.data, 'utf-8.file') as fp:
result = fp.read()
self.assertEqual(result, 'Hello, UTF-8 world!\n')
def test_open_text_given_encoding(self):
with resources.open_text(self.data, 'utf-16.file', 'utf-16', 'strict') as fp:
with util.suppress_known_deprecation():
with resources.open_text(
self.data, 'utf-16.file', 'utf-16', 'strict'
) as fp:
result = fp.read()
self.assertEqual(result, 'Hello, UTF-16 world!\n')
def test_open_text_with_errors(self):
# Raises UnicodeError without the 'errors' argument.
with util.suppress_known_deprecation():
with resources.open_text(self.data, 'utf-16.file', 'utf-8', 'strict') as fp:
self.assertRaises(UnicodeError, fp.read)
with util.suppress_known_deprecation():
with resources.open_text(self.data, 'utf-16.file', 'utf-8', 'ignore') as fp:
result = fp.read()
self.assertEqual(
@ -47,11 +56,13 @@ class OpenTests:
)
def test_open_binary_FileNotFoundError(self):
with util.suppress_known_deprecation():
self.assertRaises(
FileNotFoundError, resources.open_binary, self.data, 'does-not-exist'
)
def test_open_text_FileNotFoundError(self):
with util.suppress_known_deprecation():
self.assertRaises(
FileNotFoundError, resources.open_text, self.data, 'does-not-exist'
)

View File

@ -8,6 +8,7 @@ from .resources import util
class CommonTests(util.CommonTests, unittest.TestCase):
def execute(self, package, path):
with util.suppress_known_deprecation():
with resources.path(package, path):
pass
@ -17,6 +18,7 @@ class PathTests:
# Path should be readable.
# Test also implicitly verifies the returned object is a pathlib.Path
# instance.
with util.suppress_known_deprecation():
with resources.path(self.data, 'utf-8.file') as path:
self.assertTrue(path.name.endswith("utf-8.file"), repr(path))
# pathlib.Path.read_text() was introduced in Python 3.5.
@ -32,6 +34,7 @@ class PathDiskTests(PathTests, unittest.TestCase):
# Guarantee the internal implementation detail that
# file-system-backed resources do not get the tempdir
# treatment.
with util.suppress_known_deprecation():
with resources.path(self.data, 'utf-8.file') as path:
assert 'data' in str(path)
@ -51,6 +54,7 @@ class PathZipTests(PathTests, util.ZipSetup, unittest.TestCase):
def test_remove_in_context_manager(self):
# It is not an error if the file that was temporarily stashed on the
# file system is removed inside the `with` stanza.
with util.suppress_known_deprecation():
with resources.path(self.data, 'utf-8.file') as path:
path.unlink()

View File

@ -7,30 +7,39 @@ from .resources import util
class CommonBinaryTests(util.CommonTests, unittest.TestCase):
def execute(self, package, path):
with util.suppress_known_deprecation():
resources.read_binary(package, path)
class CommonTextTests(util.CommonTests, unittest.TestCase):
def execute(self, package, path):
with util.suppress_known_deprecation():
resources.read_text(package, path)
class ReadTests:
def test_read_binary(self):
with util.suppress_known_deprecation():
result = resources.read_binary(self.data, 'binary.file')
self.assertEqual(result, b'\0\1\2\3')
def test_read_text_default_encoding(self):
with util.suppress_known_deprecation():
result = resources.read_text(self.data, 'utf-8.file')
self.assertEqual(result, 'Hello, UTF-8 world!\n')
def test_read_text_given_encoding(self):
with util.suppress_known_deprecation():
result = resources.read_text(self.data, 'utf-16.file', encoding='utf-16')
self.assertEqual(result, 'Hello, UTF-16 world!\n')
def test_read_text_with_errors(self):
# Raises UnicodeError without the 'errors' argument.
self.assertRaises(UnicodeError, resources.read_text, self.data, 'utf-16.file')
with util.suppress_known_deprecation():
self.assertRaises(
UnicodeError, resources.read_text, self.data, 'utf-16.file'
)
with util.suppress_known_deprecation():
result = resources.read_text(self.data, 'utf-16.file', errors='ignore')
self.assertEqual(
result,
@ -47,10 +56,12 @@ class ReadDiskTests(ReadTests, unittest.TestCase):
class ReadZipTests(ReadTests, util.ZipSetup, unittest.TestCase):
def test_read_submodule_resource(self):
submodule = import_module('ziptestdata.subdirectory')
with util.suppress_known_deprecation():
result = resources.read_binary(submodule, 'binary.file')
self.assertEqual(result, b'\0\1\2\3')
def test_read_submodule_resource_by_name(self):
with util.suppress_known_deprecation():
result = resources.read_binary('ziptestdata.subdirectory', 'binary.file')
self.assertEqual(result, b'\0\1\2\3')

View File

@ -15,16 +15,20 @@ class ResourceTests:
# Subclasses are expected to set the `data` attribute.
def test_is_resource_good_path(self):
with util.suppress_known_deprecation():
self.assertTrue(resources.is_resource(self.data, 'binary.file'))
def test_is_resource_missing(self):
with util.suppress_known_deprecation():
self.assertFalse(resources.is_resource(self.data, 'not-a-file'))
def test_is_resource_subresource_directory(self):
# Directories are not resources.
with util.suppress_known_deprecation():
self.assertFalse(resources.is_resource(self.data, 'subdirectory'))
def test_contents(self):
with util.suppress_known_deprecation():
contents = set(resources.contents(self.data))
# There may be cruft in the directory listing of the data directory.
# It could have a __pycache__ directory,
@ -58,24 +62,28 @@ class ResourceLoaderTests(unittest.TestCase):
package = util.create_package(
file=data01, path=data01.__file__, contents=['A', 'B', 'C']
)
with util.suppress_known_deprecation():
self.assertEqual(set(resources.contents(package)), {'A', 'B', 'C'})
def test_resource_is_resource(self):
package = util.create_package(
file=data01, path=data01.__file__, contents=['A', 'B', 'C', 'D/E', 'D/F']
)
with util.suppress_known_deprecation():
self.assertTrue(resources.is_resource(package, 'B'))
def test_resource_directory_is_not_resource(self):
package = util.create_package(
file=data01, path=data01.__file__, contents=['A', 'B', 'C', 'D/E', 'D/F']
)
with util.suppress_known_deprecation():
self.assertFalse(resources.is_resource(package, 'D'))
def test_resource_missing_is_not_resource(self):
package = util.create_package(
file=data01, path=data01.__file__, contents=['A', 'B', 'C', 'D/E', 'D/F']
)
with util.suppress_known_deprecation():
self.assertFalse(resources.is_resource(package, 'Z'))
@ -94,6 +102,7 @@ class ResourceCornerCaseTests(unittest.TestCase):
module.__file__ = '/path/which/shall/not/be/named'
module.__spec__.loader = module.__loader__
module.__spec__.origin = module.__file__
with util.suppress_known_deprecation():
self.assertFalse(resources.is_resource(module, 'A'))
@ -102,20 +111,24 @@ class ResourceFromZipsTest01(util.ZipSetupBase, unittest.TestCase):
def test_is_submodule_resource(self):
submodule = import_module('ziptestdata.subdirectory')
with util.suppress_known_deprecation():
self.assertTrue(resources.is_resource(submodule, 'binary.file'))
def test_read_submodule_resource_by_name(self):
with util.suppress_known_deprecation():
self.assertTrue(
resources.is_resource('ziptestdata.subdirectory', 'binary.file')
)
def test_submodule_contents(self):
submodule = import_module('ziptestdata.subdirectory')
with util.suppress_known_deprecation():
self.assertEqual(
set(resources.contents(submodule)), {'__init__.py', 'binary.file'}
)
def test_submodule_contents_by_name(self):
with util.suppress_known_deprecation():
self.assertEqual(
set(resources.contents('ziptestdata.subdirectory')),
{'__init__.py', 'binary.file'},
@ -130,11 +143,15 @@ class ResourceFromZipsTest02(util.ZipSetupBase, unittest.TestCase):
Test thata zip with two unrelated subpackages return
distinct resources. Ref python/importlib_resources#44.
"""
with util.suppress_known_deprecation():
self.assertEqual(
set(resources.contents('ziptestdata.one')), {'__init__.py', 'resource1.txt'}
set(resources.contents('ziptestdata.one')),
{'__init__.py', 'resource1.txt'},
)
with util.suppress_known_deprecation():
self.assertEqual(
set(resources.contents('ziptestdata.two')), {'__init__.py', 'resource2.txt'}
set(resources.contents('ziptestdata.two')),
{'__init__.py', 'resource2.txt'},
)
@ -176,16 +193,19 @@ class DeletingZipsTest(unittest.TestCase):
pass
def test_contents_does_not_keep_open(self):
with util.suppress_known_deprecation():
c = resources.contents('ziptestdata')
self.zip_path.unlink()
del c
def test_is_resource_does_not_keep_open(self):
with util.suppress_known_deprecation():
c = resources.is_resource('ziptestdata', 'binary.file')
self.zip_path.unlink()
del c
def test_is_resource_failure_does_not_keep_open(self):
with util.suppress_known_deprecation():
c = resources.is_resource('ziptestdata', 'not-present')
self.zip_path.unlink()
del c
@ -199,16 +219,19 @@ class DeletingZipsTest(unittest.TestCase):
def test_entered_path_does_not_keep_open(self):
# This is what certifi does on import to make its bundle
# available for the process duration.
with util.suppress_known_deprecation():
c = resources.path('ziptestdata', 'binary.file').__enter__()
self.zip_path.unlink()
del c
def test_read_binary_does_not_keep_open(self):
with util.suppress_known_deprecation():
c = resources.read_binary('ziptestdata', 'binary.file')
self.zip_path.unlink()
del c
def test_read_text_does_not_keep_open(self):
with util.suppress_known_deprecation():
c = resources.read_text('ziptestdata', 'utf-8.file', encoding='utf-8')
self.zip_path.unlink()
del c
@ -226,14 +249,17 @@ class ResourceFromNamespaceTest01(unittest.TestCase):
sys.path.remove(cls.site_dir)
def test_is_submodule_resource(self):
with util.suppress_known_deprecation():
self.assertTrue(
resources.is_resource(import_module('namespacedata01'), 'binary.file')
)
def test_read_submodule_resource_by_name(self):
with util.suppress_known_deprecation():
self.assertTrue(resources.is_resource('namespacedata01', 'binary.file'))
def test_submodule_contents(self):
with util.suppress_known_deprecation():
contents = set(resources.contents(import_module('namespacedata01')))
try:
contents.remove('__pycache__')
@ -242,6 +268,7 @@ class ResourceFromNamespaceTest01(unittest.TestCase):
self.assertEqual(contents, {'binary.file', 'utf-8.file', 'utf-16.file'})
def test_submodule_contents_by_name(self):
with util.suppress_known_deprecation():
contents = set(resources.contents('namespacedata01'))
try:
contents.remove('__pycache__')

View File

@ -0,0 +1 @@
Deprecated legacy functions in :mod:`importlib.resources`.