modernize: use Python 3.8+ features#9775
Merged
Merged
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 there.matchresult into theis Nonetestnanorst.rst_to_text:while char := text.read(1)cockpit.runnerread_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_testpass; affected modules import cleanly.🤖 Generated with Claude Code