1010from dataclasses import dataclass
1111from inspect import signature
1212from types import MethodType
13- from typing import TYPE_CHECKING , Any , Callable , Generic , TypeVar , cast , overload
13+ from typing import TYPE_CHECKING , Callable , Generic , TypeVar , cast , overload
1414
1515import factory
1616import factory .enums
4545
4646
4747@dataclass (eq = False )
48- class DeferredFunction (Generic [T ]):
48+ class DeferredFunction (Generic [T , U ]):
4949 name : str
5050 factory : type [Factory [T ]]
5151 is_related : bool
52- function : Callable [[SubRequest ], Any ]
52+ function : Callable [[SubRequest ], U ]
5353
54- def __call__ (self , request : SubRequest ) -> Any :
54+ def __call__ (self , request : SubRequest ) -> U :
5555 return self .function (request )
5656
5757
@@ -76,21 +76,33 @@ def named_model(model_cls: type[T], name: str) -> type[T]:
7676# @register
7777# class AuthorFactory(Factory): ...
7878@overload
79- def register (factory_class : type [Factory [T ]], _name : str | None = None , ** kwargs : Any ) -> type [Factory [T ]]: ...
79+ def register (
80+ factory_class : type [Factory [T ]],
81+ _name : str | None = ...,
82+ * ,
83+ _caller_locals : Box [dict [str , object ]] | None = ...,
84+ ** kwargs : object ,
85+ ) -> type [Factory [T ]]: ...
8086
8187
8288# @register(...)
8389# class AuthorFactory(Factory): ...
8490@overload
85- def register (* , _name : str | None = None , ** kwargs : Any ) -> Callable [[type [Factory [T ]]], type [Factory [T ]]]: ...
91+ def register (
92+ factory_class : None ,
93+ _name : str | None = ...,
94+ * ,
95+ _caller_locals : Box [dict [str , object ]] | None = ...,
96+ ** kwargs : object ,
97+ ) -> Callable [[type [Factory [T ]]], type [Factory [T ]]]: ...
8698
8799
88100def register (
89101 factory_class : type [Factory [T ]] | None = None ,
90102 _name : str | None = None ,
91103 * ,
92- _caller_locals : Box [dict [str , Any ]] | None = None ,
93- ** kwargs : Any ,
104+ _caller_locals : Box [dict [str , object ]] | None = None ,
105+ ** kwargs : object ,
94106) -> type [Factory [T ]] | Callable [[type [Factory [T ]]], type [Factory [T ]]]:
95107 r"""Register fixtures for the factory class.
96108
@@ -141,9 +153,9 @@ def generate_fixtures(
141153 factory_class : type [Factory [T ]],
142154 model_name : str ,
143155 factory_name : str ,
144- overrides : Mapping [str , Any ],
145- caller_locals : Box [Mapping [str , Any ]],
146- ) -> Iterable [tuple [str , Callable [..., Any ]]]:
156+ overrides : Mapping [str , object ],
157+ caller_locals : Box [Mapping [str , object ]],
158+ ) -> Iterable [tuple [str , Callable [..., object ]]]:
147159 """Generate all the FixtureDefs for the given factory class."""
148160
149161 related : list [str ] = []
@@ -199,10 +211,10 @@ def create_fixture_with_related(
199211
200212def make_declaration_fixturedef (
201213 attr_name : str ,
202- value : Any ,
214+ value : object ,
203215 factory_class : type [Factory [T ]],
204216 related : list [str ],
205- ) -> Callable [..., Any ]:
217+ ) -> Callable [[ SubRequest ], object ]:
206218 """Create the FixtureDef for a factory declaration."""
207219 if isinstance (value , (SubFactory , RelatedFactory )):
208220 subfactory_class : type [Factory [object ]] = value .get_factory ()
@@ -246,7 +258,7 @@ def make_declaration_fixturedef(
246258 )
247259
248260
249- def inject_into_caller (name : str , function : Callable [..., Any ], locals_ : Box [dict [str , Any ]]) -> None :
261+ def inject_into_caller (name : str , function : Callable [..., object ], locals_ : Box [dict [str , object ]]) -> None :
250262 """Inject a function into the caller's locals, making sure that the function will work also within classes."""
251263 # We need to check if the caller frame is a class, since in that case the first argument is the class itself.
252264 # In that case, we can apply the staticmethod() decorator to the injected function, so that the first param
@@ -302,7 +314,7 @@ def get_deps(
302314 model_name = get_model_name (factory_class ) if model_name is None else model_name
303315 parent_model_name = get_model_name (parent_factory_class ) if parent_factory_class is not None else None
304316
305- def is_dep (value : Any ) -> bool :
317+ def is_dep (value : object ) -> bool :
306318 if isinstance (value , RelatedFactory ):
307319 return False
308320 if isinstance (value , SubFactory ):
@@ -325,7 +337,7 @@ def evaluate(request: SubRequest, value: LazyFixture[T] | T) -> T:
325337 return value .evaluate (request ) if isinstance (value , LazyFixture ) else value
326338
327339
328- def noop (* args : Any , ** kwargs : Any ) -> None :
340+ def noop (* args : object , ** kwargs : object ) -> None :
329341 """No-op function."""
330342 pass
331343
@@ -388,7 +400,7 @@ def model_fixture(request: SubRequest, factory_name: str) -> object:
388400 request ._fixture_defs [fixture_name ] = request ._fixturedef
389401
390402 # Defer post-generation declarations
391- deferred : list [DeferredFunction [object ]] = []
403+ deferred : list [DeferredFunction [object , object ]] = []
392404
393405 for attr in factory_class ._meta .post_declarations .sorted ():
394406 decl = factory_class ._meta .post_declarations .declarations [attr ]
@@ -427,7 +439,7 @@ def model_fixture(request: SubRequest, factory_name: str) -> object:
427439 return instance
428440
429441
430- def make_deferred_related (factory : type [Factory [T ]], fixture : str , attr : str ) -> DeferredFunction [T ]:
442+ def make_deferred_related (factory : type [Factory [T ]], fixture : str , attr : str ) -> DeferredFunction [T , object ]:
431443 """Make deferred function for the related factory declaration.
432444
433445 :param factory: Factory class.
@@ -438,7 +450,7 @@ def make_deferred_related(factory: type[Factory[T]], fixture: str, attr: str) ->
438450 """
439451 name = SEPARATOR .join ((fixture , attr ))
440452
441- def deferred_impl (request : SubRequest ) -> Any :
453+ def deferred_impl (request : SubRequest ) -> object :
442454 return request .getfixturevalue (name )
443455
444456 return DeferredFunction (
@@ -453,11 +465,11 @@ def make_deferred_postgen(
453465 step : BuildStep ,
454466 factory_class : type [Factory [T ]],
455467 fixture : str ,
456- instance : Any ,
468+ instance : T ,
457469 attr : str ,
458470 declaration : PostGenerationDeclaration ,
459471 context : PostGenerationContext ,
460- ) -> DeferredFunction [T ]:
472+ ) -> DeferredFunction [T , object ]:
461473 """Make deferred function for the post-generation declaration.
462474
463475 :param step: factory_boy builder step.
@@ -472,7 +484,7 @@ def make_deferred_postgen(
472484 """
473485 name = SEPARATOR .join ((fixture , attr ))
474486
475- def deferred_impl (request : SubRequest ) -> Any :
487+ def deferred_impl (request : SubRequest ) -> object :
476488 return declaration .call (instance , step , context )
477489
478490 return DeferredFunction (
@@ -493,13 +505,13 @@ def attr_fixture(request: SubRequest, value: T) -> T:
493505 return value
494506
495507
496- def subfactory_fixture (request : SubRequest , factory_class : type [Factory [object ]]) -> Any :
508+ def subfactory_fixture (request : SubRequest , factory_class : type [Factory [object ]]) -> object :
497509 """SubFactory/RelatedFactory fixture implementation."""
498510 fixture = inflection .underscore (factory_class ._meta .model .__name__ )
499511 return request .getfixturevalue (fixture )
500512
501513
502- def get_caller_locals (depth : int = 0 ) -> dict [str , Any ]:
514+ def get_caller_locals (depth : int = 0 ) -> dict [str , object ]:
503515 """Get the local namespace of the caller frame."""
504516 return sys ._getframe (depth + 2 ).f_locals
505517
0 commit comments