Skip to content

Commit 7bb786c

Browse files
committed
substitute many .format() with f-strings
1 parent ea17bb4 commit 7bb786c

4 files changed

Lines changed: 68 additions & 81 deletions

File tree

test/test_git.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,24 @@ def test_no_tags(self):
3636
with self.assertRaises(ValueError):
3737
query_git_repo(self.repo_path)
3838
version = predict_git_repo(self.repo_path)
39-
self.assertEqual(version.to_str(), '0.1.0.dev{}+{}'.format(i, self.repo_head_hexsha))
39+
self.assertEqual(version.to_str(), f'0.1.0.dev{i}+{self.repo_head_hexsha}')
4040

4141
def test_only_nonversion_tags(self):
4242
for i in range(1, 5):
4343
self.git_commit_new_file()
44-
self.repo.create_tag('commit_{}'.format(i))
44+
self.repo.create_tag(f'commit_{i}')
4545
with self.assertRaises(ValueError):
4646
query_git_repo(self.repo_path)
4747
version = predict_git_repo(self.repo_path)
48-
self.assertEqual(version.to_str(), '0.1.0.dev{}+{}'.format(i, self.repo_head_hexsha))
48+
self.assertEqual(version.to_str(), f'0.1.0.dev{i}+{self.repo_head_hexsha}')
4949

5050
def test_inconsistent_tag_prefix(self):
5151
version = Version.from_str('1.0')
5252
for _ in range(5):
5353
for version_tag_prefix in ('v', 'ver', ''):
5454
self.git_commit_new_file()
5555
version.increment(VersionComponent.Minor)
56-
self.repo.create_tag('{}{}'.format(version_tag_prefix, version))
56+
self.repo.create_tag(f'{version_tag_prefix}{version}')
5757
current_version = query_git_repo(self.repo_path)
5858
self.assertEqual(version, current_version)
5959
upcoming_version = predict_git_repo(self.repo_path)
@@ -62,7 +62,7 @@ def test_inconsistent_tag_prefix(self):
6262
def test_nonversion_tags(self):
6363
version = Version.from_str('0.1.0')
6464
self.git_commit_new_file()
65-
self.repo.create_tag('v{}'.format(version))
65+
self.repo.create_tag(f'v{version}')
6666
path = self.git_commit_new_file()
6767
self.git_modify_file(path, commit=True)
6868
self.repo.create_tag('release1')
@@ -104,17 +104,17 @@ def test_nonversion_merged_branches(self):
104104
with self.assertRaises(ValueError):
105105
query_git_repo(self.repo_path)
106106
upcoming_version = predict_git_repo(self.repo_path)
107-
self.assertEqual(upcoming_version.to_str(), '0.1.0.dev6+{}'.format(self.repo_head_hexsha))
107+
self.assertEqual(upcoming_version.to_str(), f'0.1.0.dev6+{self.repo_head_hexsha}')
108108

109109
def test_invalid_version_tags(self):
110110
for i in range(1, 3):
111111
self.git_commit_new_file()
112-
self.repo.create_tag('v{}.0.0'.format(i))
112+
self.repo.create_tag(f'v{i}.0.0')
113113
self.git_commit_new_file()
114-
self.repo.create_tag('version_{}.0.0'.format(i + 1))
114+
self.repo.create_tag(f'version_{i + 1}.0.0')
115115

116116
current_version = query_git_repo(self.repo_path)
117-
self.assertEqual(current_version.to_str(), '{}.0.0'.format(i))
117+
self.assertEqual(current_version.to_str(), f'{i}.0.0')
118118
upcoming_version = predict_git_repo(self.repo_path)
119119
current_version.devel_increment(1)
120120
current_version.local = (self.repo_head_hexsha,)
@@ -124,7 +124,7 @@ def test_dirty_repo(self):
124124
version = Version.from_str('0.9.0')
125125
for new_commit, add in itertools.product((False, True), (False, True)):
126126
path = self.git_commit_new_file()
127-
self.repo.create_tag('v{}'.format(version))
127+
self.repo.create_tag(f'v{version}')
128128
if new_commit:
129129
self.git_commit_new_file()
130130
self.git_modify_file(path, add=add)
@@ -136,7 +136,7 @@ def test_dirty_repo(self):
136136
current_version.devel_increment(1)
137137
current_version.local = (self.repo_head_hexsha,)
138138
self.assertTupleEqual(current_version.local, upcoming_version.local[:1])
139-
local_prefix = '+{}.dirty'.format(self.repo_head_hexsha)
139+
local_prefix = f'+{self.repo_head_hexsha}.dirty'
140140
else:
141141
local_prefix = '+dirty'
142142
self.assertTrue(upcoming_version.local_to_str().startswith(local_prefix),
@@ -158,12 +158,12 @@ def test_nonlatest_commit(self):
158158
current_version = query_git_repo(self.repo_path)
159159
self.assertEqual(current_version.to_str(), '0.1.0')
160160
upcoming_version = predict_git_repo(self.repo_path)
161-
self.assertEqual(upcoming_version.to_str(), '0.1.1.dev1+{}'.format(self.repo_head_hexsha))
161+
self.assertEqual(upcoming_version.to_str(), f'0.1.1.dev1+{self.repo_head_hexsha}')
162162
self.repo.git.checkout('devel')
163163
current_version = query_git_repo(self.repo_path)
164164
self.assertEqual(current_version.to_str(), '0.2.0')
165165
upcoming_version = predict_git_repo(self.repo_path)
166-
self.assertEqual(upcoming_version.to_str(), '0.2.1.dev1+{}'.format(self.repo_head_hexsha))
166+
self.assertEqual(upcoming_version.to_str(), f'0.2.1.dev1+{self.repo_head_hexsha}')
167167

168168
def test_tags_on_merged_branches(self):
169169
self.git_commit_new_file()
@@ -183,7 +183,7 @@ def test_tags_on_merged_branches(self):
183183
current_version = query_git_repo(self.repo_path)
184184
self.assertEqual(current_version.to_str(), '0.2.0')
185185
upcoming_version = predict_git_repo(self.repo_path)
186-
self.assertEqual(upcoming_version.to_str(), '0.2.1.dev5+{}'.format(self.repo_head_hexsha))
186+
self.assertEqual(upcoming_version.to_str(), f'0.2.1.dev5+{self.repo_head_hexsha}')
187187

188188
def test_tag_on_merged_branch(self):
189189
self.git_commit_new_file()
@@ -197,7 +197,7 @@ def test_tag_on_merged_branch(self):
197197
current_version = query_git_repo(self.repo_path)
198198
self.assertEqual(current_version.to_str(), '1.0.0')
199199
upcoming_version = predict_git_repo(self.repo_path)
200-
self.assertEqual(upcoming_version.to_str(), '1.0.1.dev1+{}'.format(self.repo_head_hexsha))
200+
self.assertEqual(upcoming_version.to_str(), f'1.0.1.dev1+{self.repo_head_hexsha}')
201201

202202
def test_many_versions_on_one_commit(self):
203203
self.git_commit_new_file()
@@ -208,7 +208,7 @@ def test_many_versions_on_one_commit(self):
208208
current_version = query_git_repo(self.repo_path)
209209
self.assertEqual(current_version.to_str(), '0.3.0')
210210
upcoming_version = predict_git_repo(self.repo_path)
211-
self.assertEqual(upcoming_version.to_str(), '0.3.1.dev1+{}'.format(self.repo_head_hexsha))
211+
self.assertEqual(upcoming_version.to_str(), f'0.3.1.dev1+{self.repo_head_hexsha}')
212212

213213
def test_version_decreased(self):
214214
self.git_commit_new_file()
@@ -219,4 +219,4 @@ def test_version_decreased(self):
219219
current_version = query_git_repo(self.repo_path)
220220
self.assertEqual(current_version.to_str(), '0.1.0')
221221
upcoming_version = predict_git_repo(self.repo_path)
222-
self.assertEqual(upcoming_version.to_str(), '0.1.1.dev1+{}'.format(self.repo_head_hexsha))
222+
self.assertEqual(upcoming_version.to_str(), f'0.1.1.dev1+{self.repo_head_hexsha}')

version_query/git_query.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def preprocess_git_version_tag(tag: str):
2020
return tag[1:]
2121
if tag and tag[0] in ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9'):
2222
return tag
23-
raise ValueError('given tag "{}" does not appear to be a version tag'.format(tag))
23+
raise ValueError(f'given tag "{tag}" does not appear to be a version tag')
2424

2525

2626
def _git_version_tags(repo: git.Repo) -> t.Mapping[git.Tag, Version]:
@@ -59,8 +59,8 @@ def _latest_git_version_tag_on_branches(
5959
results.append(result)
6060
if not results:
6161
if main_commit_distance is None:
62-
raise ValueError('reached max commit distance {} with no version tags in repo {}'
63-
.format(MAX_COMMIT_DISTANCE, repo))
62+
raise ValueError(f'reached max commit distance {MAX_COMMIT_DISTANCE}'
63+
f' with no version tags in repo {repo}')
6464
return main_commit_distance
6565
final_result = sorted(results, key=lambda _: _[2])[-1]
6666
_LOG.log(logging.NOTSET, 'result from %i branches is %s and %s',
@@ -99,8 +99,8 @@ def _latest_git_version_tag(
9999
_LOG.log(logging.NOTSET, 'found version data %s', current_version_tags)
100100
break
101101
if commit_distance >= MAX_COMMIT_DISTANCE:
102-
raise ValueError('reached max commit distance {} with no version tags in repo {}'
103-
.format(MAX_COMMIT_DISTANCE, repo))
102+
raise ValueError(f'reached max commit distance {MAX_COMMIT_DISTANCE}'
103+
f' with no version tags in repo {repo}')
104104
commit_distance += 1
105105
if len(commit.parents) <= 1:
106106
continue
@@ -113,7 +113,7 @@ def _latest_git_version_tag(
113113
if not current_version_tags:
114114
if assume_if_none:
115115
return commit, None, Version.from_str('0.1.0.dev0'), commit_distance
116-
raise ValueError('the given repo {} has no version tags'.format(repo))
116+
raise ValueError(f'the given repo {repo} has no version tags')
117117
tag, version = sorted(current_version_tags.items(), key=lambda _: _[1])[-1]
118118
_LOG.log(logging.NOTSET, 'result is %s and %s', tag, version)
119119
return commit, tag, version, commit_distance
@@ -142,7 +142,7 @@ def predict_git_repo(repo_path: pathlib.Path, search_parent_directories: bool =
142142
version.devel_increment(commit_distance)
143143
version.local = (repo.head.commit.hexsha[:8],)
144144
if is_repo_dirty:
145-
dt_ = 'dirty{}'.format(datetime.datetime.strftime(datetime.datetime.now(), '%Y%m%d%H%M%S'))
145+
dt_ = f'dirty{datetime.datetime.strftime(datetime.datetime.now(), "%Y%m%d%H%M%S")}'
146146
if version.has_local:
147147
assert version.local is not None # mypy needs this
148148
version.local = (*version.local, '.', dt_)

version_query/py_query.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ def query_package_folder(path: pathlib.Path, search_parent_directories: bool = F
3434
paths += path.parents
3535
metadata_json_paths, pkg_info_paths = None, None
3636
for pth in paths:
37-
metadata_json_paths = list(pth.parent.glob('{}*.dist-info/metadata.json'.format(pth.name)))
38-
pkg_info_paths = list(pth.parent.glob('{}*.egg-info/PKG-INFO'.format(pth.name)))
39-
pkg_info_paths += list(pth.parent.glob('{}*.dist-info/METADATA'.format(pth.name)))
37+
metadata_json_paths = list(pth.parent.glob(f'{pth.name}*.dist-info/metadata.json'))
38+
pkg_info_paths = list(pth.parent.glob(f'{pth.name}*.egg-info/PKG-INFO'))
39+
pkg_info_paths += list(pth.parent.glob(f'{pth.name}*.dist-info/METADATA'))
4040
if len(metadata_json_paths) == 1 and not pkg_info_paths:
4141
return query_metadata_json(metadata_json_paths[0])
4242
if not metadata_json_paths and len(pkg_info_paths) == 1:

0 commit comments

Comments
 (0)