You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
factory_boy/__init__.py uses this pattern for its optional sub-packages:
try:
from . importalchemyexceptImportError:
pass
I have a Django project that uses factory_boy a lot, and imports it even in view code. It also happens that the project has sqlalchemy installed for another tool, but it doesn’t use it directly.
On a importtime-waterfall profile, I noticed that importing factory_boy.alchemy takes ~300ms, nearly all in sqlalchemy and its submodules.
It would be great if importing factory_boy didn’t automatically import sqlalchemy.
Proposed solution
Defer the few sqlalchemy imports in factory.alchemy to the functions that use them. Drop the except ImportError pattern in factory_boy since it should then work regardless of whether sqlalchemy is installed.
Extra notes
None.
The text was updated successfully, but these errors were encountered:
adamchainz
changed the title
Defer imports of optional submodules
Defer sqlalchemy import
Mar 29, 2024
I do agree that import factory should not have imported factory.django or factory.alchemy dynamically.
Unfortunately, I'm afraid this ship has sailed :/
Any change on that topic would have to be non-breaking to the thousands of users of the existing, documented import style.
I do not wish to provoke large amount of code churn for users of the library unless that brings some improvements to them.
Before working on pull requests, I'd like to have a high-level agreement on an approach that wouldn't break the code for lots of people.
The problem
factory_boy/__init__.py
uses this pattern for its optional sub-packages:I have a Django project that uses
factory_boy
a lot, and imports it even in view code. It also happens that the project hassqlalchemy
installed for another tool, but it doesn’t use it directly.On a importtime-waterfall profile, I noticed that importing
factory_boy.alchemy
takes ~300ms, nearly all insqlalchemy
and its submodules.It would be great if importing
factory_boy
didn’t automatically importsqlalchemy
.Proposed solution
Defer the few
sqlalchemy
imports infactory.alchemy
to the functions that use them. Drop theexcept ImportError
pattern infactory_boy
since it should then work regardless of whethersqlalchemy
is installed.Extra notes
None.
The text was updated successfully, but these errors were encountered: