useless-with-lock (PLW2101)#
Derived from the Pylint linter.
This rule is unstable and in preview. The --preview
flag is required for use.
What it does#
Checks for direct uses of lock objects in with
statements.
Why is this bad?#
Creating a lock (via threading.Lock
or similar) in a with
statement
has no effect, as locks are only relevant when shared between threads.
Instead, assign the lock to a variable outside the with
statement,
and share that variable between threads.
Example#
Use instead:
import threading
counter = 0
lock = threading.Lock()
def increment():
global counter
with lock:
counter += 1