Skip to content

Commit 8478101

Browse files
committed
run pyupgrade on version.py
1 parent ae2f358 commit 8478101

File tree

1 file changed

+21
-25
lines changed

1 file changed

+21
-25
lines changed

nbviewer/_version.py

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, env=
8282
stderr=(subprocess.PIPE if hide_stderr else None),
8383
)
8484
break
85-
except EnvironmentError:
85+
except OSError:
8686
e = sys.exc_info()[1]
8787
if e.errno == errno.ENOENT:
8888
continue
@@ -92,7 +92,7 @@ def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, env=
9292
return None, None
9393
else:
9494
if verbose:
95-
print("unable to find command, tried %s" % (commands,))
95+
print(f"unable to find command, tried {commands}")
9696
return None, None
9797
stdout = p.communicate()[0].strip().decode()
9898
if p.returncode != 0:
@@ -143,22 +143,21 @@ def git_get_keywords(versionfile_abs):
143143
# _version.py.
144144
keywords = {}
145145
try:
146-
f = open(versionfile_abs, "r")
147-
for line in f.readlines():
148-
if line.strip().startswith("git_refnames ="):
149-
mo = re.search(r'=\s*"(.*)"', line)
150-
if mo:
151-
keywords["refnames"] = mo.group(1)
152-
if line.strip().startswith("git_full ="):
153-
mo = re.search(r'=\s*"(.*)"', line)
154-
if mo:
155-
keywords["full"] = mo.group(1)
156-
if line.strip().startswith("git_date ="):
157-
mo = re.search(r'=\s*"(.*)"', line)
158-
if mo:
159-
keywords["date"] = mo.group(1)
160-
f.close()
161-
except EnvironmentError:
146+
with open(versionfile_abs) as f:
147+
for line in f.readlines():
148+
if line.strip().startswith("git_refnames ="):
149+
mo = re.search(r'=\s*"(.*)"', line)
150+
if mo:
151+
keywords["refnames"] = mo.group(1)
152+
if line.strip().startswith("git_full ="):
153+
mo = re.search(r'=\s*"(.*)"', line)
154+
if mo:
155+
keywords["full"] = mo.group(1)
156+
if line.strip().startswith("git_date ="):
157+
mo = re.search(r'=\s*"(.*)"', line)
158+
if mo:
159+
keywords["date"] = mo.group(1)
160+
except OSError:
162161
pass
163162
return keywords
164163

@@ -182,11 +181,11 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose):
182181
if verbose:
183182
print("keywords are unexpanded, not using")
184183
raise NotThisMethod("unexpanded keywords, not a git-archive tarball")
185-
refs = set([r.strip() for r in refnames.strip("()").split(",")])
184+
refs = {r.strip() for r in refnames.strip("()").split(",")}
186185
# starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of
187186
# just "foo-1.0". If we see a "tag: " prefix, prefer those.
188187
TAG = "tag: "
189-
tags = set([r[len(TAG) :] for r in refs if r.startswith(TAG)])
188+
tags = {r[len(TAG) :] for r in refs if r.startswith(TAG)}
190189
if not tags:
191190
# Either we're using git < 1.8.3, or there really are no tags. We use
192191
# a heuristic: assume all version tags have a digit. The old git %d
@@ -195,7 +194,7 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose):
195194
# between branches and tags. By ignoring refnames without digits, we
196195
# filter out many common branch names like "release" and
197196
# "stabilization", as well as "HEAD" and "master".
198-
tags = set([r for r in refs if re.search(r"\d", r)])
197+
tags = {r for r in refs if re.search(r"\d", r)}
199198
if verbose:
200199
print("discarding '%s', no digits" % ",".join(refs - tags))
201200
if verbose:
@@ -298,10 +297,7 @@ def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):
298297
if verbose:
299298
fmt = "tag '%s' doesn't start with prefix '%s'"
300299
print(fmt % (full_tag, tag_prefix))
301-
pieces["error"] = "tag '%s' doesn't start with prefix '%s'" % (
302-
full_tag,
303-
tag_prefix,
304-
)
300+
pieces["error"] = f"tag '{full_tag}' doesn't start with prefix '{tag_prefix}'"
305301
return pieces
306302
pieces["closest-tag"] = full_tag[len(tag_prefix) :]
307303

0 commit comments

Comments
 (0)