Skip to content

Commit a66bb52

Browse files
committed
[fix] ruff linter - rewrite map comprehensions for improved readability
1 parent 3342b8b commit a66bb52

3 files changed

Lines changed: 18 additions & 25 deletions

File tree

src/xspct_db/routes.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -963,9 +963,7 @@ async def post(self) -> r200[QueryResponse] | r401[ErrorResponse] | r500[ErrorRe
963963
# Apply rewrite rules then prefilter on the canonical (rewritten) addresses.
964964
# rewrite_map: original -> canonical; originals are the response keys.
965965
rewrite_rules = self.request.app.get("rewrite_rules", [])
966-
rewrite_map: dict[str, str] = {
967-
u: rewrite.apply_rewrite_rules(u, rewrite_rules) for u in query_req.users
968-
}
966+
rewrite_map: dict[str, str] = {u: rewrite.apply_rewrite_rules(u, rewrite_rules) for u in query_req.users}
969967
# Deduplicated canonical addresses passed through prefilter.
970968
canonical_users = list(dict.fromkeys(rewrite_map.values()))
971969
filtered_canonical = prefilter.filter_addresses(s, canonical_users, self.request.app)
@@ -1043,9 +1041,7 @@ async def _qj_task() -> tuple[bytes, str, str | bool]:
10431041
if missing:
10441042
# Map each missing original address to the wildcard key
10451043
# derived from its canonical rewritten address.
1046-
addr_to_wks = {
1047-
u: _compute_wildcard_keys(rewrite_map[u], wildcard_queries) for u in missing
1048-
}
1044+
addr_to_wks = {u: _compute_wildcard_keys(rewrite_map[u], wildcard_queries) for u in missing}
10491045
addr_to_wks = {u: wks for u, wks in addr_to_wks.items() if wks}
10501046
unique_domain_keys = list(dict.fromkeys(dk for wks in addr_to_wks.values() for dk in wks))
10511047
dom_users = [
@@ -1208,9 +1204,7 @@ async def post(self) -> r200[RspamdSettingsResponse] | r401[ErrorResponse]:
12081204
# the canonical address. Original addresses are kept as response keys.
12091205
raw_addresses = list(dict.fromkeys(addr for addr in ([rspamd_req.from_addr] + rspamd_req.rcpts) if addr))
12101206
rs_rewrite_rules = self.request.app.get("rewrite_rules", [])
1211-
rs_rewrite_map: dict[str, str] = {
1212-
addr: rewrite.apply_rewrite_rules(addr, rs_rewrite_rules) for addr in raw_addresses
1213-
}
1207+
rs_rewrite_map: dict[str, str] = {addr: rewrite.apply_rewrite_rules(addr, rs_rewrite_rules) for addr in raw_addresses}
12141208
canonical_addresses = list(dict.fromkeys(rs_rewrite_map.values()))
12151209
canonical_addresses = prefilter.filter_addresses(s, canonical_addresses, self.request.app)
12161210
filtered_canonical_set = set(canonical_addresses)
@@ -1255,10 +1249,7 @@ async def _rs_task() -> tuple[bytes, str]:
12551249
if wildcard_queries:
12561250
missing = [addr for addr in addresses if addr not in user_to_pkey]
12571251
if missing:
1258-
addr_to_wks = {
1259-
addr: _compute_wildcard_keys(rs_rewrite_map[addr], wildcard_queries)
1260-
for addr in missing
1261-
}
1252+
addr_to_wks = {addr: _compute_wildcard_keys(rs_rewrite_map[addr], wildcard_queries) for addr in missing}
12621253
addr_to_wks = {addr: wks for addr, wks in addr_to_wks.items() if wks}
12631254
unique_domain_keys = list(dict.fromkeys(dk for wks in addr_to_wks.values() for dk in wks))
12641255
dom_users = [

tests/conftest.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,9 +320,7 @@ async def rewrite_yaml_app_client(rewrite_yaml_cfg: dict[str, Any], aiohttp_clie
320320

321321

322322
@pytest.fixture
323-
async def rewrite_wildcard_yaml_app_client(
324-
rewrite_wildcard_yaml_cfg: dict[str, Any], aiohttp_client: Any
325-
) -> TestClient:
323+
async def rewrite_wildcard_yaml_app_client(rewrite_wildcard_yaml_cfg: dict[str, Any], aiohttp_client: Any) -> TestClient:
326324
"""Return an aiohttp test client with both rewrite rules and wildcard lookup enabled."""
327325
stats.reset()
328326
app = create_app(rewrite_wildcard_yaml_cfg)

tests/test_rewrite.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,25 @@ def test_apply_non_matching_rule_returns_original():
8383

8484

8585
def test_apply_first_match_wins():
86-
rules = compile_rules([
87-
{"pattern": r"^(.+)@relay\.example\.com$", "replacement": r"\1@first.example.com"},
88-
{"pattern": r"^(.+)@relay\.example\.com$", "replacement": r"\1@second.example.com"},
89-
])
86+
rules = compile_rules(
87+
[
88+
{"pattern": r"^(.+)@relay\.example\.com$", "replacement": r"\1@first.example.com"},
89+
{"pattern": r"^(.+)@relay\.example\.com$", "replacement": r"\1@second.example.com"},
90+
]
91+
)
9092
result = apply_rewrite_rules("alice@relay.example.com", rules)
9193
assert result == "alice@first.example.com"
9294

9395

9496
def test_apply_non_changing_rule_does_not_count_as_match():
9597
"""A rule that produces the identical string is not treated as a match."""
96-
rules = compile_rules([
97-
# This pattern matches but replacement produces the same string.
98-
{"pattern": r"^(.+)@example\.com$", "replacement": r"\1@example.com"},
99-
{"pattern": r"^(.+)@example\.com$", "replacement": r"\1@canonical.example.com"},
100-
])
98+
rules = compile_rules(
99+
[
100+
# This pattern matches but replacement produces the same string.
101+
{"pattern": r"^(.+)@example\.com$", "replacement": r"\1@example.com"},
102+
{"pattern": r"^(.+)@example\.com$", "replacement": r"\1@canonical.example.com"},
103+
]
104+
)
101105
result = apply_rewrite_rules("alice@example.com", rules)
102106
assert result == "alice@canonical.example.com"
103107

0 commit comments

Comments
 (0)