@@ -80,28 +80,33 @@ class ThreadLocalFileContext(FileLockContext, local):
80
80
class BaseFileLock (ABC , contextlib .ContextDecorator ):
81
81
"""Abstract base class for a file lock object."""
82
82
83
- _instances : WeakValueDictionary [str , BaseFileLock ]
83
+ _instances : WeakValueDictionary [str , Self ]
84
84
85
85
def __new__ ( # noqa: PLR0913
86
86
cls ,
87
87
lock_file : str | os .PathLike [str ],
88
88
timeout : float = - 1 ,
89
89
mode : int = 0o644 ,
90
- thread_local : bool = True , # noqa: ARG003, FBT001, FBT002
90
+ thread_local : bool = True , # noqa: FBT001, FBT002
91
91
* ,
92
- blocking : bool = True , # noqa: ARG003
92
+ blocking : bool = True ,
93
93
is_singleton : bool = False ,
94
94
** kwargs : Any , # capture remaining kwargs for subclasses # noqa: ARG003, ANN401
95
95
) -> Self :
96
96
"""Create a new lock object or if specified return the singleton instance for the lock file."""
97
97
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
99
101
100
102
instance = cls ._instances .get (str (lock_file ))
101
103
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 :
105
110
msg = "Singleton lock instances cannot be initialized with differing arguments"
106
111
raise ValueError (msg )
107
112
@@ -112,7 +117,7 @@ def __init_subclass__(cls, **kwargs: dict[str, Any]) -> None:
112
117
super ().__init_subclass__ (** kwargs )
113
118
cls ._instances = WeakValueDictionary ()
114
119
115
- def __init__ ( # noqa: PLR0913
120
+ def _initialize ( # noqa: PLR0913
116
121
self ,
117
122
lock_file : str | os .PathLike [str ],
118
123
timeout : float = - 1 ,
0 commit comments