@@ -80,28 +80,33 @@ class ThreadLocalFileContext(FileLockContext, local):
8080class BaseFileLock (ABC , contextlib .ContextDecorator ):
8181 """Abstract base class for a file lock object."""
8282
83- _instances : WeakValueDictionary [str , BaseFileLock ]
83+ _instances : WeakValueDictionary [str , Self ]
8484
8585 def __new__ ( # noqa: PLR0913
8686 cls ,
8787 lock_file : str | os .PathLike [str ],
8888 timeout : float = - 1 ,
8989 mode : int = 0o644 ,
90- thread_local : bool = True , # noqa: ARG003, FBT001, FBT002
90+ thread_local : bool = True , # noqa: FBT001, FBT002
9191 * ,
92- blocking : bool = True , # noqa: ARG003
92+ blocking : bool = True ,
9393 is_singleton : bool = False ,
9494 ** kwargs : Any , # capture remaining kwargs for subclasses # noqa: ARG003, ANN401
9595 ) -> Self :
9696 """Create a new lock object or if specified return the singleton instance for the lock file."""
9797 if not is_singleton :
98- return super ().__new__ (cls )
98+ self = super ().__new__ (cls )
99+ self ._initialize (lock_file , timeout , mode , thread_local , blocking = blocking , is_singleton = is_singleton )
100+ return self
99101
100102 instance = cls ._instances .get (str (lock_file ))
101103 if not instance :
102- instance = super ().__new__ (cls )
103- cls ._instances [str (lock_file )] = instance
104- elif timeout != instance .timeout or mode != instance .mode :
104+ self = super ().__new__ (cls )
105+ self ._initialize (lock_file , timeout , mode , thread_local , blocking = blocking , is_singleton = is_singleton )
106+ cls ._instances [str (lock_file )] = self
107+ return self
108+
109+ if timeout != instance .timeout or mode != instance .mode :
105110 msg = "Singleton lock instances cannot be initialized with differing arguments"
106111 raise ValueError (msg )
107112
@@ -112,7 +117,7 @@ def __init_subclass__(cls, **kwargs: dict[str, Any]) -> None:
112117 super ().__init_subclass__ (** kwargs )
113118 cls ._instances = WeakValueDictionary ()
114119
115- def __init__ ( # noqa: PLR0913
120+ def _initialize ( # noqa: PLR0913
116121 self ,
117122 lock_file : str | os .PathLike [str ],
118123 timeout : float = - 1 ,
0 commit comments