From 734534f329e0fb72b4584fa1d57c4b83a55bc886 Mon Sep 17 00:00:00 2001 From: Kwist Date: Wed, 19 Jun 2024 19:06:22 +0300 Subject: [PATCH 1/4] Fix TypeError: _CountedFileLock.__init__() got an unexpected keyword argument 'timeout' --- src/filelock/_api.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/filelock/_api.py b/src/filelock/_api.py index 96beffa4..b5622b6b 100644 --- a/src/filelock/_api.py +++ b/src/filelock/_api.py @@ -1,6 +1,7 @@ from __future__ import annotations import contextlib +import inspect import logging import os import time @@ -114,15 +115,24 @@ def __call__( # noqa: PLR0913 msg += f"\n\t{param_name} (existing lock has {set_param} but {passed_param} was passed)" raise ValueError(msg) - instance = super().__call__( - lock_file=lock_file, - timeout=timeout, - mode=mode, - thread_local=thread_local, - blocking=blocking, - is_singleton=is_singleton, + # Workaround to make `__init__`'s params optional in subclasses + # E.g. virtualenv changes the `__init__` signature of the `BaseFileLock` class + # (https://github.com/tox-dev/filelock/pull/340) + + all_params = { + "lock_file": lock_file, + "timeout": timeout, + "mode": mode, + "thread_local": thread_local, + "blocking": blocking, + "is_singleton": is_singleton, **kwargs, - ) + } + + present_params = set(inspect.signature(cls.__init__).parameters) + init_params = {key: value for key, value in all_params.items() if key in present_params} + + instance = super().__call__(**init_params) if is_singleton: cls._instances[str(lock_file)] = instance # type: ignore[attr-defined] From 3fd40fa165e943ebbbadbcef1aea74229e7ea0e1 Mon Sep 17 00:00:00 2001 From: Kwist Date: Wed, 19 Jun 2024 19:12:44 +0300 Subject: [PATCH 2/4] fix --- src/filelock/_api.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/filelock/_api.py b/src/filelock/_api.py index b5622b6b..4109ea28 100644 --- a/src/filelock/_api.py +++ b/src/filelock/_api.py @@ -116,7 +116,7 @@ def __call__( # noqa: PLR0913 raise ValueError(msg) # Workaround to make `__init__`'s params optional in subclasses - # E.g. virtualenv changes the `__init__` signature of the `BaseFileLock` class + # E.g. virtualenv changes the signature of the `__init__` method in the `BaseFileLock` class descendant # (https://github.com/tox-dev/filelock/pull/340) all_params = { @@ -131,6 +131,8 @@ def __call__( # noqa: PLR0913 present_params = set(inspect.signature(cls.__init__).parameters) 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) From 95b4bd522908fac22de3be0d2f4eef40f95d61c1 Mon Sep 17 00:00:00 2001 From: Kwist Date: Wed, 19 Jun 2024 19:17:37 +0300 Subject: [PATCH 3/4] fix type check --- src/filelock/_api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/filelock/_api.py b/src/filelock/_api.py index 4109ea28..7db0e010 100644 --- a/src/filelock/_api.py +++ b/src/filelock/_api.py @@ -129,7 +129,7 @@ def __call__( # noqa: PLR0913 **kwargs, } - present_params = set(inspect.signature(cls.__init__).parameters) + present_params = set(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 From 560e64600e38bd9eb9368cae0763ee50338d77f4 Mon Sep 17 00:00:00 2001 From: Kwist Date: Wed, 19 Jun 2024 19:22:35 +0300 Subject: [PATCH 4/4] ignore Skipping analyzing "virtualenv": module is installed, but missing library stubs or py.typed marker [import-untyped] --- tests/test_virtualenv.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_virtualenv.py b/tests/test_virtualenv.py index 85c64f84..9d868e79 100644 --- a/tests/test_virtualenv.py +++ b/tests/test_virtualenv.py @@ -2,7 +2,7 @@ from typing import TYPE_CHECKING -from virtualenv import cli_run +from virtualenv import cli_run # type: ignore[import-untyped] if TYPE_CHECKING: from pathlib import Path