Skip to content

Commit 141f5d8

Browse files
authored
Allow users to subclass FileLock with custom keyword arguments (#284)
* Allow users to subclass FileLock with custom keyword arguments * Lint
1 parent 3e3455e commit 141f5d8

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/filelock/_api.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ def __new__( # noqa: PLR0913
8787
thread_local: bool = True, # noqa: ARG003, FBT001, FBT002
8888
*,
8989
is_singleton: bool = False,
90+
**kwargs: dict[str, Any], # capture remaining kwargs for subclasses # noqa: ARG003
9091
) -> Self:
9192
"""Create a new lock object or if specified return the singleton instance for the lock file."""
9293
if not is_singleton:

tests/test_filelock.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from pathlib import Path, PurePath
1313
from stat import S_IWGRP, S_IWOTH, S_IWUSR, filemode
1414
from types import TracebackType
15-
from typing import TYPE_CHECKING, Callable, Iterator, Tuple, Type, Union
15+
from typing import TYPE_CHECKING, Any, Callable, Iterator, Tuple, Type, Union
1616
from uuid import uuid4
1717

1818
import pytest
@@ -613,6 +613,37 @@ def test_lock_can_be_non_thread_local(
613613
lock.release(force=True)
614614

615615

616+
def test_subclass_compatibility(tmp_path: Path) -> None:
617+
class MyFileLock(FileLock):
618+
def __init__( # noqa: PLR0913 Too many arguments to function call (6 > 5)
619+
self,
620+
lock_file: str | os.PathLike[str],
621+
timeout: float = -1,
622+
mode: int = 0o644,
623+
thread_local: bool = True,
624+
my_param: int = 0,
625+
**kwargs: dict[str, Any],
626+
) -> None:
627+
pass
628+
629+
lock_path = tmp_path / "a"
630+
MyFileLock(str(lock_path), my_param=1)
631+
632+
class MySoftFileLock(SoftFileLock):
633+
def __init__( # noqa: PLR0913 Too many arguments to function call (6 > 5)
634+
self,
635+
lock_file: str | os.PathLike[str],
636+
timeout: float = -1,
637+
mode: int = 0o644,
638+
thread_local: bool = True,
639+
my_param: int = 0,
640+
**kwargs: dict[str, Any],
641+
) -> None:
642+
pass
643+
644+
MySoftFileLock(str(lock_path), my_param=1)
645+
646+
616647
@pytest.mark.parametrize("lock_type", [FileLock, SoftFileLock])
617648
def test_singleton_and_non_singleton_locks_are_distinct(lock_type: type[BaseFileLock], tmp_path: Path) -> None:
618649
lock_path = tmp_path / "a"

0 commit comments

Comments
 (0)