From eda6dbbf387def2063d1b6719b64b20f9e7f2ab4 Mon Sep 17 00:00:00 2001 From: Federico Caselli Date: Sat, 7 Mar 2020 19:17:07 +0100 Subject: Simplified module pre-loading strategy and made it linter friendly Introduced a modules registry to register modules that should be lazily loaded in the package init. This ensures that they are in the system module cache, avoiding potential thread safety issues as when importing them directly in the function that uses them. The module registry is used to obtain these modules directly, ensuring that the all the lazily loaded modules are resolved at the proper time This replaces dependency_for decorator and the dependencies decorator logic, removing the need to pass the resolved modules as arguments of the decodated functions and removes possible errors caused by linters. Fixes: #4689 Fixes: #4656 Change-Id: I2e291eba4297867fc0ddb5d875b9f7af34751d01 --- lib/sqlalchemy/sql/schema.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'lib/sqlalchemy/sql/schema.py') diff --git a/lib/sqlalchemy/sql/schema.py b/lib/sqlalchemy/sql/schema.py index 4c627c4cc..69f60ba24 100644 --- a/lib/sqlalchemy/sql/schema.py +++ b/lib/sqlalchemy/sql/schema.py @@ -2193,8 +2193,10 @@ class ColumnDefault(DefaultGenerator): ) @util.memoized_property - @util.dependencies("sqlalchemy.sql.sqltypes") - def _arg_is_typed(self, sqltypes): + @util.preload_module("sqlalchemy.sql.sqltypes") + def _arg_is_typed(self): + sqltypes = util.preloaded.sql_sqltypes + if self.is_clause_element: return not isinstance(self.arg.type, sqltypes.NullType) else: @@ -2440,14 +2442,16 @@ class Sequence(roles.StatementRole, DefaultGenerator): def is_clause_element(self): return False - @util.dependencies("sqlalchemy.sql.functions.func") - def next_value(self, func): + @util.preload_module("sqlalchemy.sql.functions") + def next_value(self): """Return a :class:`.next_value` function element which will render the appropriate increment function for this :class:`.Sequence` within any SQL expression. """ - return func.next_value(self, bind=self.bind) + return util.preloaded.sql_functions.func.next_value( + self, bind=self.bind + ) def _set_parent(self, column): super(Sequence, self)._set_parent(column) @@ -3925,10 +3929,10 @@ class MetaData(SchemaItem): """ return self._bind - @util.dependencies("sqlalchemy.engine.url") - def _bind_to(self, url, bind): + @util.preload_module("sqlalchemy.engine.url") + def _bind_to(self, bind): """Bind this MetaData to an Engine, Connection, string or URL.""" - + url = util.preloaded.engine_url if isinstance(bind, util.string_types + (url.URL,)): self._bind = sqlalchemy.create_engine(bind) else: @@ -4231,10 +4235,10 @@ class ThreadLocalMetaData(MetaData): return getattr(self.context, "_engine", None) - @util.dependencies("sqlalchemy.engine.url") - def _bind_to(self, url, bind): + @util.preload_module("sqlalchemy.engine.url") + def _bind_to(self, bind): """Bind to a Connectable in the caller's thread.""" - + url = util.preloaded.engine_url if isinstance(bind, util.string_types + (url.URL,)): try: self.context._engine = self.__engines[bind] -- cgit v1.2.1