14
14
15
15
from .codegen import make_fixture_model_module , FixtureDef
16
16
from .compat import PostGenerationContext
17
- from typing import TYPE_CHECKING , overload
18
- from typing_extensions import Protocol
17
+ from typing import TYPE_CHECKING , overload , cast
18
+ from typing_extensions import Protocol , TypeAlias
19
19
20
20
if TYPE_CHECKING :
21
21
from typing import Any , Callable , TypeVar
22
- from _pytest .fixtures import FixtureRequest
22
+ from _pytest .fixtures import SubRequest , FixtureFunction
23
23
from factory .builder import BuildStep
24
24
from factory .declarations import PostGeneration
25
25
from factory .declarations import PostGenerationContext
26
26
27
- FactoryType = type [ factory .Factory ]
27
+ FactoryType : TypeAlias = factory .Factory
28
28
T = TypeVar ("T" )
29
29
F = TypeVar ("F" , bound = FactoryType )
30
30
@@ -37,9 +37,9 @@ class DeferredFunction:
37
37
name : str
38
38
factory : FactoryType
39
39
is_related : bool
40
- function : Callable [[FixtureRequest ], Any ]
40
+ function : Callable [[SubRequest ], Any ]
41
41
42
- def __call__ (self , request : FixtureRequest ) -> Any :
42
+ def __call__ (self , request : SubRequest ) -> Any :
43
43
return self .function (request )
44
44
45
45
@@ -51,7 +51,7 @@ def __call__(self, factory_class: F, _name: str | None = None, **kwargs: Any) ->
51
51
52
52
53
53
@overload
54
- def register (
54
+ def register ( # type: ignore[misc]
55
55
factory_class : None = None ,
56
56
_name : str | None = None ,
57
57
** kwargs : Any ,
@@ -177,7 +177,7 @@ def register(
177
177
return factory_class
178
178
179
179
180
- def inject_into_caller (name : str , function : Callable , locals_ : dict [str , Any ]) -> None :
180
+ def inject_into_caller (name : str , function : Callable [..., Any ] , locals_ : dict [str , Any ]) -> None :
181
181
"""Inject a function into the caller's locals, making sure that the function will work also within classes."""
182
182
# We need to check if the caller frame is a class, since in that case the first argument is the class itself.
183
183
# In that case, we can apply the staticmethod() decorator to the injected function, so that the first param
@@ -191,7 +191,7 @@ def inject_into_caller(name: str, function: Callable, locals_: dict[str, Any]) -
191
191
# Therefore, we can just check for __qualname__ to figure out if we are in a class, and apply the @staticmethod.
192
192
is_class_or_function = "__qualname__" in locals_
193
193
if is_class_or_function :
194
- function = staticmethod (function )
194
+ function = staticmethod (function ) # type: ignore[assignment]
195
195
196
196
locals_ [name ] = function
197
197
@@ -238,20 +238,21 @@ def is_dep(value: Any) -> bool:
238
238
]
239
239
240
240
241
- def evaluate (request : FixtureRequest , value : LazyFixture | Any ) -> Any :
241
+ def evaluate (request : SubRequest , value : LazyFixture | Any ) -> Any :
242
242
"""Evaluate the declaration (lazy fixtures, etc)."""
243
243
return value .evaluate (request ) if isinstance (value , LazyFixture ) else value
244
244
245
245
246
- def model_fixture (request : FixtureRequest , factory_name : str ) -> Any :
246
+ def model_fixture (request : SubRequest , factory_name : str ) -> Any :
247
247
"""Model fixture implementation."""
248
248
factoryboy_request = request .getfixturevalue ("factoryboy_request" )
249
249
250
250
# Try to evaluate as much post-generation dependencies as possible
251
251
factoryboy_request .evaluate (request )
252
252
253
+ fixture_name = str (request .fixturename )
253
254
factory_class : FactoryType = request .getfixturevalue (factory_name )
254
- prefix = "" .join ((request . fixturename , SEPARATOR ))
255
+ prefix = "" .join ((fixture_name , SEPARATOR ))
255
256
256
257
# Create model fixture instance
257
258
@@ -279,7 +280,7 @@ class Factory(factory_class):
279
280
280
281
# Cache the instance value on pytest level so that the fixture can be resolved before the return
281
282
request ._fixturedef .cached_result = (instance , 0 , None )
282
- request ._fixture_defs [request . fixturename ] = request ._fixturedef
283
+ request ._fixture_defs [fixture_name ] = request ._fixturedef
283
284
284
285
# Defer post-generation declarations
285
286
deferred : list [DeferredFunction ] = []
@@ -289,7 +290,7 @@ class Factory(factory_class):
289
290
decl = factory_class ._meta .post_declarations .declarations [attr ]
290
291
291
292
if isinstance (decl , factory .RelatedFactory ):
292
- deferred .append (make_deferred_related (factory_class , request . fixturename , attr ))
293
+ deferred .append (make_deferred_related (factory_class , fixture_name , attr ))
293
294
else :
294
295
argname = "" .join ((prefix , attr ))
295
296
extra = {}
@@ -309,7 +310,7 @@ class Factory(factory_class):
309
310
extra = extra ,
310
311
)
311
312
deferred .append (
312
- make_deferred_postgen (step , factory_class , request . fixturename , instance , attr , decl , postgen_context )
313
+ make_deferred_postgen (step , factory_class , fixture_name , instance , attr , decl , postgen_context )
313
314
)
314
315
factoryboy_request .defer (deferred )
315
316
@@ -329,7 +330,7 @@ def make_deferred_related(factory: FactoryType, fixture: str, attr: str) -> Defe
329
330
"""
330
331
name = SEPARATOR .join ((fixture , attr ))
331
332
332
- def deferred_impl (request : FixtureRequest ) -> Any :
333
+ def deferred_impl (request : SubRequest ) -> Any :
333
334
return request .getfixturevalue (name )
334
335
335
336
return DeferredFunction (
@@ -362,7 +363,7 @@ def make_deferred_postgen(
362
363
"""
363
364
name = SEPARATOR .join ((fixture , attr ))
364
365
365
- def deferred_impl (request : FixtureRequest ) -> Any :
366
+ def deferred_impl (request : SubRequest ) -> Any :
366
367
return declaration .call (instance , step , context )
367
368
368
369
return DeferredFunction (
@@ -373,17 +374,17 @@ def deferred_impl(request: FixtureRequest) -> Any:
373
374
)
374
375
375
376
376
- def factory_fixture (request : FixtureRequest , factory_class : F ) -> F :
377
+ def factory_fixture (request : SubRequest , factory_class : F ) -> F :
377
378
"""Factory fixture implementation."""
378
379
return factory_class
379
380
380
381
381
- def attr_fixture (request : FixtureRequest , value : T ) -> T :
382
+ def attr_fixture (request : SubRequest , value : T ) -> T :
382
383
"""Attribute fixture implementation."""
383
384
return value
384
385
385
386
386
- def subfactory_fixture (request : FixtureRequest , factory_class : FactoryType ) -> Any :
387
+ def subfactory_fixture (request : SubRequest , factory_class : FactoryType ) -> Any :
387
388
"""SubFactory/RelatedFactory fixture implementation."""
388
389
fixture = inflection .underscore (factory_class ._meta .model .__name__ )
389
390
return request .getfixturevalue (fixture )
@@ -397,7 +398,7 @@ def get_caller_locals(depth: int = 2) -> dict[str, Any]:
397
398
class LazyFixture :
398
399
"""Lazy fixture."""
399
400
400
- def __init__ (self , fixture : Callable | str ) -> None :
401
+ def __init__ (self , fixture : FixtureFunction | str ) -> None :
401
402
"""Lazy pytest fixture wrapper.
402
403
403
404
:param fixture: Fixture name or callable with dependencies.
@@ -409,7 +410,7 @@ def __init__(self, fixture: Callable | str) -> None:
409
410
else :
410
411
self .args = [self .fixture ]
411
412
412
- def evaluate (self , request : FixtureRequest ) -> Any :
413
+ def evaluate (self , request : SubRequest ) -> Any :
413
414
"""Evaluate the lazy fixture.
414
415
415
416
:param request: pytest request object.
0 commit comments