-
Notifications
You must be signed in to change notification settings - Fork 400
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Only `Factory.build()` and `Factory.create()` are properly typed, provided the class is declared as `class UserFactory(Factory[User]):`. Relies on mypy for tests. Reviewed-By: Raphaël Barrois <[email protected]>
- Loading branch information
1 parent
69809cf
commit 68de8e7
Showing
9 changed files
with
59 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
# Copyright: See the LICENSE file. | ||
|
||
import sys | ||
|
||
from .base import ( | ||
BaseDictFactory, | ||
BaseListFactory, | ||
|
@@ -70,10 +72,10 @@ | |
pass | ||
|
||
__author__ = 'Raphaël Barrois <[email protected]>' | ||
try: | ||
if sys.version_info >= (3, 8): | ||
# Python 3.8+ | ||
import importlib.metadata as importlib_metadata | ||
except ImportError: | ||
else: | ||
import importlib_metadata | ||
|
||
__version__ = importlib_metadata.version("factory_boy") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,7 @@ dev = | |
Django | ||
flake8 | ||
isort | ||
mypy | ||
Pillow | ||
SQLAlchemy | ||
sqlalchemy_utils | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Copyright: See the LICENSE file. | ||
|
||
import dataclasses | ||
import unittest | ||
|
||
import factory | ||
|
||
|
||
@dataclasses.dataclass | ||
class User: | ||
name: str | ||
email: str | ||
id: int | ||
|
||
|
||
class TypingTests(unittest.TestCase): | ||
|
||
def test_simple_factory(self) -> None: | ||
|
||
class UserFactory(factory.Factory[User]): | ||
name = "John Doe" | ||
email = "[email protected]" | ||
id = 42 | ||
|
||
class Meta: | ||
model = User | ||
|
||
result: User | ||
result = UserFactory.build() | ||
result = UserFactory.create() | ||
self.assertEqual(result.name, "John Doe") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
It would also be nice to add the return type for the
__new__
method. While not called directly, it would correctly add type hints (tested with Pylance) when you call the class directly.For example:
Ideally, it would be type-hinted in the metaclass, but it seems like that is not possible.