Summary
Run ruff check --fix --select UP across the codebase to modernize Python syntax automatically. This is a low-risk, high-value cleanup that improves code consistency and readability.
Suggested by @bblommers in #1838:
"Speaking of: running ruff check --select UP seems to be mostly f-strings violations, which could be fixed automatically using ruff check --fix. I would be happy to add that as part of this PR - but I would recommend running that against the entire codebase in a separate PR for that. It makes reviewing a lot easier!"
What pyupgrade (UP) Does
The UP rule set modernizes Python syntax to use newer, cleaner constructs. Key transformations include:
String Formatting → f-strings
# Before (UP031, UP032)
"Hello, %s" % name
"Hello, {}".format(name)
# After
f"Hello, {name}"
Type Annotations → Modern Syntax
# Before (UP006, UP007)
from typing import Dict, List, Optional, Union
x: Optional[str]
y: Union[int, str]
z: Dict[str, List[int]]
# After
x: str | None
y: int | str
z: dict[str, list[int]]
Other Modernizations
| Rule |
Before |
After |
| UP005 |
assertEquals() |
assertEqual() |
| UP008 |
super(Foo, self) |
super() |
| UP009 |
# -*- coding: utf-8 -*- |
(removed, UTF-8 is default) |
| UP010 |
from __future__ import print_function |
(removed) |
| UP015 |
open("f", "r") |
open("f") |
| UP018 |
str("literal") |
"literal" |
| UP025 |
"unicode" encoding |
(simplified) |
| UP028 |
yield in for loop |
yield from |
| UP034 |
(x,) extraneous parens |
x, |
| UP035 |
typing.Dict |
dict |
| UP036 |
Version checks for old Python |
(removed) |
| UP038 |
isinstance(x, (A, B)) |
isinstance(x, A | B) |
Implementation
Step 1: Preview Changes
# See what would change (dry run)
ruff check --select UP src/autobahn/
Step 2: Auto-fix
# Apply fixes automatically
ruff check --fix --select UP src/autobahn/
Step 3: Manual Review
Some changes may need manual attention:
- Complex string formatting with many arguments
- Edge cases in type annotations
- Any changes that affect runtime behavior (rare but possible)
Step 4: Run Tests
Scope
In scope:
- All
.py files in src/autobahn/
- Automatic fixes via
ruff --fix
- Manual fixes for any remaining violations
Out of scope:
- Test files (can be done in follow-up)
- Changes that would affect public API signatures
- Any behavioral changes (this is purely syntactic)
Why a Separate PR?
As @bblommers noted, keeping this separate from typing work makes review much easier:
- Single concern — only modernization, no new types
- Mechanical changes — easy to verify correctness
- Clean diff — reviewers can focus on unexpected changes
- Bisectable — if issues arise, easy to identify source
Acceptance Criteria
Assignee
@bblommers has hinted at / offered to take this on — thank you, should you decide to invest time & work!
Related
References
Checklist
Summary
Run
ruff check --fix --select UPacross the codebase to modernize Python syntax automatically. This is a low-risk, high-value cleanup that improves code consistency and readability.Suggested by @bblommers in #1838:
What pyupgrade (UP) Does
The
UPrule set modernizes Python syntax to use newer, cleaner constructs. Key transformations include:String Formatting → f-strings
Type Annotations → Modern Syntax
Other Modernizations
assertEquals()assertEqual()super(Foo, self)super()# -*- coding: utf-8 -*-from __future__ import print_functionopen("f", "r")open("f")str("literal")"literal""unicode"encodingyieldinforloopyield from(x,)extraneous parensx,typing.Dictdictisinstance(x, (A, B))isinstance(x, A | B)Implementation
Step 1: Preview Changes
# See what would change (dry run) ruff check --select UP src/autobahn/Step 2: Auto-fix
# Apply fixes automatically ruff check --fix --select UP src/autobahn/Step 3: Manual Review
Some changes may need manual attention:
Step 4: Run Tests
just test cpy314Scope
In scope:
.pyfiles insrc/autobahn/ruff --fixOut of scope:
Why a Separate PR?
As @bblommers noted, keeping this separate from typing work makes review much easier:
Acceptance Criteria
ruff check --select UP src/autobahn/reports zero violationsjust test cpy314)Assignee
@bblommers has hinted at / offered to take this on — thank you, should you decide to invest time & work!
Related
References
Checklist