Skip to content

Commit af00c7e

Browse files
committed
feat: Add basic typing support
1 parent 928fd22 commit af00c7e

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

factory/base.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44
import collections
55
import logging
66
import warnings
7+
from typing import Generic, List, TypeVar
78

89
from . import builder, declarations, enums, errors, utils
910

1011
logger = 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

factory/django.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import logging
1010
import os
1111
import warnings
12+
from typing import TypeVar
1213

1314
from django.contrib.auth.hashers import make_password
1415
from django.core import files as django_files
@@ -20,7 +21,7 @@
2021

2122

2223
DEFAULT_DB_ALIAS = 'default' # Same as django.db.DEFAULT_DB_ALIAS
23-
24+
T = TypeVar("T")
2425

2526
_LAZY_LOADS = {}
2627

@@ -72,7 +73,7 @@ def get_model_class(self):
7273
return self.model
7374

7475

75-
class DjangoModelFactory(base.Factory):
76+
class DjangoModelFactory(base.Factory[T]):
7677
"""Factory for Django models.
7778
7879
This makes sure that the 'sequence' field of created objects is a new id.

0 commit comments

Comments
 (0)