88from abc import ABC , abstractmethod
99from dataclasses import dataclass
1010from threading import local
11- from types import TracebackType
12- from typing import Any
11+ from typing import TYPE_CHECKING , Any
1312
1413from ._error import Timeout
1514
15+ if TYPE_CHECKING :
16+ from types import TracebackType
17+
1618_LOGGER = logging .getLogger ("filelock" )
1719
1820
@@ -30,18 +32,16 @@ def __enter__(self) -> BaseFileLock:
3032
3133 def __exit__ (
3234 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 ,
3638 ) -> None :
3739 self .lock .release ()
3840
3941
4042@dataclass
4143class 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."""
4545
4646 # The context is held in a separate class to allow optional use of thread local storage via the
4747 # ThreadLocalFileContext class.
@@ -63,9 +63,7 @@ class FileLockContext:
6363
6464
6565class ThreadLocalFileContext (FileLockContext , local ):
66- """
67- A thread local version of the ``FileLockContext`` class.
68- """
66+ """A thread local version of the ``FileLockContext`` class."""
6967
7068
7169class BaseFileLock (ABC , contextlib .ContextDecorator ):
@@ -76,7 +74,7 @@ def __init__(
7674 lock_file : str | os .PathLike [Any ],
7775 timeout : float = - 1 ,
7876 mode : int = 0o644 ,
79- thread_local : bool = True ,
77+ thread_local : bool = True , # noqa: FBT001, FBT002
8078 ) -> None :
8179 """
8280 Create a new lock object.
@@ -151,9 +149,7 @@ def is_locked(self) -> bool:
151149
152150 @property
153151 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)."""
157153 return self ._context .lock_counter
158154
159155 def acquire (
@@ -218,22 +214,21 @@ def acquire(
218214 if self .is_locked :
219215 _LOGGER .debug ("Lock %s acquired on %s" , lock_id , lock_filename )
220216 break
221- elif blocking is False :
217+ if blocking is False :
222218 _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 :
225221 _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 )
231226 except BaseException : # Something did go wrong, so decrement the counter.
232227 self ._context .lock_counter = max (0 , self ._context .lock_counter - 1 )
233228 raise
234229 return AcquireReturnProxy (lock = self )
235230
236- def release (self , force : bool = False ) -> None :
231+ def release (self , force : bool = False ) -> None : # noqa: FBT001, FBT002
237232 """
238233 Releases the file lock. Please note, that the lock is only completely released, if the lock counter is 0. Also
239234 note, that the lock file itself is not automatically deleted.
@@ -262,9 +257,9 @@ def __enter__(self) -> BaseFileLock:
262257
263258 def __exit__ (
264259 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 ,
268263 ) -> None :
269264 """
270265 Release the lock.
0 commit comments