Skip to content

Commit

Permalink
Allow users to subclass FileLock with custom keyword arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
hmaarrfk committed Oct 30, 2023
1 parent 3e3455e commit 1ac2251
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/filelock/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def __new__( # noqa: PLR0913
thread_local: bool = True, # noqa: ARG003, FBT001, FBT002
*,
is_singleton: bool = False,
**kwargs: dict[str, Any], # capture remaining kwargs for subclasses # noqa: ARG003
) -> Self:
"""Create a new lock object or if specified return the singleton instance for the lock file."""
if not is_singleton:
Expand Down
15 changes: 15 additions & 0 deletions tests/test_filelock.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,21 @@ def test_lock_can_be_non_thread_local(
lock.release(force=True)


@pytest.mark.parametrize("lock_type", [FileLock, SoftFileLock])
def test_subclass_compatibility(lock_type: type[BaseFileLock], tmp_path: Path) -> None:
class MyFileLock(lock_type):
def __init__(
self,
*args, # noqa: ANN002
my_param: int = 0,
**kwargs, # noqa: ANN003
) -> None:
pass

lock_path = tmp_path / "a"
MyFileLock(str(lock_path), my_param=1)


@pytest.mark.parametrize("lock_type", [FileLock, SoftFileLock])
def test_singleton_and_non_singleton_locks_are_distinct(lock_type: type[BaseFileLock], tmp_path: Path) -> None:
lock_path = tmp_path / "a"
Expand Down

0 comments on commit 1ac2251

Please sign in to comment.