Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add generic typing to factories #1100

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ dist/
htmlcov/
MANIFEST
tags

# Virtualenv
.venv/
venv/
2 changes: 1 addition & 1 deletion factory/alchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def _build_default_options(self):
]


class SQLAlchemyModelFactory(base.Factory):
class SQLAlchemyModelFactory(base.Factory[base.T]):
"""Factory for SQLAlchemy models. """

_options_class = SQLAlchemyOptions
Expand Down
10 changes: 5 additions & 5 deletions factory/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ def __init__(self, **kwargs):
setattr(self, field, value)


class StubFactory(Factory):
class StubFactory(Factory[StubObject]):

class Meta:
strategy = enums.STUB_STRATEGY
Expand All @@ -681,7 +681,7 @@ def create(cls, **kwargs):
raise errors.UnsupportedStrategy()


class BaseDictFactory(Factory):
class BaseDictFactory(Factory[T]):
"""Factory for dictionary-like classes."""
class Meta:
abstract = True
Expand All @@ -698,12 +698,12 @@ def _create(cls, model_class, *args, **kwargs):
return cls._build(model_class, *args, **kwargs)


class DictFactory(BaseDictFactory):
class DictFactory(BaseDictFactory[dict]):
class Meta:
model = dict


class BaseListFactory(Factory):
class BaseListFactory(Factory[T]):
"""Factory for list-like classes."""
class Meta:
abstract = True
Expand All @@ -724,7 +724,7 @@ def _create(cls, model_class, *args, **kwargs):
return cls._build(model_class, *args, **kwargs)


class ListFactory(BaseListFactory):
class ListFactory(BaseListFactory[list]):
class Meta:
model = list

Expand Down
2 changes: 1 addition & 1 deletion factory/mogo.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from . import base


class MogoFactory(base.Factory):
class MogoFactory(base.Factory[base.T]):
"""Factory for mogo objects."""
class Meta:
abstract = True
Expand Down
2 changes: 1 addition & 1 deletion factory/mongoengine.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from . import base


class MongoEngineFactory(base.Factory):
class MongoEngineFactory(base.Factory[base.T]):
"""Factory for mongoengine objects."""

class Meta:
Expand Down
25 changes: 19 additions & 6 deletions tests/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import dataclasses
import unittest

from typing_extensions import assert_type

import factory


Expand All @@ -14,9 +16,7 @@ class User:


class TypingTests(unittest.TestCase):

def test_simple_factory(self) -> None:

class UserFactory(factory.Factory[User]):
name = "John Doe"
email = "[email protected]"
Expand All @@ -25,7 +25,20 @@ class UserFactory(factory.Factory[User]):
class Meta:
model = User

result: User
result = UserFactory.build()
result = UserFactory.create()
self.assertEqual(result.name, "John Doe")
assert_type(UserFactory.build(), User)
assert_type(UserFactory.create(), User)
assert_type(UserFactory.build_batch(2), list[User])
assert_type(UserFactory.create_batch(2), list[User])
self.assertEqual(UserFactory.create().name, "John Doe")

def test_dict_factory(self) -> None:
class Pet(factory.DictFactory):
species = "dog"
name = "rover"

self.assertTrue(isinstance(Pet.build(), dict))
self.assertTrue(isinstance(Pet.create(), dict))

def test_list_factory(self) -> None:
self.assertTrue(isinstance(factory.ListFactory.build(), list))
self.assertTrue(isinstance(factory.ListFactory.create(), list))
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ python =
[testenv]
deps =
mypy
typing_extensions
alchemy: SQLAlchemy
mongo: mongoengine
mongo: mongomock
Expand Down