gh-116608: Apply style and compatibility changes from importlib_metad… · python/cpython@e913d2c
11import unittest
22import os
334-from test.support.warnings_helper import ignore_warnings, check_warnings
4+from test.support import warnings_helper
556-import importlib.resources as resources
6+from importlib import resources
7788# Since the functional API forwards to Traversable, we only test
99# filesystem resources here -- not zip files, namespace packages etc.
@@ -22,8 +22,7 @@ class ModuleAnchorMixin:
22222323class FunctionalAPIBase:
2424def _gen_resourcetxt_path_parts(self):
25-"""Yield various names of a text file in anchor02, each in a subTest
26- """
25+"""Yield various names of a text file in anchor02, each in a subTest"""
2726for path_parts in (
2827 ('subdirectory', 'subsubdir', 'resource.txt'),
2928 ('subdirectory/subsubdir/resource.txt',),
@@ -36,7 +35,7 @@ def assertEndsWith(self, string, suffix):
3635"""Assert that `string` ends with `suffix`.
37363837 Used to ignore an architecture-specific UTF-16 byte-order mark."""
39-self.assertEqual(string[-len(suffix):], suffix)
38+self.assertEqual(string[-len(suffix) :], suffix)
40394140def test_read_text(self):
4241self.assertEqual(
@@ -45,15 +44,20 @@ def test_read_text(self):
4544 )
4645self.assertEqual(
4746resources.read_text(
48-self.anchor02, 'subdirectory', 'subsubdir', 'resource.txt',
47+self.anchor02,
48+'subdirectory',
49+'subsubdir',
50+'resource.txt',
4951encoding='utf-8',
5052 ),
5153'a resource',
5254 )
5355for path_parts in self._gen_resourcetxt_path_parts():
5456self.assertEqual(
5557resources.read_text(
56-self.anchor02, *path_parts, encoding='utf-8',
58+self.anchor02,
59+*path_parts,
60+encoding='utf-8',
5761 ),
5862'a resource',
5963 )
@@ -67,13 +71,16 @@ def test_read_text(self):
6771resources.read_text(self.anchor01, 'utf-16.file')
6872self.assertEqual(
6973resources.read_text(
70-self.anchor01, 'binary.file', encoding='latin1',
74+self.anchor01,
75+'binary.file',
76+encoding='latin1',
7177 ),
7278'\x00\x01\x02\x03',
7379 )
7480self.assertEndsWith( # ignore the BOM
7581resources.read_text(
76-self.anchor01, 'utf-16.file',
82+self.anchor01,
83+'utf-16.file',
7784errors='backslashreplace',
7885 ),
7986'Hello, UTF-16 world!\n'.encode('utf-16-le').decode(
@@ -97,7 +104,8 @@ def test_open_text(self):
97104self.assertEqual(f.read(), 'Hello, UTF-8 world!\n')
98105for path_parts in self._gen_resourcetxt_path_parts():
99106with resources.open_text(
100-self.anchor02, *path_parts,
107+self.anchor02,
108+*path_parts,
101109encoding='utf-8',
102110 ) as f:
103111self.assertEqual(f.read(), 'a resource')
@@ -111,11 +119,14 @@ def test_open_text(self):
111119with self.assertRaises(UnicodeDecodeError):
112120f.read()
113121with resources.open_text(
114-self.anchor01, 'binary.file', encoding='latin1',
122+self.anchor01,
123+'binary.file',
124+encoding='latin1',
115125 ) as f:
116126self.assertEqual(f.read(), '\x00\x01\x02\x03')
117127with resources.open_text(
118-self.anchor01, 'utf-16.file',
128+self.anchor01,
129+'utf-16.file',
119130errors='backslashreplace',
120131 ) as f:
121132self.assertEndsWith( # ignore the BOM
@@ -130,16 +141,17 @@ def test_open_binary(self):
130141self.assertEqual(f.read(), b'Hello, UTF-8 world!\n')
131142for path_parts in self._gen_resourcetxt_path_parts():
132143with resources.open_binary(
133-self.anchor02, *path_parts,
144+self.anchor02,
145+*path_parts,
134146 ) as f:
135147self.assertEqual(f.read(), b'a resource')
136148137149def test_path(self):
138150with resources.path(self.anchor01, 'utf-8.file') as path:
139-with open(str(path)) as f:
151+with open(str(path), encoding='utf-8') as f:
140152self.assertEqual(f.read(), 'Hello, UTF-8 world!\n')
141153with resources.path(self.anchor01) as path:
142-with open(os.path.join(path, 'utf-8.file')) as f:
154+with open(os.path.join(path, 'utf-8.file'), encoding='utf-8') as f:
143155self.assertEqual(f.read(), 'Hello, UTF-8 world!\n')
144156145157def test_is_resource(self):
@@ -152,32 +164,32 @@ def test_is_resource(self):
152164self.assertTrue(is_resource(self.anchor02, *path_parts))
153165154166def test_contents(self):
155-is_resource = resources.is_resource
156-with check_warnings((".*contents.*", DeprecationWarning)):
167+with warnings_helper.check_warnings((".*contents.*", DeprecationWarning)):
157168c = resources.contents(self.anchor01)
158169self.assertGreaterEqual(
159170set(c),
160171 {'utf-8.file', 'utf-16.file', 'binary.file', 'subdirectory'},
161172 )
162-with (
163-self.assertRaises(OSError),
164-check_warnings((".*contents.*", DeprecationWarning)),
165- ):
173+with self.assertRaises(OSError), warnings_helper.check_warnings((
174+".*contents.*",
175+DeprecationWarning,
176+ )):
166177list(resources.contents(self.anchor01, 'utf-8.file'))
178+167179for path_parts in self._gen_resourcetxt_path_parts():
168-with (
169-self.assertRaises(OSError),
170-check_warnings((".*contents.*", DeprecationWarning)),
171- ):
180+with self.assertRaises(OSError), warnings_helper.check_warnings((
181+".*contents.*",
182+DeprecationWarning,
183+ )):
172184list(resources.contents(self.anchor01, *path_parts))
173-with check_warnings((".*contents.*", DeprecationWarning)):
185+with warnings_helper.check_warnings((".*contents.*", DeprecationWarning)):
174186c = resources.contents(self.anchor01, 'subdirectory')
175187self.assertGreaterEqual(
176188set(c),
177189 {'binary.file'},
178190 )
179191180-@ignore_warnings(category=DeprecationWarning)
192+@warnings_helper.ignore_warnings(category=DeprecationWarning)
181193def test_common_errors(self):
182194for func in (
183195resources.read_text,
@@ -208,18 +220,24 @@ def test_text_errors(self):
208220# Multiple path arguments need explicit encoding argument.
209221with self.assertRaises(TypeError):
210222func(
211-self.anchor02, 'subdirectory',
212-'subsubdir', 'resource.txt',
223+self.anchor02,
224+'subdirectory',
225+'subsubdir',
226+'resource.txt',
213227 )
214228215229216230class FunctionalAPITest_StringAnchor(
217-unittest.TestCase, FunctionalAPIBase, StringAnchorMixin,
231+unittest.TestCase,
232+FunctionalAPIBase,
233+StringAnchorMixin,
218234):
219235pass
220236221237222238class FunctionalAPITest_ModuleAnchor(
223-unittest.TestCase, FunctionalAPIBase, ModuleAnchorMixin,
239+unittest.TestCase,
240+FunctionalAPIBase,
241+ModuleAnchorMixin,
224242):
225243pass