Skip to content

Assignment Expression Reports superfluous-parens False Positive #3249

Closed
@t-mart

Description

@t-mart

An if statement that tests a negated (not) parenthesized Assignment Expression without any other operators (See "Non-issues" section below) is reported by Pylint to have superfluous parentheses.

I believe this is a false positive. Removing the parentheses is invalid syntax.

Steps to reproduce

  1. Write an if statement that tests a negated (not) Assignment Expression, such as:
if not (my_var := False):
    print(my_var)
  1. Run pylint against that code.

Current behavior

Pylint reports superfluous-parens:Unnecessary parens after 'not' keyword.

Expected behavior

Pylint should not report about this syntax.

Removing the parentheses is a syntax error: SyntaxError: cannot use named assignment with operator.

pylint --version output

pylint 2.4.4
astroid 2.3.3
Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:21:23) [MSC v.1916 32 bit (Intel)]

Non-issues

  • If the negated assignment-expression if statement contains another binary operator, no Pylint report is generated about it. (Or at least, that seems to be the only difference.)

    For example, this slightly-modified snippet from What’s New In Python 3.8 does not cause Pylint to report:

    if not (n := len(list(range(11))) > 10:
        print(f"List is too long ({n} elements, expected <= 10)")

Metadata

Metadata

Assignees

No one assigned

    Labels

    Assignment expressionRelated to the walrus operator / assignment expressionBug 🪲Good first issueFriendly and approachable by new contributorsHelp wanted 🙏Outside help would be appreciated, good for new contributors

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions