Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Lib/_markupbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,7 @@ def _scan_name(self, i, declstartpos):
n = len(rawdata)
if i == n:
return None, -1
m = _declname_match(rawdata, i)
if m:
if (m := _declname_match(rawdata, i)):
s = m.group()
name = s.strip()
if (i + len(s)) == n:
Expand Down
3 changes: 1 addition & 2 deletions Lib/_osx_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,7 @@ def _check_for_unavailable_sdk(_config_vars):
# to /usr and /System/Library by either a standalone CLT
# package or the CLT component within Xcode.
cflags = _config_vars.get('CFLAGS', '')
m = re.search(r'-isysroot\s+(\S+)', cflags)
if m is not None:
if (m := re.search(r'-isysroot\s+(\S+)', cflags)) is not None:
sdk = m.group(1)
if not os.path.exists(sdk):
for cv in _UNIVERSAL_CONFIG_VARS:
Expand Down
6 changes: 2 additions & 4 deletions Lib/configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1059,8 +1059,7 @@ def _read(self, fp, fpname):
else:
indent_level = cur_indent_level
# is it a section header?
mo = self.SECTCRE.match(value)
if mo:
if (mo := self.SECTCRE.match(value)):
sectname = mo.group('header')
if sectname in self._sections:
if self._strict and sectname in elements_added:
Expand All @@ -1082,8 +1081,7 @@ def _read(self, fp, fpname):
raise MissingSectionHeaderError(fpname, lineno, line)
# an option line?
else:
mo = self._optcre.match(value)
if mo:
if (mo := self._optcre.match(value)):
optname, vi, optval = mo.group('option', 'vi', 'value')
if not optname:
e = self._handle_error(e, fpname, lineno, line)
Expand Down
3 changes: 1 addition & 2 deletions Lib/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,8 +599,7 @@ def _is_type(annotation, cls, a_module, a_type, is_type_predicate):
# a eval() penalty for every single field of every dataclass
# that's defined. It was judged not worth it.

match = _MODULE_IDENTIFIER_RE.match(annotation)
if match:
if (match := _MODULE_IDENTIFIER_RE.match(annotation)):
ns = None
module_name = match.group(1)
if not module_name:
Expand Down
7 changes: 4 additions & 3 deletions Lib/imaplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1579,9 +1579,10 @@ def run(cmd, args):
run(cmd, args)

for ml in run('list', ('/tmp/', 'yy%')):
mo = re.match(r'.*"([^"]+)"$', ml)
if mo: path = mo.group(1)
else: path = ml.split()[-1]
if (mo := re.match(r'.*"([^"]+)"$', ml)):
path = mo.group(1)
else:
path = ml.split()[-1]
run('delete', (path,))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the original author was already doing

if: then_stuff
else: else_stuff

to keep it from taking up too much white space. Your rewrite is actually a line longer, but only because you've gone back to the standard "suite indented on the next line" format. So that suggests this is relieving a real problem.

for cmd,args in test_seq2:
Expand Down
3 changes: 1 addition & 2 deletions Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -796,8 +796,7 @@ def findsource(object):
# that's most probably not inside a function definition.
candidates = []
for i in range(len(lines)):
match = pat.match(lines[i])
if match:
if (match := pat.match(lines[i])):
# if it's at toplevel, it's already the best one
if lines[i][0] == 'c':
return lines, i
Expand Down
3 changes: 1 addition & 2 deletions Lib/keyword.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ def main():
lines = []
for line in fp:
if '{1, "' in line:
match = strprog.search(line)
if match:
if (match := strprog.search(line)):
lines.append(" '" + match.group(1) + "'," + nl)
lines.sort()

Expand Down
6 changes: 2 additions & 4 deletions Lib/nntplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,8 +619,7 @@ def _getdescriptions(self, group_pattern, return_all):
resp, lines = self._longcmdstring('XGTITLE ' + group_pattern)
groups = {}
for raw_line in lines:
match = line_pat.search(raw_line.strip())
if match:
if (match := line_pat.search(raw_line.strip())):
name, desc = match.group(1, 2)
if not return_all:
return desc
Expand Down Expand Up @@ -845,8 +844,7 @@ def xgtitle(self, group, *, file=None):
resp, raw_lines = self._longcmdstring('XGTITLE ' + group, file)
lines = []
for raw_line in raw_lines:
match = line_pat.search(raw_line.strip())
if match:
if (match := line_pat.search(raw_line.strip())):
lines.append(match.group(1, 2))
return resp, lines

Expand Down
3 changes: 1 addition & 2 deletions Lib/smtplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,8 +471,7 @@ def ehlo(self, name=''):
# It's actually stricter, in that only spaces are allowed between
# parameters, but were not going to check for that here. Note
# that the space isn't present if there are no parameters.
m = re.match(r'(?P<feature>[A-Za-z0-9][A-Za-z0-9\-]*) ?', each)
if m:
if (m := re.match(r'(?P<feature>[A-Za-z0-9][A-Za-z0-9\-]*) ?', each)):
feature = m.group("feature").lower()
params = m.string[m.end("feature"):].strip()
if feature == "auth":
Expand Down
3 changes: 1 addition & 2 deletions Lib/string.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,7 @@ def substitute(*args, **kws):
# Helper function for .sub()
def convert(mo):
# Check the most common path first.
named = mo.group('named') or mo.group('braced')
if named is not None:
if (named := mo.group('named') or mo.group('braced')) is not None:
return str(mapping[named])
if mo.group('escaped') is not None:
return self.delimiter
Expand Down
15 changes: 5 additions & 10 deletions Lib/sysconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,7 @@ def _parse_makefile(filename, vars=None):
for line in lines:
if line.startswith('#') or line.strip() == '':
continue
m = _variable_rx.match(line)
if m:
if (m := _variable_rx.match(line)):
n, v = m.group(1, 2)
v = v.strip()
# `$$' is a literal `$' in make
Expand Down Expand Up @@ -449,18 +448,15 @@ def parse_config_h(fp, vars=None):
line = fp.readline()
if not line:
break
m = define_rx.match(line)
if m:
if (m := define_rx.match(line)):
n, v = m.group(1, 2)
try:
v = int(v)
except ValueError:
pass
vars[n] = v
else:
m = undef_rx.match(line)
if m:
vars[m.group(1)] = 0
elif (m := undef_rx.match(line)):
vars[m.group(1)] = 0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one looks like an improvement to me, because otherwise I smell a bug ... what if it went down the else case, but then the 2nd if wasn't true? Is that case handled?

Now I still smell the potential bug, but with an elif, it is a lot easier to explain what I'm worried about, as a simple "What if neither pattern matches?"

return vars


Expand Down Expand Up @@ -659,8 +655,7 @@ def get_platform():
osname = "cygwin"
import re
rel_re = re.compile(r'[\d.]+')
m = rel_re.match(release)
if m:
if (m := rel_re.match(release)):
release = m.group()
elif osname[:6] == "darwin":
import _osx_support
Expand Down
3 changes: 1 addition & 2 deletions Lib/tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1200,8 +1200,7 @@ def _proc_pax(self, tarfile):
# these fields are UTF-8 encoded but since POSIX.1-2008 tar
# implementations are allowed to store them as raw binary strings if
# the translation to UTF-8 fails.
match = re.search(br"\d+ hdrcharset=([^\n]+)\n", buf)
if match is not None:
if (match := re.search(br"\d+ hdrcharset=([^\n]+)\n", buf)) is not None:
pax_headers["hdrcharset"] = match.group(1).decode("utf-8")

# For the time being, we don't care about anything other than "BINARY".
Expand Down
12 changes: 5 additions & 7 deletions Lib/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,15 @@ def _main():
tokens = {}
prev_val = None
for line in lines:
match = prog.match(line)
if match:

if (match := prog.match(line)):
name, val = match.group(1, 2)
val = int(val)
tokens[val] = {'token': name} # reverse so we can sort them...
prev_val = val
else:
comment_match = comment_regex.match(line)
if comment_match and prev_val is not None:
comment = comment_match.group(1)
tokens[prev_val]['comment'] = comment
elif (comment_match := comment_regex.match(line)) and prev_val is not None:
comment = comment_match.group(1)
tokens[prev_val]['comment'] = comment
keys = sorted(tokens.keys())
# load the output skeleton from the target:
try:
Expand Down