Skip to content

Hotfix: Restore __init__ method; more robust initialization for singleton locks #338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Improve subclass test; put singleton validation in __init__
  • Loading branch information
ethanbb committed Jun 12, 2024
commit 57c4a3114689c28b0f83b86322de5c130bf1d973
24 changes: 15 additions & 9 deletions src/filelock/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ class BaseFileLock(ABC, contextlib.ContextDecorator):
def __new__( # noqa: PLR0913
cls,
lock_file: str | os.PathLike[str],
timeout: float = -1,
mode: int = 0o644,
thread_local: bool = True, # noqa: FBT001, FBT002
timeout: float = -1, # noqa: ARG003
mode: int = 0o644, # noqa: ARG003
thread_local: bool = True, # noqa: FBT001, FBT002, ARG003
*,
blocking: bool = True,
blocking: bool = True, # noqa: ARG003
is_singleton: bool = False,
**kwargs: Any, # capture remaining kwargs for subclasses # noqa: ARG003, ANN401
) -> Self:
Expand All @@ -103,10 +103,6 @@ def __new__( # noqa: PLR0913
cls._instances[str(lock_file)] = self
return self

if timeout != instance.timeout or mode != instance.mode:
msg = "Singleton lock instances cannot be initialized with differing arguments"
raise ValueError(msg)

return instance # type: ignore[return-value] # https://github.com/python/mypy/issues/15322

def __init_subclass__(cls, **kwargs: dict[str, Any]) -> None:
Expand Down Expand Up @@ -140,7 +136,17 @@ def __init__( # noqa: PLR0913
to pass the same object around.

"""
if hasattr(self, "_context"):
if is_singleton and hasattr(self, "_context"):
# test whether other parameters match existing instance.
if (
self.is_thread_local() != thread_local
or not self.is_singleton
or self._context.timeout != timeout
or self._context.mode != mode
or self._context.blocking != blocking
):
msg = "Singleton lock instances cannot be initialized with differing arguments"
raise ValueError(msg)
return # bypass initialization because object is already initialized

self._is_thread_local = thread_local
Expand Down
10 changes: 6 additions & 4 deletions tests/test_filelock.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,9 +687,10 @@ def __init__( # noqa: PLR0913 Too many arguments to function call (6 > 5)
mode: int = 0o644,
thread_local: bool = True,
my_param: int = 0,
**kwargs: dict[str, Any],
**kwargs: dict[str, Any], # noqa: ARG002
) -> None:
pass
super().__init__(lock_file, timeout, mode, thread_local, blocking=True, is_singleton=True)
self.my_param = my_param

lock_path = tmp_path / "a"
MyFileLock(str(lock_path), my_param=1)
Expand All @@ -702,9 +703,10 @@ def __init__( # noqa: PLR0913 Too many arguments to function call (6 > 5)
mode: int = 0o644,
thread_local: bool = True,
my_param: int = 0,
**kwargs: dict[str, Any],
**kwargs: dict[str, Any], # noqa: ARG002
) -> None:
pass
super().__init__(lock_file, timeout, mode, thread_local, blocking=True, is_singleton=True)
self.my_param = my_param

MySoftFileLock(str(lock_path), my_param=1)

Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy