-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b4c44b4
commit 8d692e6
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
docs/codemods/python/pixee_python_bad-lock-with-statement.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
--- | ||
title: Separate Lock Instantiation from `with` Call | ||
sidebar_position: 1 | ||
--- | ||
|
||
## pixee:python/bad-lock-with-statement | ||
|
||
| Importance | Review Guidance | Requires SARIF Tool | | ||
|------------|----------------------------|---------------------| | ||
| Low | Merge Without Review | No | | ||
|
||
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: | ||
|
||
```diff | ||
import threading | ||
- with threading.Lock(): | ||
+ lock = threading.Lock() | ||
+ with lock: | ||
... | ||
``` | ||
|
||
If you have feedback on this codemod, [please let us know](mailto:[email protected])! | ||
|
||
## 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 | ||
|
||
* [https://pylint.pycqa.org/en/latest/user_guide/messages/warning/useless-with-lock.](https://pylint.pycqa.org/en/latest/user_guide/messages/warning/useless-with-lock.) | ||
* [https://docs.python.org/3/library/threading.html#using-locks-conditions-and-semaphores-in-the-with-statement](https://docs.python.org/3/library/threading.html#using-locks-conditions-and-semaphores-in-the-with-statement) |