Skip to main content

Separate Lock Instantiation from `with` Call

pixee:python/bad-lock-with-statement​

ImportanceReview GuidanceRequires Scanning Tool
LowMerge Without ReviewNo

This codemod separates creating a threading lock instance from calling it as a context manager. Calling with threading.Lock() does not have the effect you would expect. The lock is not acquired. Instead, to correctly acquire a lock, create the instance separately, before calling it as a context manager.

The change will apply to any of these threading classes: Lock, RLock, Condition, Semaphore, and BoundedSemaphore.

The change looks like this:

  import threading
- with threading.Lock():
+ lock = threading.Lock()
+ with lock:
...

If you have feedback on this codemod, please let us know!

F.A.Q.​

Why is this codemod marked as Merge Without Review?​

We believe this replacement is safe and should not result in any issues.

Codemod Settings​

N/A

References​