From: Tamir Duberstein <tamird@kernel.org>
To: "Kernel.org Tools" <tools@kernel.org>
Cc: Konstantin Ryabitsev <konstantin@linuxfoundation.org>,
Tamir Duberstein <tamird@kernel.org>
Subject: [PATCH b4 v2 09/11] Avoid duplicate map lookups
Date: Sun, 19 Apr 2026 12:00:04 -0400 [thread overview]
Message-ID: <20260419-ruff-check-v2-9-089dfb264501@kernel.org> (raw)
In-Reply-To: <20260419-ruff-check-v2-0-089dfb264501@kernel.org>
Use dict.get and defaultdict to avoid repeated membership checks before
indexing into the same map. This keeps the existing behavior while
making later type narrowing easier for mypy and pyright.
Signed-off-by: Tamir Duberstein <tamird@kernel.org>
---
src/b4/__init__.py | 105 ++++++++++++++++++++++++++---------------------------
src/b4/ez.py | 10 ++---
2 files changed, 56 insertions(+), 59 deletions(-)
diff --git a/src/b4/__init__.py b/src/b4/__init__.py
index c427ee4..72b1c97 100644
--- a/src/b4/__init__.py
+++ b/src/b4/__init__.py
@@ -28,6 +28,7 @@ import tempfile
import textwrap
import time
import urllib.parse
+from collections import defaultdict
from contextlib import contextmanager
from email import charset
from email.message import EmailMessage
@@ -198,7 +199,7 @@ class LoreMailbox:
msgid_map: Dict[str, 'LoreMessage']
series: Dict[int, 'LoreSeries']
covers: Dict[int, 'LoreMessage']
- trailer_map: Dict[str, List['LoreMessage']]
+ trailer_map: defaultdict[str, List['LoreMessage']]
followups: List['LoreMessage']
unknowns: List['LoreMessage']
@@ -206,7 +207,7 @@ class LoreMailbox:
self.msgid_map = dict()
self.series = dict()
self.covers = dict()
- self.trailer_map = dict()
+ self.trailer_map = defaultdict(list)
self.followups = list()
self.unknowns = list()
@@ -223,11 +224,6 @@ class LoreMailbox:
return '\n'.join(out)
- def get_by_msgid(self, msgid: str) -> Optional['LoreMessage']:
- if msgid in self.msgid_map:
- return self.msgid_map[msgid]
- return None
-
def partial_reroll(self, revision: int, sloppytrailers: bool) -> None:
# Is it a partial reroll?
# To qualify for a partial reroll:
@@ -244,11 +240,13 @@ class LoreMailbox:
for patch in lser.patches:
if patch is None:
continue
- if patch.in_reply_to is None or patch.in_reply_to not in self.msgid_map:
+ if (
+ patch.in_reply_to is None
+ or (ppatch := self.msgid_map.get(patch.in_reply_to)) is None
+ ):
logger.debug('Patch not sent as a reply-to')
sane = False
break
- ppatch = self.msgid_map[patch.in_reply_to]
found = False
while True:
if (
@@ -263,10 +261,10 @@ class LoreMailbox:
# Do we have another level up?
if (
ppatch.in_reply_to is None
- or ppatch.in_reply_to not in self.msgid_map
+ or (npatch := self.msgid_map.get(ppatch.in_reply_to)) is None
):
break
- ppatch = self.msgid_map[ppatch.in_reply_to]
+ ppatch = npatch
if not found:
sane = False
@@ -330,9 +328,7 @@ class LoreMailbox:
qmsgs, ignore_msgids=set(self.msgid_map.keys())
)
for patchid, fmsgs in patchid_map.items():
- if patchid not in self.trailer_map:
- self.trailer_map[patchid] = list()
- self.trailer_map[patchid] += fmsgs
+ self.trailer_map[patchid].extend(fmsgs)
def get_latest_revision(self) -> Optional[int]:
if not len(self.series):
@@ -356,10 +352,10 @@ class LoreMailbox:
revision = self.get_latest_revision()
if revision is None:
return None
- elif revision not in self.series:
- return None
- lser = self.series[revision]
+ lser = self.series.get(revision)
+ if lser is None:
+ return None
# Is it empty?
empty = True
@@ -375,15 +371,15 @@ class LoreMailbox:
self.partial_reroll(revision, sloppytrailers)
# Grab our cover letter if we have one
- if revision in self.covers:
- lser.add_patch(self.covers[revision])
+ if (cover := self.covers.get(revision)) is not None:
+ lser.add_patch(cover)
lser.has_cover = True
else:
# Let's find the first patch with an in-reply-to and see if that
# is our cover letter
for member in lser.patches:
if member is not None and member.in_reply_to is not None:
- potential = self.get_by_msgid(member.in_reply_to)
+ potential = self.msgid_map.get(member.in_reply_to)
if (
potential is not None
and potential.has_diffstat
@@ -414,12 +410,13 @@ class LoreMailbox:
if fmsg.in_reply_to is None:
# Check if there's something matching in References
for refid in fmsg.references:
- if refid in self.msgid_map and refid != fmsg.msgid:
- pmsg = self.msgid_map[refid]
+ if (
+ refid != fmsg.msgid
+ and (pmsg := self.msgid_map.get(refid)) is not None
+ ):
logger.debug('Found a references entry %s in msgid_map', refid)
break
- elif fmsg.in_reply_to in self.msgid_map:
- pmsg = self.msgid_map[fmsg.in_reply_to]
+ elif (pmsg := self.msgid_map.get(fmsg.in_reply_to)) is not None:
logger.debug('Found in-reply-to %s in msgid_map', fmsg.in_reply_to)
if pmsg is None:
# Can't find the message we're replying to here
@@ -443,8 +440,6 @@ class LoreMailbox:
# previous revisions to current revision if patch id did
# not change
if pmsg.git_patch_id:
- if pmsg.git_patch_id not in self.trailer_map:
- self.trailer_map[pmsg.git_patch_id] = list()
self.trailer_map[pmsg.git_patch_id].append(fmsg)
pmsg.followup_trailers += trailers
break
@@ -452,15 +447,18 @@ class LoreMailbox:
# Could be a cover letter
pmsg.followup_trailers += trailers
break
- if pmsg.in_reply_to and pmsg.in_reply_to in self.msgid_map:
+ if (
+ pmsg.in_reply_to
+ and (nmsg := self.msgid_map.get(pmsg.in_reply_to)) is not None
+ ):
# Avoid bad message id causing infinite loop
- if pmsg == self.msgid_map[pmsg.in_reply_to]:
+ if pmsg == nmsg:
break
lvl += 1
for pltr in pmsg.trailers:
pltr.lmsg = pmsg
trailers.append(pltr)
- pmsg = self.msgid_map[pmsg.in_reply_to]
+ pmsg = nmsg
continue
break
@@ -472,29 +470,28 @@ class LoreMailbox:
logger.debug(
' matching patch_id %s from: %s', lmsg.git_patch_id, lmsg.full_subject
)
- if lmsg.git_patch_id in self.trailer_map:
- for fmsg in self.trailer_map[lmsg.git_patch_id]:
- logger.debug(' matched: %s', fmsg.msgid)
- fltrs, fmis = fmsg.get_trailers(sloppy=sloppytrailers)
- for fltr in fltrs:
- if fltr in lmsg.trailers:
- logger.debug(' trailer already exists')
- continue
- if fltr in lmsg.followup_trailers:
- logger.debug(' identical trailer received for this series')
- continue
- logger.debug(
- ' carrying over the trailer to this series (may be duplicate)'
- )
- logger.debug(' %s', lmsg.full_subject)
- logger.debug(' + %s', fltr.as_string())
- if fltr.lmsg:
- logger.debug(' via: %s', fltr.lmsg.msgid)
- lmsg.followup_trailers.append(fltr)
- for fltr in fmis:
- lser.trailer_mismatches.add(
- (fltr.name, fltr.value, fmsg.fromname, fmsg.fromemail)
- )
+ for fmsg in self.trailer_map.get(lmsg.git_patch_id, ()):
+ logger.debug(' matched: %s', fmsg.msgid)
+ fltrs, fmis = fmsg.get_trailers(sloppy=sloppytrailers)
+ for fltr in fltrs:
+ if fltr in lmsg.trailers:
+ logger.debug(' trailer already exists')
+ continue
+ if fltr in lmsg.followup_trailers:
+ logger.debug(' identical trailer received for this series')
+ continue
+ logger.debug(
+ ' carrying over the trailer to this series (may be duplicate)'
+ )
+ logger.debug(' %s', lmsg.full_subject)
+ logger.debug(' + %s', fltr.as_string())
+ if fltr.lmsg:
+ logger.debug(' via: %s', fltr.lmsg.msgid)
+ lmsg.followup_trailers.append(fltr)
+ for fltr in fmis:
+ lser.trailer_mismatches.add(
+ (fltr.name, fltr.value, fmsg.fromname, fmsg.fromemail)
+ )
return lser
@@ -529,7 +526,7 @@ class LoreMailbox:
if lmsg.revision_inferred and lmsg.in_reply_to:
# We have an inferred revision here.
# Do we have an upthread cover letter that specifies a revision?
- irt = self.get_by_msgid(lmsg.in_reply_to)
+ irt = self.msgid_map.get(lmsg.in_reply_to)
if irt is not None and irt.has_diffstat and not irt.has_diff:
# Yes, this is very likely our cover letter
logger.debug(' fixed revision to v%s', irt.revision)
@@ -3796,7 +3793,7 @@ def get_config_from_git(
chunks = key.split('.')
cfgkey = chunks[-1].lower()
if cfgkey in multivals:
- if cfgkey not in gitconfig or gitconfig[cfgkey] is None:
+ if gitconfig.get(cfgkey) is None:
gitconfig[cfgkey] = list()
gitconfig[cfgkey].append(value)
else:
diff --git a/src/b4/ez.py b/src/b4/ez.py
index 3a59256..d64e0bc 100644
--- a/src/b4/ez.py
+++ b/src/b4/ez.py
@@ -26,6 +26,7 @@ import textwrap
import time
import urllib.parse
import uuid
+from collections import defaultdict
from email.message import EmailMessage
from string import Template
from typing import Any, Dict, List, Optional, Set, Tuple, Union
@@ -2262,7 +2263,7 @@ def cmd_send(cmdargs: argparse.Namespace) -> None:
seen: Set[str] = set()
excludes: Set[str] = set()
- pccs: Dict[str, List[Tuple[str, str]]] = dict()
+ pccs: defaultdict[str, List[Tuple[str, str]]] = defaultdict(list)
if cmdargs.preview_to or cmdargs.no_trailer_to_cc:
todests = list()
@@ -2285,10 +2286,9 @@ def cmd_send(cmdargs: argparse.Namespace) -> None:
if btr.addr[1] in seen:
continue
if commit:
- if commit not in pccs:
- pccs[commit] = list()
- if btr.addr not in pccs[commit]:
- pccs[commit].append(btr.addr)
+ cpccs = pccs[commit]
+ if btr.addr not in cpccs:
+ cpccs.append(btr.addr)
continue
seen.add(btr.addr[1])
if btr.lname == 'to':
--
2.53.0
next prev parent reply other threads:[~2026-04-19 16:00 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-19 15:59 [PATCH b4 v2 00/11] Enable stricter local checks Tamir Duberstein
2026-04-19 15:59 ` [PATCH b4 v2 01/11] Add CI script Tamir Duberstein
2026-04-19 15:59 ` [PATCH b4 v2 02/11] Add ruff checks to CI Tamir Duberstein
2026-04-19 15:59 ` [PATCH b4 v2 03/11] Import dependencies unconditionally Tamir Duberstein
2026-04-19 15:59 ` [PATCH b4 v2 04/11] Add ruff format check to CI Tamir Duberstein
2026-04-19 18:06 ` Tamir Duberstein
2026-04-19 16:00 ` [PATCH b4 v2 05/11] Fix tests under uv with complex git config Tamir Duberstein
2026-04-19 16:00 ` [PATCH b4 v2 06/11] Fix typings in misc/ Tamir Duberstein
2026-04-19 16:00 ` [PATCH b4 v2 07/11] Enable mypy unreachable warnings Tamir Duberstein
2026-04-19 16:00 ` [PATCH b4 v2 08/11] Enable and fix pyright diagnostics Tamir Duberstein
2026-04-19 16:00 ` Tamir Duberstein [this message]
2026-04-19 16:00 ` [PATCH b4 v2 10/11] Add ty and configuration Tamir Duberstein
2026-04-19 16:00 ` [PATCH b4 v2 11/11] Enable pyright strict mode Tamir Duberstein
2026-04-23 2:48 ` [PATCH b4 v2 00/11] Enable stricter local checks Konstantin Ryabitsev
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260419-ruff-check-v2-9-089dfb264501@kernel.org \
--to=tamird@kernel.org \
--cc=konstantin@linuxfoundation.org \
--cc=tools@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox