8
8
from abc import ABC , abstractmethod
9
9
from dataclasses import dataclass
10
10
from threading import local
11
- from types import TracebackType
12
- from typing import Any
11
+ from typing import TYPE_CHECKING , Any
13
12
14
13
from ._error import Timeout
15
14
15
+ if TYPE_CHECKING :
16
+ from types import TracebackType
17
+
16
18
_LOGGER = logging .getLogger ("filelock" )
17
19
18
20
@@ -30,18 +32,16 @@ def __enter__(self) -> BaseFileLock:
30
32
31
33
def __exit__ (
32
34
self ,
33
- exc_type : type [BaseException ] | None , # noqa: U100
34
- exc_value : BaseException | None , # noqa: U100
35
- traceback : TracebackType | None , # noqa: U100
35
+ exc_type : type [BaseException ] | None ,
36
+ exc_value : BaseException | None ,
37
+ traceback : TracebackType | None ,
36
38
) -> None :
37
39
self .lock .release ()
38
40
39
41
40
42
@dataclass
41
43
class FileLockContext :
42
- """
43
- A dataclass which holds the context for a ``BaseFileLock`` object.
44
- """
44
+ """A dataclass which holds the context for a ``BaseFileLock`` object."""
45
45
46
46
# The context is held in a separate class to allow optional use of thread local storage via the
47
47
# ThreadLocalFileContext class.
@@ -63,9 +63,7 @@ class FileLockContext:
63
63
64
64
65
65
class ThreadLocalFileContext (FileLockContext , local ):
66
- """
67
- A thread local version of the ``FileLockContext`` class.
68
- """
66
+ """A thread local version of the ``FileLockContext`` class."""
69
67
70
68
71
69
class BaseFileLock (ABC , contextlib .ContextDecorator ):
@@ -76,7 +74,7 @@ def __init__(
76
74
lock_file : str | os .PathLike [Any ],
77
75
timeout : float = - 1 ,
78
76
mode : int = 0o644 ,
79
- thread_local : bool = True ,
77
+ thread_local : bool = True , # noqa: FBT001, FBT002
80
78
) -> None :
81
79
"""
82
80
Create a new lock object.
@@ -151,9 +149,7 @@ def is_locked(self) -> bool:
151
149
152
150
@property
153
151
def lock_counter (self ) -> int :
154
- """
155
- :return: The number of times this lock has been acquired (but not yet released).
156
- """
152
+ """:return: The number of times this lock has been acquired (but not yet released)."""
157
153
return self ._context .lock_counter
158
154
159
155
def acquire (
@@ -218,22 +214,21 @@ def acquire(
218
214
if self .is_locked :
219
215
_LOGGER .debug ("Lock %s acquired on %s" , lock_id , lock_filename )
220
216
break
221
- elif blocking is False :
217
+ if blocking is False :
222
218
_LOGGER .debug ("Failed to immediately acquire lock %s on %s" , lock_id , lock_filename )
223
- raise Timeout (lock_filename )
224
- elif 0 <= timeout < time .perf_counter () - start_time :
219
+ raise Timeout (lock_filename ) # noqa: TRY301
220
+ if 0 <= timeout < time .perf_counter () - start_time :
225
221
_LOGGER .debug ("Timeout on acquiring lock %s on %s" , lock_id , lock_filename )
226
- raise Timeout (lock_filename )
227
- else :
228
- msg = "Lock %s not acquired on %s, waiting %s seconds ..."
229
- _LOGGER .debug (msg , lock_id , lock_filename , poll_interval )
230
- time .sleep (poll_interval )
222
+ raise Timeout (lock_filename ) # noqa: TRY301
223
+ msg = "Lock %s not acquired on %s, waiting %s seconds ..."
224
+ _LOGGER .debug (msg , lock_id , lock_filename , poll_interval )
225
+ time .sleep (poll_interval )
231
226
except BaseException : # Something did go wrong, so decrement the counter.
232
227
self ._context .lock_counter = max (0 , self ._context .lock_counter - 1 )
233
228
raise
234
229
return AcquireReturnProxy (lock = self )
235
230
236
- def release (self , force : bool = False ) -> None :
231
+ def release (self , force : bool = False ) -> None : # noqa: FBT001, FBT002
237
232
"""
238
233
Releases the file lock. Please note, that the lock is only completely released, if the lock counter is 0. Also
239
234
note, that the lock file itself is not automatically deleted.
@@ -262,9 +257,9 @@ def __enter__(self) -> BaseFileLock:
262
257
263
258
def __exit__ (
264
259
self ,
265
- exc_type : type [BaseException ] | None , # noqa: U100
266
- exc_value : BaseException | None , # noqa: U100
267
- traceback : TracebackType | None , # noqa: U100
260
+ exc_type : type [BaseException ] | None ,
261
+ exc_value : BaseException | None ,
262
+ traceback : TracebackType | None ,
268
263
) -> None :
269
264
"""
270
265
Release the lock.
0 commit comments