Skip to content

Commit f0c18f6

Browse files
committed
style: use walrus for regexing
1 parent 6afcdc3 commit f0c18f6

File tree

3 files changed

+6
-13
lines changed

3 files changed

+6
-13
lines changed

ci/parse_relnotes.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ def parse_md(lines):
4444
buffer = TextChunkBuffer()
4545

4646
for line in lines:
47-
header_match = re.search(r"^(#+) (.+)$", line)
48-
is_header = bool(header_match)
49-
if is_header:
47+
if header_match := re.search(r"^(#+) (.+)$", line):
5048
yield from buffer.flush()
5149
hashes, text = header_match.groups()
5250
yield (f"h{len(hashes)}", text)
@@ -80,8 +78,7 @@ def sections(parsed_data):
8078

8179
def refind(regex, text):
8280
"""Find a regex in some text, and return the matched text, or None."""
83-
m = re.search(regex, text)
84-
if m:
81+
if m := re.search(regex, text):
8582
return m.group()
8683
else:
8784
return None

coverage/files.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,7 @@ def match(self, fpath: str) -> bool:
300300

301301
def sep(s: str) -> str:
302302
"""Find the path separator used in this string, or os.sep if none."""
303-
sep_match = re.search(r"[\\/]", s)
304-
if sep_match:
303+
if sep_match := re.search(r"[\\/]", s):
305304
the_sep = sep_match[0]
306305
else:
307306
the_sep = os.sep
@@ -337,8 +336,7 @@ def _glob_to_regex(pattern: str) -> str:
337336
pos = 0
338337
while pos < len(pattern):
339338
for rx, sub in G2RX_TOKENS: # pragma: always breaks
340-
m = rx.match(pattern, pos=pos)
341-
if m:
339+
if m := rx.match(pattern, pos=pos):
342340
if sub is None:
343341
raise ConfigError(f"File pattern can't include {m[0]!r}")
344342
path_rx.append(m.expand(sub))
@@ -469,8 +467,7 @@ def map(self, path: str, exists:Callable[[str], bool] = source_exists) -> str:
469467
self.pprinted = True
470468

471469
for original_pattern, regex, result in self.aliases:
472-
m = regex.match(path)
473-
if m:
470+
if m := regex.match(path):
474471
new = path.replace(m[0], result)
475472
new = new.replace(sep(path), sep(result))
476473
if not self.relative:

lab/parser.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ def one_file(self, options, filename):
6161
# `filename` can have a line number suffix. In that case, extract those
6262
# lines, dedent them, and use that. This is for trying test cases
6363
# embedded in the test files.
64-
match = re.search(r"^(.*):(\d+)-(\d+)$", filename)
65-
if match:
64+
if match := re.search(r"^(.*):(\d+)-(\d+)$", filename):
6665
filename, start, end = match.groups()
6766
start, end = int(start), int(end)
6867
else:

0 commit comments

Comments
 (0)