From d5b3ffa9802f52b77bfc03225c616b09e39cc487 Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Sun, 14 Jun 2026 17:35:20 +0200 Subject: [PATCH] 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 --- src/borg/archiver/tar_cmds.py | 5 +---- src/borg/cockpit/runner.py | 5 +---- src/borg/helpers/nanorst.py | 5 +---- src/borg/version.py | 3 +-- 4 files changed, 4 insertions(+), 14 deletions(-) diff --git a/src/borg/archiver/tar_cmds.py b/src/borg/archiver/tar_cmds.py index 73119e8ee..5c91ecb7c 100644 --- a/src/borg/archiver/tar_cmds.py +++ b/src/borg/archiver/tar_cmds.py @@ -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 diff --git a/src/borg/cockpit/runner.py b/src/borg/cockpit/runner.py index b32e56ad0..010d39449 100644 --- a/src/borg/cockpit/runner.py +++ b/src/borg/cockpit/runner.py @@ -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}) diff --git a/src/borg/helpers/nanorst.py b/src/borg/helpers/nanorst.py index 3d7c3316e..ea3ac2024 100644 --- a/src/borg/helpers/nanorst.py +++ b/src/borg/helpers/nanorst.py @@ -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": diff --git a/src/borg/version.py b/src/borg/version.py index 6371dfa76..a42b4e30e 100644 --- a/src/borg/version.py +++ b/src/borg/version.py @@ -19,8 +19,7 @@ def parse_version(version): (?P\d+)\.(?P\d+)\.(?P\d+) # version, e.g. 1.2.33 (?P\.?(?Pa|b|rc|dev)(?P\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"])]