44import collections
55import logging
66import warnings
7+ from typing import Generic , List , TypeVar
78
89from . import builder , declarations , enums , errors , utils
910
1011logger = logging .getLogger ('factory.generate' )
1112
13+ T = TypeVar ('T' )
14+
1215# Factory metaclasses
1316
1417
@@ -405,7 +408,7 @@ def reset(self, next_value=0):
405408 self .seq = next_value
406409
407410
408- class BaseFactory :
411+ class BaseFactory ( Generic [ T ]) :
409412 """Factory base support for sequences, attributes and stubs."""
410413
411414 # Backwards compatibility
@@ -506,12 +509,12 @@ def _create(cls, model_class, *args, **kwargs):
506509 return model_class (* args , ** kwargs )
507510
508511 @classmethod
509- def build (cls , ** kwargs ):
512+ def build (cls , ** kwargs ) -> T :
510513 """Build an instance of the associated class, with overridden attrs."""
511514 return cls ._generate (enums .BUILD_STRATEGY , kwargs )
512515
513516 @classmethod
514- def build_batch (cls , size , ** kwargs ):
517+ def build_batch (cls , size , ** kwargs ) -> List [ T ] :
515518 """Build a batch of instances of the given class, with overridden attrs.
516519
517520 Args:
@@ -523,12 +526,12 @@ def build_batch(cls, size, **kwargs):
523526 return [cls .build (** kwargs ) for _ in range (size )]
524527
525528 @classmethod
526- def create (cls , ** kwargs ):
529+ def create (cls , ** kwargs ) -> T :
527530 """Create an instance of the associated class, with overridden attrs."""
528531 return cls ._generate (enums .CREATE_STRATEGY , kwargs )
529532
530533 @classmethod
531- def create_batch (cls , size , ** kwargs ):
534+ def create_batch (cls , size , ** kwargs ) -> List [ T ] :
532535 """Create a batch of instances of the given class, with overridden attrs.
533536
534537 Args:
@@ -627,7 +630,7 @@ def simple_generate_batch(cls, create, size, **kwargs):
627630 return cls .generate_batch (strategy , size , ** kwargs )
628631
629632
630- class Factory (BaseFactory , metaclass = FactoryMetaClass ):
633+ class Factory (BaseFactory [ T ] , metaclass = FactoryMetaClass ):
631634 """Factory base with build and create support.
632635
633636 This class has the ability to support multiple ORMs by using custom creation
0 commit comments