Skip to content

Commit 792f73e

Browse files
committed
callable_marker: add type hints
Signed-off-by: Gaëtan Lehmann <[email protected]>
1 parent d01aa98 commit 792f73e

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

lib/common.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import traceback
1010
from enum import Enum
1111
from pydantic import TypeAdapter, ValidationError
12-
from typing import Any, Dict, Literal, Optional, Type, TypeVar, cast, overload, TYPE_CHECKING, Union
12+
from typing import Any, Callable, Dict, Literal, Optional, Type, TypeVar, cast, overload, TYPE_CHECKING, Union
1313
from uuid import UUID
1414

1515
import pytest
@@ -90,7 +90,7 @@ def ensure_type(typ: Type[T], value: Any) -> T:
9090
return ta.validate_python(value)
9191
raise TypeError(f"'{type(value).__name__}' object is not of the expected type '{typ.__name__}'")
9292

93-
def callable_marker(value, request):
93+
def callable_marker(value: Union[T, Callable[..., T]], request: pytest.FixtureRequest) -> T:
9494
"""
9595
Process value optionally generated by fixture-dependent callable.
9696
@@ -106,8 +106,12 @@ def callable_marker(value, request):
106106
for arg_name in inspect.getfullargspec(value).args}
107107
except pytest.FixtureLookupError as e:
108108
raise RuntimeError("fixture in mapping not found on test") from e
109-
value = value(**params)
110-
return value
109+
# callable ensures the value is of type Callable[..., object], which is not enough in that case
110+
# we can trust the static checker though, and thus use cast
111+
fn = cast(Callable[..., T], value)
112+
return fn(**params)
113+
else:
114+
return value
111115

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

tests/install/conftest.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import logging
22
import os
3-
from typing import Sequence
3+
from typing import Callable, Sequence, Union
44
import pytest
55
import pytest_dependency # type: ignore
66
import tempfile
77
import xml.etree.ElementTree as ET
88

99
from lib import installer, pxe
10-
from lib.common import callable_marker, url_download, wait_for
10+
from lib.common import callable_marker, ensure_type, url_download, wait_for
1111
from lib.installer import AnswerFile
1212
from lib.commands import local_cmd
1313

@@ -66,8 +66,8 @@ def answerfile(request):
6666
return
6767

6868
# construct answerfile definition from option "base", and explicit bits
69-
answerfile_def = callable_marker(marker.args[0], request)
70-
assert isinstance(answerfile_def, AnswerFile)
69+
marker_args = ensure_type(Sequence[Union[AnswerFile, Callable[..., AnswerFile]]], marker.args)
70+
answerfile_def = callable_marker(marker_args[0], request)
7171

7272
answerfile_def.top_append(
7373
dict(TAG="admin-interface",
@@ -329,8 +329,8 @@ def xcpng_chained(request):
329329
# take test name from mark
330330
marker = request.node.get_closest_marker("continuation_of")
331331
assert marker is not None, "xcpng_chained fixture requires 'continuation_of' marker"
332-
continuation_of = callable_marker(marker.args[0], request)
333-
assert isinstance(continuation_of, Sequence)
332+
marker_args = ensure_type(Sequence[Union[Sequence[dict], Callable[..., Sequence[dict]]]], marker.args)
333+
continuation_of = callable_marker(marker_args[0], request)
334334

335335
vm_defs = [dict(name=vm_spec['vm'],
336336
image_test=vm_spec['image_test'],

0 commit comments

Comments
 (0)