WIP: use assignment expression in stdlib (combined PR) by vstinner · Pull Request #8122 · python/cpython

added 6 commits

July 5, 2018 23:24
Modify for loops and list comprehensions to use assignment
expressions.
Replace:

    else:
        var = expr
        if var:
            ...

with:

    elif (var := expr):
        ...
Replace:

    var = expr
    if var:
        ...

with:

    if (var := expr):
        ...

Don't replace when:

* var is used after the if
* Code like "var = regex.match(); if var: return var.group(1)"
  is left unchanged since it's covered by a dedicated PR.

Sometimes, var is only used in the condition and so has been removed
in this change. Maybe such kind of pattern should be addressed in a
different pull request.

This patch is restricted to the simplest test "if var:", other
conditions like "if var > 0:" are left unchaned to keep this change
reviewable (short enough).
Replace:

    m = regex.match(text)
    if m:
        return m.group(1)

with:

    if (m := regex.match(text)):
        return m.group(1)

Notes:

* Similar code using "if not m: return" is unchanged
* When the condition was more complex than "if m", the code is left
  unchanged
Replace:
    while True:
        x = expr
        if not x:
            break
        ...
with:
    while (x := expr):
        ...

Notes:

* Corner case: (x := expr) and (x != NUL), use x in the second test,
  whereas x is set in the first test
* asyncio/sslproto.py change avoids adding an empty chunk to appdata.
* some changes remove comments
* some lines are longer than 80 characters to keep everything on the
  same line

RonnyPfannschmidt

alesdakshanin

phord

phord

phord

JimJJewett

graingert

motleytech

motleytech

motleytech

motleytech