Skip to content

Commit 0e5db08

Browse files
glehmannydirson
authored andcommitted
callable_marker: add type hints
Signed-off-by: Gaëtan Lehmann <[email protected]>
1 parent 188f140 commit 0e5db08

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

lib/common.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import time
88
import traceback
99
from enum import Enum
10-
from typing import Dict, Literal, Optional, overload, TYPE_CHECKING, Union
10+
from typing import Callable, cast, Dict, Literal, Optional, overload, TYPE_CHECKING, TypeVar, Union
1111
from uuid import UUID
1212

1313
import pytest
@@ -17,6 +17,8 @@
1717
if TYPE_CHECKING:
1818
import lib.host
1919

20+
T = TypeVar("T")
21+
2022
class PackageManagerEnum(Enum):
2123
UNKNOWN = 1
2224
RPM = 2
@@ -67,7 +69,7 @@ def expand_scope_relative_nodeid(scoped_nodeid, scope, ref_nodeid):
6769
logging.debug("scope: %r base: %r relative: %r", scope, base, scoped_nodeid)
6870
return "::".join(itertools.chain(base, (scoped_nodeid,)))
6971

70-
def callable_marker(value, request):
72+
def callable_marker(value: Union[T, Callable[..., T]], request: pytest.FixtureRequest) -> T:
7173
"""
7274
Process value optionally generated by fixture-dependent callable.
7375
@@ -83,8 +85,12 @@ def callable_marker(value, request):
8385
for arg_name in inspect.getfullargspec(value).args}
8486
except pytest.FixtureLookupError as e:
8587
raise RuntimeError("fixture in mapping not found on test") from e
86-
value = value(**params)
87-
return value
88+
# callable ensures the value is of type Callable[..., object], which is not enough in that case
89+
# we can trust the static checker though, and thus use cast
90+
fn = cast(Callable[..., T], value)
91+
return fn(**params)
92+
else:
93+
return value
8894

8995
def wait_for(fn, msg=None, timeout_secs=2 * 60, retry_delay_secs=2, invert=False):
9096
if msg is not None:

0 commit comments

Comments
 (0)