Skip to content

Rule suggestion: Prevent walrus assignments in assert statements #7205

@awaelchli

Description

@awaelchli

If you want to make your software package run with the python -O optimization, you might want to ensure that the code does not contain a walrus assignment in assert statements:

y = 0
assert (x := y) is not None
x = x + 1

This code example runs fine normally, but if run with python -O the assertion gets removed and the program raises a UnboundLocalError because x is no longer bound.

As a developer, I might want to configure my linter in such a way that walrus assignments are not allowed in assertion statements. This way, I can enforce that the code will be compatible running with python -O.

Metadata

Metadata

Assignees

No one assigned

    Labels

    acceptedReady for implementationruleImplementing or modifying a lint rule

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions