https://github.com/python/cpython/commit/e913d2c87f1ae4e7a4aef5ba78368ef31d060767
commit: e913d2c87f1ae4e7a4aef5ba78368ef31d060767
branch: main
author: Jason R. Coombs <[email protected]>
committer: jaraco <[email protected]>
date: 2024-08-15T15:32:05-04:00
summary:
gh-116608: Apply style and compatibility changes from importlib_metadata.
(#123028)
files:
M Lib/importlib/resources/_functional.py
M Lib/test/test_importlib/resources/test_functional.py
diff --git a/Lib/importlib/resources/_functional.py
b/Lib/importlib/resources/_functional.py
index 9e3ea1547d486a..f59416f2dd627d 100644
--- a/Lib/importlib/resources/_functional.py
+++ b/Lib/importlib/resources/_functional.py
@@ -57,11 +57,7 @@ def contents(anchor, *path_names):
DeprecationWarning,
stacklevel=1,
)
- return (
- resource.name
- for resource
- in _get_resource(anchor, path_names).iterdir()
- )
+ return (resource.name for resource in _get_resource(anchor,
path_names).iterdir())
def _get_encoding_arg(path_names, encoding):
diff --git a/Lib/test/test_importlib/resources/test_functional.py
b/Lib/test/test_importlib/resources/test_functional.py
index d60a2bedd89798..f65ffe646cf0d4 100644
--- a/Lib/test/test_importlib/resources/test_functional.py
+++ b/Lib/test/test_importlib/resources/test_functional.py
@@ -1,9 +1,9 @@
import unittest
import os
-from test.support.warnings_helper import ignore_warnings, check_warnings
+from test.support import warnings_helper
-import importlib.resources as resources
+from importlib import resources
# Since the functional API forwards to Traversable, we only test
# filesystem resources here -- not zip files, namespace packages etc.
@@ -22,8 +22,7 @@ class ModuleAnchorMixin:
class FunctionalAPIBase:
def _gen_resourcetxt_path_parts(self):
- """Yield various names of a text file in anchor02, each in a subTest
- """
+ """Yield various names of a text file in anchor02, each in a subTest"""
for path_parts in (
('subdirectory', 'subsubdir', 'resource.txt'),
('subdirectory/subsubdir/resource.txt',),
@@ -36,7 +35,7 @@ def assertEndsWith(self, string, suffix):
"""Assert that `string` ends with `suffix`.
Used to ignore an architecture-specific UTF-16 byte-order mark."""
- self.assertEqual(string[-len(suffix):], suffix)
+ self.assertEqual(string[-len(suffix) :], suffix)
def test_read_text(self):
self.assertEqual(
@@ -45,7 +44,10 @@ def test_read_text(self):
)
self.assertEqual(
resources.read_text(
- self.anchor02, 'subdirectory', 'subsubdir', 'resource.txt',
+ self.anchor02,
+ 'subdirectory',
+ 'subsubdir',
+ 'resource.txt',
encoding='utf-8',
),
'a resource',
@@ -53,7 +55,9 @@ def test_read_text(self):
for path_parts in self._gen_resourcetxt_path_parts():
self.assertEqual(
resources.read_text(
- self.anchor02, *path_parts, encoding='utf-8',
+ self.anchor02,
+ *path_parts,
+ encoding='utf-8',
),
'a resource',
)
@@ -67,13 +71,16 @@ def test_read_text(self):
resources.read_text(self.anchor01, 'utf-16.file')
self.assertEqual(
resources.read_text(
- self.anchor01, 'binary.file', encoding='latin1',
+ self.anchor01,
+ 'binary.file',
+ encoding='latin1',
),
'\x00\x01\x02\x03',
)
self.assertEndsWith( # ignore the BOM
resources.read_text(
- self.anchor01, 'utf-16.file',
+ self.anchor01,
+ 'utf-16.file',
errors='backslashreplace',
),
'Hello, UTF-16 world!\n'.encode('utf-16-le').decode(
@@ -97,7 +104,8 @@ def test_open_text(self):
self.assertEqual(f.read(), 'Hello, UTF-8 world!\n')
for path_parts in self._gen_resourcetxt_path_parts():
with resources.open_text(
- self.anchor02, *path_parts,
+ self.anchor02,
+ *path_parts,
encoding='utf-8',
) as f:
self.assertEqual(f.read(), 'a resource')
@@ -111,11 +119,14 @@ def test_open_text(self):
with self.assertRaises(UnicodeDecodeError):
f.read()
with resources.open_text(
- self.anchor01, 'binary.file', encoding='latin1',
+ self.anchor01,
+ 'binary.file',
+ encoding='latin1',
) as f:
self.assertEqual(f.read(), '\x00\x01\x02\x03')
with resources.open_text(
- self.anchor01, 'utf-16.file',
+ self.anchor01,
+ 'utf-16.file',
errors='backslashreplace',
) as f:
self.assertEndsWith( # ignore the BOM
@@ -130,16 +141,17 @@ def test_open_binary(self):
self.assertEqual(f.read(), b'Hello, UTF-8 world!\n')
for path_parts in self._gen_resourcetxt_path_parts():
with resources.open_binary(
- self.anchor02, *path_parts,
+ self.anchor02,
+ *path_parts,
) as f:
self.assertEqual(f.read(), b'a resource')
def test_path(self):
with resources.path(self.anchor01, 'utf-8.file') as path:
- with open(str(path)) as f:
+ with open(str(path), encoding='utf-8') as f:
self.assertEqual(f.read(), 'Hello, UTF-8 world!\n')
with resources.path(self.anchor01) as path:
- with open(os.path.join(path, 'utf-8.file')) as f:
+ with open(os.path.join(path, 'utf-8.file'), encoding='utf-8') as f:
self.assertEqual(f.read(), 'Hello, UTF-8 world!\n')
def test_is_resource(self):
@@ -152,32 +164,32 @@ def test_is_resource(self):
self.assertTrue(is_resource(self.anchor02, *path_parts))
def test_contents(self):
- is_resource = resources.is_resource
- with check_warnings((".*contents.*", DeprecationWarning)):
+ with warnings_helper.check_warnings((".*contents.*",
DeprecationWarning)):
c = resources.contents(self.anchor01)
self.assertGreaterEqual(
set(c),
{'utf-8.file', 'utf-16.file', 'binary.file', 'subdirectory'},
)
- with (
- self.assertRaises(OSError),
- check_warnings((".*contents.*", DeprecationWarning)),
- ):
+ with self.assertRaises(OSError), warnings_helper.check_warnings((
+ ".*contents.*",
+ DeprecationWarning,
+ )):
list(resources.contents(self.anchor01, 'utf-8.file'))
+
for path_parts in self._gen_resourcetxt_path_parts():
- with (
- self.assertRaises(OSError),
- check_warnings((".*contents.*", DeprecationWarning)),
- ):
+ with self.assertRaises(OSError), warnings_helper.check_warnings((
+ ".*contents.*",
+ DeprecationWarning,
+ )):
list(resources.contents(self.anchor01, *path_parts))
- with check_warnings((".*contents.*", DeprecationWarning)):
+ with warnings_helper.check_warnings((".*contents.*",
DeprecationWarning)):
c = resources.contents(self.anchor01, 'subdirectory')
self.assertGreaterEqual(
set(c),
{'binary.file'},
)
- @ignore_warnings(category=DeprecationWarning)
+ @warnings_helper.ignore_warnings(category=DeprecationWarning)
def test_common_errors(self):
for func in (
resources.read_text,
@@ -208,18 +220,24 @@ def test_text_errors(self):
# Multiple path arguments need explicit encoding argument.
with self.assertRaises(TypeError):
func(
- self.anchor02, 'subdirectory',
- 'subsubdir', 'resource.txt',
+ self.anchor02,
+ 'subdirectory',
+ 'subsubdir',
+ 'resource.txt',
)
class FunctionalAPITest_StringAnchor(
- unittest.TestCase, FunctionalAPIBase, StringAnchorMixin,
+ unittest.TestCase,
+ FunctionalAPIBase,
+ StringAnchorMixin,
):
pass
class FunctionalAPITest_ModuleAnchor(
- unittest.TestCase, FunctionalAPIBase, ModuleAnchorMixin,
+ unittest.TestCase,
+ FunctionalAPIBase,
+ ModuleAnchorMixin,
):
pass
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]