Skip to content

Commit 6fc6f82

Browse files
committed
Defer Django and sqlalchemy imports
1 parent dca3a16 commit 6fc6f82

File tree

3 files changed

+16
-25
lines changed

3 files changed

+16
-25
lines changed

factory/__init__.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import importlib.metadata
44

5+
from . import alchemy, mogo, mongoengine
56
from .base import (
67
BaseDictFactory,
78
BaseListFactory,
@@ -54,22 +55,10 @@
5455
stub_batch,
5556
)
5657

57-
try:
58-
from . import alchemy
59-
except ImportError:
60-
pass
6158
try:
6259
from . import django
6360
except ImportError:
6461
pass
65-
try:
66-
from . import mogo
67-
except ImportError:
68-
pass
69-
try:
70-
from . import mongoengine
71-
except ImportError:
72-
pass
7362

7463
__author__ = 'Raphaël Barrois <raphael.barrois+fboy@polytechnique.org>'
7564
__version__ = importlib.metadata.version("factory_boy")

factory/alchemy.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
# Copyright: See the LICENSE file.
22

3-
from sqlalchemy.exc import IntegrityError
4-
from sqlalchemy.orm.exc import NoResultFound
5-
63
from . import base, errors
74

85
SESSION_PERSISTENCE_COMMIT = 'commit'
@@ -64,6 +61,9 @@ def _generate(cls, strategy, params):
6461

6562
@classmethod
6663
def _get_or_create(cls, model_class, session, args, kwargs):
64+
from sqlalchemy.exc import IntegrityError
65+
from sqlalchemy.orm.exc import NoResultFound
66+
6767
key_fields = {}
6868
for field in cls._meta.sqlalchemy_get_or_create:
6969
if field not in kwargs:

factory/django.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111
import warnings
1212
from typing import Dict, TypeVar
1313

14-
from django.contrib.auth.hashers import make_password
15-
from django.core import files as django_files
16-
from django.db import IntegrityError
17-
1814
from . import base, declarations, errors
1915

2016
logger = logging.getLogger('factory.generate')
@@ -124,6 +120,7 @@ def _generate(cls, strategy, params):
124120
@classmethod
125121
def _get_or_create(cls, model_class, *args, **kwargs):
126122
"""Create an instance of the model through objects.get_or_create."""
123+
from django.db import IntegrityError
127124
manager = cls._get_manager(model_class)
128125

129126
assert 'defaults' not in cls._meta.django_get_or_create, (
@@ -193,7 +190,10 @@ def _after_postgeneration(cls, instance, create, results=None):
193190

194191

195192
class Password(declarations.Transformer):
196-
def __init__(self, password, transform=make_password, **kwargs):
193+
def __init__(self, password, transform=None, **kwargs):
194+
if transform is None:
195+
from django.contrib.auth.hashers import make_password
196+
transform = make_password
197197
super().__init__(password, transform=transform, **kwargs)
198198

199199

@@ -207,6 +207,7 @@ def _make_data(self, params):
207207
return params.get('data', b'')
208208

209209
def _make_content(self, params):
210+
from django.core.files.base import ContentFile, File
210211
path = ''
211212

212213
from_path = params.get('from_path')
@@ -222,21 +223,21 @@ def _make_content(self, params):
222223
if from_path:
223224
path = from_path
224225
with open(path, 'rb') as f:
225-
content = django_files.base.ContentFile(f.read())
226+
content = ContentFile(f.read())
226227

227228
elif from_file:
228229
f = from_file
229-
content = django_files.File(f)
230+
content = File(f)
230231
path = content.name
231232

232233
elif from_func:
233234
func = from_func
234-
content = django_files.File(func())
235+
content = File(func())
235236
path = content.name
236237

237238
else:
238239
data = self._make_data(params)
239-
content = django_files.base.ContentFile(data)
240+
content = ContentFile(data)
240241

241242
if path:
242243
default_filename = os.path.basename(path)
@@ -248,8 +249,9 @@ def _make_content(self, params):
248249

249250
def evaluate(self, instance, step, extra):
250251
"""Fill in the field."""
252+
from django.core.files.base import File
251253
filename, content = self._make_content(extra)
252-
return django_files.File(content.file, filename)
254+
return File(content.file, filename)
253255

254256

255257
class ImageField(FileField):

0 commit comments

Comments
 (0)