9
9
import traceback
10
10
from enum import Enum
11
11
from pydantic import TypeAdapter , ValidationError
12
- from typing import Any , Dict , Literal , Optional , Type , TypeVar , overload , TYPE_CHECKING , Union
12
+ from typing import Any , Callable , Dict , Literal , Optional , Type , TypeVar , cast , overload , TYPE_CHECKING , Union
13
13
from uuid import UUID
14
14
15
15
import pytest
@@ -69,7 +69,6 @@ def expand_scope_relative_nodeid(scoped_nodeid, scope, ref_nodeid):
69
69
logging .debug ("scope: %r base: %r relative: %r" , scope , base , scoped_nodeid )
70
70
return "::" .join (itertools .chain (base , (scoped_nodeid ,)))
71
71
72
- def callable_marker (value , request ):
73
72
T = TypeVar ("T" )
74
73
75
74
_ensure_type_cache : Dict [type , TypeAdapter ] = {}
@@ -97,6 +96,7 @@ def ensure_type(typ: Type[T], value: Any) -> T:
97
96
raise TypeError (f"'{ type (value ).__name__ } ' object is not of the expected type '{ typ .__name__ } '" )
98
97
return value
99
98
99
+ def callable_marker (value : Union [T , Callable [[], T ]], request : pytest .FixtureRequest ) -> T :
100
100
"""
101
101
Process value optionally generated by fixture-dependent callable.
102
102
@@ -112,8 +112,11 @@ def ensure_type(typ: Type[T], value: Any) -> T:
112
112
for arg_name in inspect .getfullargspec (value ).args }
113
113
except pytest .FixtureLookupError as e :
114
114
raise RuntimeError ("fixture in mapping not found on test" ) from e
115
- value = value (** params )
116
- return value
115
+ # callable ensures the value is of type Callable[..., object], which is not enough in that case
116
+ # we can trust the static checker though, and thus use cast
117
+ return cast (Callable [[], T ], value )(** params )
118
+ else :
119
+ return value
117
120
118
121
def wait_for (fn , msg = None , timeout_secs = 2 * 60 , retry_delay_secs = 2 , invert = False ):
119
122
if msg is not None :
0 commit comments