Skip to content

Commit f6fbdb9

Browse files
authored
bpo-38693: Prefer f-strings in importlib.resources (importlib_resources 5.0.6). (GH-26387)
Automerge-Triggered-By: GH:jaraco
1 parent 6cc800d commit f6fbdb9

File tree

6 files changed

+10
-12
lines changed

6 files changed

+10
-12
lines changed

Lib/importlib/_common.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def normalize_path(path):
3131
str_path = str(path)
3232
parent, file_name = os.path.split(str_path)
3333
if parent:
34-
raise ValueError('{!r} must be only a file name'.format(path))
34+
raise ValueError(f'{path!r} must be only a file name')
3535
return file_name
3636

3737

@@ -65,7 +65,7 @@ def get_package(package):
6565
"""
6666
resolved = resolve(package)
6767
if wrap_spec(resolved).submodule_search_locations is None:
68-
raise TypeError('{!r} is not a package'.format(package))
68+
raise TypeError(f'{package!r} is not a package')
6969
return resolved
7070

7171

Lib/importlib/readers.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,15 @@ def joinpath(self, child):
9494
__truediv__ = joinpath
9595

9696
def open(self, *args, **kwargs):
97-
raise FileNotFoundError('{} is not a file'.format(self))
97+
raise FileNotFoundError(f'{self} is not a file')
9898

9999
@property
100100
def name(self):
101101
return self._paths[0].name
102102

103103
def __repr__(self):
104-
return 'MultiplexedPath({})'.format(
105-
', '.join("'{}'".format(path) for path in self._paths)
106-
)
104+
paths = ', '.join(f"'{path}'" for path in self._paths)
105+
return f'MultiplexedPath({paths})'
107106

108107

109108
class NamespaceReader(abc.TraversableResources):

Lib/importlib/resources.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,7 @@ def open_binary(package: Package, resource: Resource) -> BinaryIO:
6868
if data is not None:
6969
return BytesIO(data)
7070

71-
raise FileNotFoundError(
72-
'{!r} resource not found in {!r}'.format(resource, spec.name)
73-
)
71+
raise FileNotFoundError(f'{resource!r} resource not found in {spec.name!r}')
7472

7573

7674
def open_text(

Lib/test/test_importlib/test_reader.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def test_join_path(self):
7979
def test_repr(self):
8080
self.assertEqual(
8181
repr(MultiplexedPath(self.folder)),
82-
"MultiplexedPath('{}')".format(self.folder),
82+
f"MultiplexedPath('{self.folder}')",
8383
)
8484

8585
def test_name(self):
@@ -121,7 +121,7 @@ def test_files(self):
121121
reader = NamespaceReader(namespacedata01.__spec__.submodule_search_locations)
122122
root = os.path.abspath(os.path.join(__file__, '..', 'namespacedata01'))
123123
self.assertIsInstance(reader.files(), MultiplexedPath)
124-
self.assertEqual(repr(reader.files()), "MultiplexedPath('{}')".format(root))
124+
self.assertEqual(repr(reader.files()), f"MultiplexedPath('{root}')")
125125

126126

127127
if __name__ == '__main__':

Lib/test/test_importlib/test_resource.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def setUp(self):
152152
data_path = pathlib.Path(self.ZIP_MODULE.__file__)
153153
data_dir = data_path.parent
154154
self.source_zip_path = data_dir / 'ziptestdata.zip'
155-
self.zip_path = pathlib.Path('{}.zip'.format(uuid.uuid4())).absolute()
155+
self.zip_path = pathlib.Path(f'{uuid.uuid4()}.zip').absolute()
156156
self.zip_path.write_bytes(self.source_zip_path.read_bytes())
157157
sys.path.append(str(self.zip_path))
158158
self.data = import_module('ziptestdata')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Prefer f-strings to ``.format`` in importlib.resources.

0 commit comments

Comments
 (0)