modernize: use Python 3.8+ features

Follow-up to the py39-modernize work, covering features available
since Python 3.8 (the minimum supported version is 3.11):

- use the walrus operator (PEP 572) for assignment expressions where
  it removes a separate assign-then-test line and reads more clearly:
  - version.parse_version: inline the re.match result into the test
  - nanorst.rst_to_text: while char := text.read(1)
  - cockpit.runner read_stream: while line := await stream.readline()
  - tar_cmds._import_tar: while tarinfo := tar.next()

Most other Python 3.8 features are already in use (shlex.join, bare
@lru_cache, typing.Protocol/Literal), so this pass is small.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Thomas Waldmann 2026-06-14 17:35:20 +02:00
parent f5e5aa59f0
commit d5b3ffa980
4 changed files with 4 additions and 14 deletions

View file

@ -293,10 +293,7 @@ class TarMixIn:
tar = tarfile.open(fileobj=tarstream, mode="r|", ignore_zeros=args.ignore_zeros)
while True:
tarinfo = tar.next()
if not tarinfo:
break
while tarinfo := tar.next():
if tarinfo.isreg():
status = tfo.process_file(tarinfo=tarinfo, status="A", type=stat.S_IFREG, tar=tar)
archive.stats.nfiles += 1

View file

@ -44,10 +44,7 @@ class BorgRunner:
)
async def read_stream(stream, stream_name):
while True:
line = await stream.readline()
if not line:
break
while line := await stream.readline():
decoded_line = line.decode("utf-8", errors="replace").rstrip()
if decoded_line:
self.log_callback({"type": "stream_line", "stream": stream_name, "line": decoded_line})

View file

@ -65,10 +65,7 @@ def rst_to_text(text, state_hook=None, references=None):
inline_single = ("*", "`")
while True:
char = text.read(1)
if not char:
break
while char := text.read(1):
next = text.peek(1) # type: str
if state == "text":

View file

@ -19,8 +19,7 @@ def parse_version(version):
(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+) # version, e.g. 1.2.33
(?P<prerelease>\.?(?P<ptype>a|b|rc|dev)(?P<pnum>\d+))? # optional prerelease, e.g. a1 or b2 or rc33 or .dev1
"""
m = re.match(version_re, version, re.VERBOSE)
if m is None:
if (m := re.match(version_re, version, re.VERBOSE)) is None:
raise ValueError("Invalid version string %s" % version)
gd = m.groupdict()
version = [int(gd["major"]), int(gd["minor"]), int(gd["patch"])]