Skip to content

Commit

Permalink
feat: Add basic typing support
Browse files Browse the repository at this point in the history
  • Loading branch information
last-partizan committed Jun 10, 2022
1 parent 869e1b0 commit 17ef2b8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
15 changes: 9 additions & 6 deletions factory/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
import collections
import logging
import warnings
from typing import Generic, List, TypeVar

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

logger = logging.getLogger('factory.generate')

T = TypeVar('T')

# Factory metaclasses


Expand Down Expand Up @@ -405,7 +408,7 @@ def reset(self, next_value=0):
self.seq = next_value


class BaseFactory:
class BaseFactory(Generic[T]):
"""Factory base support for sequences, attributes and stubs."""

# Backwards compatibility
Expand Down Expand Up @@ -506,12 +509,12 @@ def _create(cls, model_class, *args, **kwargs):
return model_class(*args, **kwargs)

@classmethod
def build(cls, **kwargs):
def build(cls, **kwargs) -> T:
"""Build an instance of the associated class, with overridden attrs."""
return cls._generate(enums.BUILD_STRATEGY, kwargs)

@classmethod
def build_batch(cls, size, **kwargs):
def build_batch(cls, size, **kwargs) -> List[T]:
"""Build a batch of instances of the given class, with overridden attrs.
Args:
Expand All @@ -523,12 +526,12 @@ def build_batch(cls, size, **kwargs):
return [cls.build(**kwargs) for _ in range(size)]

@classmethod
def create(cls, **kwargs):
def create(cls, **kwargs) -> T:
"""Create an instance of the associated class, with overridden attrs."""
return cls._generate(enums.CREATE_STRATEGY, kwargs)

@classmethod
def create_batch(cls, size, **kwargs):
def create_batch(cls, size, **kwargs) -> List[T]:
"""Create a batch of instances of the given class, with overridden attrs.
Args:
Expand Down Expand Up @@ -627,7 +630,7 @@ def simple_generate_batch(cls, create, size, **kwargs):
return cls.generate_batch(strategy, size, **kwargs)


class Factory(BaseFactory, metaclass=FactoryMetaClass):
class Factory(BaseFactory[T], metaclass=FactoryMetaClass):
"""Factory base with build and create support.
This class has the ability to support multiple ORMs by using custom creation
Expand Down
5 changes: 3 additions & 2 deletions factory/django.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import logging
import os
import warnings
from typing import TypeVar

from django.contrib.auth.hashers import make_password
from django.core import files as django_files
Expand All @@ -20,7 +21,7 @@


DEFAULT_DB_ALIAS = 'default' # Same as django.db.DEFAULT_DB_ALIAS

T = TypeVar("T")

_LAZY_LOADS = {}

Expand Down Expand Up @@ -72,7 +73,7 @@ def get_model_class(self):
return self.model


class DjangoModelFactory(base.Factory):
class DjangoModelFactory(base.Factory[T]):
"""Factory for Django models.
This makes sure that the 'sequence' field of created objects is a new id.
Expand Down

0 comments on commit 17ef2b8

Please sign in to comment.