Skip to content

Files

Latest commit

 

History

History
31 lines (19 loc) · 627 Bytes

useless-with-lock.md

File metadata and controls

31 lines (19 loc) · 627 Bytes

Pattern: Unnecessary with lock

Issue: -

Description

Used when a new lock instance is created by using with statement which has no effect. Instead, an existing instance should be used to acquire lock.

Example of incorrect code:

import threading
from threading import Lock, RLock, Condition, Semaphore, BoundedSemaphore


with threading.Lock():  # [useless-with-lock]
    ...

Example of correct code:

import threading
from threading import Lock, RLock, Condition, Semaphore, BoundedSemaphore


lock = threading.Lock()
with lock:  # this is ok
    ...