Skip to content

Defer sqlalchemy import #1070

@adamchainz

Description

@adamchainz

The problem

factory_boy/__init__.py uses this pattern for its optional sub-packages:

try:
    from . import alchemy
except ImportError:
    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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions