Search code examples
pythonpython-3.xpylintpython-3.8

pylint argues about ternary operator with assignment expression


I have some code like this:

return (
    (1 / a)
    if (a := foo())
    else 0
)

My pylint argues about this because "Using variable 'a' before assignment", even the evaluation order should be a := foo() first and then 1 / a or 0. I tried pip install --upgrade pylint, but it seems that pylint still do not agree this.


Solution

  • OK, I find that this is an issue of Pylint:

    https://github.com/PyCQA/pylint/issues/3347

    "pylint can parse the walrus operator but we haven't actually implemented support for it." (21 Jan)

    Anyway, I will modify the code to some equivalent versions which do not cause "Using variable before assignment", for example:

    if (a := foo()):
        return 1 / a
    else:
        return 0