Skip to content

modernize: use Python 3.8+ features#9775

Merged
ThomasWaldmann merged 2 commits into
borgbackup:masterfrom
ThomasWaldmann:py38-modernize
Jun 14, 2026
Merged

modernize: use Python 3.8+ features#9775
ThomasWaldmann merged 2 commits into
borgbackup:masterfrom
ThomasWaldmann:py38-modernize

Conversation

@ThomasWaldmann

Copy link
Copy Markdown
Member

Follow-up to the py39-modernize work (#9774), covering features available since Python 3.8. This branch is stacked on py39-modernize, so it currently shows that commit too; it will resolve once #9774 merges.

Most Python 3.8 features are already in use in the codebase (shlex.join, bare @lru_cache, typing.Protocol/Literal), so the remaining lever is the walrus operator (PEP 572). Applied only to the clean, canonical cases where it removes a separate assign-then-test line:

  • version.parse_version: inline the re.match result into the is None 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()

An AST scan found ~150 x = f(); if <test x> sites, but the large majority reuse the variable later in the body, where walrus adds noise without removing the assignment — those were deliberately left alone.

Verified locally: version_test + nanorst_test pass; affected modules import cleanly.

🤖 Generated with Claude Code

ThomasWaldmann and others added 2 commits June 14, 2026 16:55
Follow-up to the py310-modernize work, covering features available
since Python 3.9 (the minimum supported version is 3.11):

- functools.cache instead of lru_cache(maxsize=None) (posix_ug, windows_ug)
- str.removeprefix() instead of slicing by len() (serve_cmd)
- PEP 585 built-in generics (list/dict/tuple/set/type) in the .pyi stubs
- collections.abc instead of typing for Callable/Iterator/Mapping/...
  in the .pyi stubs and runtime modules (chunkers, cockpit)
- PEP 604 union types (X | None) in crypto/low_level.pyi, which the
  py310-modernize pass had missed

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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>
@codecov

codecov Bot commented Jun 14, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.87%. Comparing base (ea4be4d) to head (d5b3ffa).
⚠️ Report is 22 commits behind head on master.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #9775      +/-   ##
==========================================
- Coverage   84.90%   84.87%   -0.04%     
==========================================
  Files          92       92              
  Lines       15166    15167       +1     
  Branches     2271     2271              
==========================================
- Hits        12877    12873       -4     
- Misses       1586     1590       +4     
- Partials      703      704       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@ThomasWaldmann ThomasWaldmann merged commit f31ee4c into borgbackup:master Jun 14, 2026
18 of 20 checks passed
@ThomasWaldmann ThomasWaldmann deleted the py38-modernize branch June 14, 2026 18:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant