Skip to content

Commit

Permalink
Pass file_lock as positional argument (#347)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwist-sgr authored Jun 22, 2024
1 parent 3a79343 commit 9a979df
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
7 changes: 2 additions & 5 deletions src/filelock/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ def __call__( # noqa: PLR0913
# (https://github.com/tox-dev/filelock/pull/340)

all_params = {
"lock_file": lock_file,
"timeout": timeout,
"mode": mode,
"thread_local": thread_local,
Expand All @@ -129,12 +128,10 @@ def __call__( # noqa: PLR0913
**kwargs,
}

present_params = set(inspect.signature(cls.__init__).parameters) # type: ignore[misc]
present_params = inspect.signature(cls.__init__).parameters # type: ignore[misc]
init_params = {key: value for key, value in all_params.items() if key in present_params}
# The `lock_file` parameter is required
init_params["lock_file"] = lock_file

instance = super().__call__(**init_params)
instance = super().__call__(lock_file, **init_params)

if is_singleton:
cls._instances[str(lock_file)] = instance # type: ignore[attr-defined]
Expand Down
10 changes: 10 additions & 0 deletions tests/test_filelock.py
Original file line number Diff line number Diff line change
Expand Up @@ -802,3 +802,13 @@ def __init__(self, *args: Any, **kwargs: Any) -> None: # noqa: ANN401

assert lock1 is lock2
assert init_calls == 1


def test_file_lock_positional_argument(tmp_path: Path) -> None:
class FilePathLock(FileLock):
def __init__(self, file_path: str) -> None:
super().__init__(file_path + ".lock")

lock_path = tmp_path / "a"
lock = FilePathLock(str(lock_path))
assert lock.lock_file == str(lock_path) + ".lock"

0 comments on commit 9a979df

Please sign in to comment.