Skip to content

Commit 3a79343

Browse files
authored
Fix TypeError: _CountedFileLock.__init__() got an unexpected keyword argument 'timeout' (#345)
* Fix TypeError: _CountedFileLock.__init__() got an unexpected keyword argument 'timeout' * fix * fix type check * ignore Skipping analyzing "virtualenv": module is installed, but missing library stubs or py.typed marker [import-untyped]
1 parent 81d4cf9 commit 3a79343

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

src/filelock/_api.py

+20-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import contextlib
4+
import inspect
45
import logging
56
import os
67
import time
@@ -114,15 +115,26 @@ def __call__( # noqa: PLR0913
114115
msg += f"\n\t{param_name} (existing lock has {set_param} but {passed_param} was passed)"
115116
raise ValueError(msg)
116117

117-
instance = super().__call__(
118-
lock_file=lock_file,
119-
timeout=timeout,
120-
mode=mode,
121-
thread_local=thread_local,
122-
blocking=blocking,
123-
is_singleton=is_singleton,
118+
# Workaround to make `__init__`'s params optional in subclasses
119+
# E.g. virtualenv changes the signature of the `__init__` method in the `BaseFileLock` class descendant
120+
# (https://github.com/tox-dev/filelock/pull/340)
121+
122+
all_params = {
123+
"lock_file": lock_file,
124+
"timeout": timeout,
125+
"mode": mode,
126+
"thread_local": thread_local,
127+
"blocking": blocking,
128+
"is_singleton": is_singleton,
124129
**kwargs,
125-
)
130+
}
131+
132+
present_params = set(inspect.signature(cls.__init__).parameters) # type: ignore[misc]
133+
init_params = {key: value for key, value in all_params.items() if key in present_params}
134+
# The `lock_file` parameter is required
135+
init_params["lock_file"] = lock_file
136+
137+
instance = super().__call__(**init_params)
126138

127139
if is_singleton:
128140
cls._instances[str(lock_file)] = instance # type: ignore[attr-defined]

tests/test_virtualenv.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from typing import TYPE_CHECKING
44

5-
from virtualenv import cli_run
5+
from virtualenv import cli_run # type: ignore[import-untyped]
66

77
if TYPE_CHECKING:
88
from pathlib import Path

0 commit comments

Comments
 (0)