Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Use assign expr for match/group #8097

wants to merge 1 commit into from
Changes from all commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.


Just for now

@@ -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 =
name = s.strip()
if (i + len(s)) == n:
@@ -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 ='-isysroot\s+(\S+)', cflags)
if m is not None:
if (m :='-isysroot\s+(\S+)', cflags)) is not None:
sdk =
if not os.path.exists(sdk):
@@ -1059,8 +1059,7 @@ def _read(self, fp, fpname):
indent_level = cur_indent_level
# is it a section header?
mo = self.SECTCRE.match(value)
if mo:
if (mo := self.SECTCRE.match(value)):
sectname ='header')
if sectname in self._sections:
if self._strict and sectname in elements_added:
@@ -1082,8 +1081,7 @@ def _read(self, fp, fpname):
raise MissingSectionHeaderError(fpname, lineno, line)
# an option line?
mo = self._optcre.match(value)
if mo:
if (mo := self._optcre.match(value)):
optname, vi, optval ='option', 'vi', 'value')
if not optname:
e = self._handle_error(e, fpname, lineno, line)
@@ -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 =
if not module_name:
@@ -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 =
else: path = ml.split()[-1]
if (mo := re.match(r'.*"([^"]+)"$', ml)):
path =
path = ml.split()[-1]
run('delete', (path,))

This comment has been minimized.

Copy link

JimJJewett Jul 6, 2018

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:
@@ -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
@@ -74,8 +74,7 @@ def main():
lines = []
for line in fp:
if '{1, "' in line:
match =
if match:
if (match :=
lines.append(" '" + + "'," + nl)

@@ -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 =
if match:
if (match :=
name, desc =, 2)
if not return_all:
return desc
@@ -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 =
if match:
if (match :=
lines.append(, 2))
return resp, lines

@@ -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 ="feature").lower()
params = m.string[m.end("feature"):].strip()
if feature == "auth":
@@ -120,8 +120,7 @@ def substitute(*args, **kws):
# Helper function for .sub()
def convert(mo):
# Check the most common path first.
named ='named') or'braced')
if named is not None:
if (named :='named') or'braced')) is not None:
return str(mapping[named])
if'escaped') is not None:
return self.delimiter
@@ -224,8 +224,7 @@ def _parse_makefile(filename, vars=None):
for line in lines:
if line.startswith('#') or line.strip() == '':
m = _variable_rx.match(line)
if m:
if (m := _variable_rx.match(line)):
n, v =, 2)
v = v.strip()
# `$$' is a literal `$' in make
@@ -449,18 +448,15 @@ def parse_config_h(fp, vars=None):
line = fp.readline()
if not line:
m = define_rx.match(line)
if m:
if (m := define_rx.match(line)):
n, v =, 2)
v = int(v)
except ValueError:
vars[n] = v
m = undef_rx.match(line)
if m:
vars[] = 0
elif (m := undef_rx.match(line)):
vars[] = 0

This comment has been minimized.

Copy link

JimJJewett Jul 6, 2018

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

@@ -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 =
elif osname[:6] == "darwin":
import _osx_support
@@ -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 ="\d+ hdrcharset=([^\n]+)\n", buf)
if match is not None:
if (match :="\d+ hdrcharset=([^\n]+)\n", buf)) is not None:
pax_headers["hdrcharset"] ="utf-8")

# For the time being, we don't care about anything other than "BINARY".
@@ -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 =, 2)
val = int(val)
tokens[val] = {'token': name} # reverse so we can sort them...
prev_val = val
comment_match = comment_regex.match(line)
if comment_match and prev_val is not None:
comment =
tokens[prev_val]['comment'] = comment
elif (comment_match := comment_regex.match(line)) and prev_val is not None:
comment =
tokens[prev_val]['comment'] = comment
keys = sorted(tokens.keys())
# load the output skeleton from the target:
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.