4
4
import collections
5
5
import logging
6
6
import warnings
7
+ from typing import Generic , List , TypeVar
7
8
8
9
from . import builder , declarations , enums , errors , utils
9
10
10
11
logger = logging .getLogger ('factory.generate' )
11
12
13
+ T = TypeVar ('T' )
14
+
12
15
# Factory metaclasses
13
16
14
17
@@ -405,7 +408,7 @@ def reset(self, next_value=0):
405
408
self .seq = next_value
406
409
407
410
408
- class BaseFactory :
411
+ class BaseFactory ( Generic [ T ]) :
409
412
"""Factory base support for sequences, attributes and stubs."""
410
413
411
414
# Backwards compatibility
@@ -506,12 +509,12 @@ def _create(cls, model_class, *args, **kwargs):
506
509
return model_class (* args , ** kwargs )
507
510
508
511
@classmethod
509
- def build (cls , ** kwargs ):
512
+ def build (cls , ** kwargs ) -> T :
510
513
"""Build an instance of the associated class, with overridden attrs."""
511
514
return cls ._generate (enums .BUILD_STRATEGY , kwargs )
512
515
513
516
@classmethod
514
- def build_batch (cls , size , ** kwargs ):
517
+ def build_batch (cls , size , ** kwargs ) -> List [ T ] :
515
518
"""Build a batch of instances of the given class, with overridden attrs.
516
519
517
520
Args:
@@ -523,12 +526,12 @@ def build_batch(cls, size, **kwargs):
523
526
return [cls .build (** kwargs ) for _ in range (size )]
524
527
525
528
@classmethod
526
- def create (cls , ** kwargs ):
529
+ def create (cls , ** kwargs ) -> T :
527
530
"""Create an instance of the associated class, with overridden attrs."""
528
531
return cls ._generate (enums .CREATE_STRATEGY , kwargs )
529
532
530
533
@classmethod
531
- def create_batch (cls , size , ** kwargs ):
534
+ def create_batch (cls , size , ** kwargs ) -> List [ T ] :
532
535
"""Create a batch of instances of the given class, with overridden attrs.
533
536
534
537
Args:
@@ -627,7 +630,7 @@ def simple_generate_batch(cls, create, size, **kwargs):
627
630
return cls .generate_batch (strategy , size , ** kwargs )
628
631
629
632
630
- class Factory (BaseFactory , metaclass = FactoryMetaClass ):
633
+ class Factory (BaseFactory [ T ] , metaclass = FactoryMetaClass ):
631
634
"""Factory base with build and create support.
632
635
633
636
This class has the ability to support multiple ORMs by using custom creation
0 commit comments