From 3eda9e6f94f992059e5de9c661ce3e4bc554c111 Mon Sep 17 00:00:00 2001 From: Matt F Date: Tue, 30 Jul 2024 14:53:43 -0400 Subject: [PATCH] Optimizes sqlalchemy_utils imports Importing functions from slqalchemy_utils directly causes the entire library to get imported, even though many of the library's modules go unused. Using more-focused imports should improve initialization time in performance-sensitive situations. This commit also removes a few unused imports. Closes #357 --- sqlalchemy_continuum/fetcher.py | 2 +- sqlalchemy_continuum/manager.py | 4 +--- sqlalchemy_continuum/operation.py | 2 +- sqlalchemy_continuum/plugins/activity.py | 5 +++-- sqlalchemy_continuum/plugins/flask.py | 2 +- sqlalchemy_continuum/table_builder.py | 1 - sqlalchemy_continuum/unit_of_work.py | 2 +- 7 files changed, 8 insertions(+), 10 deletions(-) diff --git a/sqlalchemy_continuum/fetcher.py b/sqlalchemy_continuum/fetcher.py index 689e3b54..76e3b953 100644 --- a/sqlalchemy_continuum/fetcher.py +++ b/sqlalchemy_continuum/fetcher.py @@ -1,6 +1,6 @@ import operator import sqlalchemy as sa -from sqlalchemy_utils import get_primary_keys, identity +from sqlalchemy_utils.functions.orm import get_primary_keys, identity from .utils import tx_column_name, end_tx_column_name diff --git a/sqlalchemy_continuum/manager.py b/sqlalchemy_continuum/manager.py index e115a27a..3966cfb5 100644 --- a/sqlalchemy_continuum/manager.py +++ b/sqlalchemy_continuum/manager.py @@ -1,9 +1,8 @@ -import re from functools import wraps import sqlalchemy as sa from sqlalchemy.orm import object_session -from sqlalchemy_utils import get_column_key +from sqlalchemy_utils.functions import get_column_key from .builder import Builder from .fetcher import SubqueryFetcher, ValidityFetcher @@ -454,4 +453,3 @@ def track_association_operations( 'operation_type': op, }) uow.pending_statements.append(stmt) - diff --git a/sqlalchemy_continuum/operation.py b/sqlalchemy_continuum/operation.py index 315f25c8..a817ad6c 100644 --- a/sqlalchemy_continuum/operation.py +++ b/sqlalchemy_continuum/operation.py @@ -2,7 +2,7 @@ from collections import OrderedDict import sqlalchemy as sa -from sqlalchemy_utils import identity +from sqlalchemy_utils.functions.orm import identity class Operation(object): diff --git a/sqlalchemy_continuum/plugins/activity.py b/sqlalchemy_continuum/plugins/activity.py index 10b85d3f..f5aab09b 100644 --- a/sqlalchemy_continuum/plugins/activity.py +++ b/sqlalchemy_continuum/plugins/activity.py @@ -192,7 +192,8 @@ import sqlalchemy as sa from sqlalchemy.ext.hybrid import hybrid_property from sqlalchemy.inspection import inspect -from sqlalchemy_utils import JSONType, generic_relationship +from sqlalchemy_utils.generic import generic_relationship +from sqlalchemy_utils.types.json import JSONType from .base import Plugin from ..factory import ModelFactory @@ -318,7 +319,7 @@ def target_version_type(cls): class ActivityPlugin(Plugin): activity_cls = None - + def after_build_models(self, manager): self.activity_cls = ActivityFactory()(manager) manager.activity_cls = self.activity_cls diff --git a/sqlalchemy_continuum/plugins/flask.py b/sqlalchemy_continuum/plugins/flask.py index f135f8bf..b8acfa46 100644 --- a/sqlalchemy_continuum/plugins/flask.py +++ b/sqlalchemy_continuum/plugins/flask.py @@ -24,7 +24,7 @@ from flask import current_app, has_app_context, has_request_context, request except ImportError: pass -from sqlalchemy_utils import ImproperlyConfigured +from sqlalchemy_utils.exceptions import ImproperlyConfigured from .base import Plugin diff --git a/sqlalchemy_continuum/table_builder.py b/sqlalchemy_continuum/table_builder.py index 9666b2de..423aba14 100644 --- a/sqlalchemy_continuum/table_builder.py +++ b/sqlalchemy_continuum/table_builder.py @@ -1,5 +1,4 @@ import sqlalchemy as sa -from sqlalchemy_utils import get_column_key class ColumnReflector(object): diff --git a/sqlalchemy_continuum/unit_of_work.py b/sqlalchemy_continuum/unit_of_work.py index cc5839ce..0c270a33 100644 --- a/sqlalchemy_continuum/unit_of_work.py +++ b/sqlalchemy_continuum/unit_of_work.py @@ -1,7 +1,7 @@ from copy import copy import sqlalchemy as sa -from sqlalchemy_utils import get_primary_keys, identity +from sqlalchemy_utils.functions.orm import get_primary_keys, identity from .operation import Operations from .utils import ( end_tx_column_name,