From: Shamil Date: Mon, 21 Apr 2025 16:36:21 +0000 (-0400) Subject: refactor (sql): simplify and optimize internal SQL handling X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=93b0be7009b4f6efd091fda31229353f929f4cc9;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git refactor (sql): simplify and optimize internal SQL handling Replaced redundant variable assignments with direct operations. Used `dict.get()` for safer dictionary lookups to streamline logic. Improves code readability and reduces unnecessary lines. Closes: #12538 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12538 Pull-request-sha: d322d1508cfc37668099e6624816aba9c647ad51 Change-Id: Ib3dfc7086ec35117fdad65e136a17aa014b96ae5 --- diff --git a/lib/sqlalchemy/sql/cache_key.py b/lib/sqlalchemy/sql/cache_key.py index 5ac11878ba..c8fa205691 100644 --- a/lib/sqlalchemy/sql/cache_key.py +++ b/lib/sqlalchemy/sql/cache_key.py @@ -516,7 +516,7 @@ class CacheKey(NamedTuple): e2, ) else: - pickup_index = stack.pop(-1) + stack.pop(-1) break def _diff(self, other: CacheKey) -> str: diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index cdcf9f5c72..b123acbff1 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -4266,7 +4266,7 @@ class SQLCompiler(Compiled): inner = "(%s)" % (inner,) return inner else: - enclosing_alias = kwargs["enclosing_alias"] = alias + kwargs["enclosing_alias"] = alias if asfrom or ashint: if isinstance(alias.name, elements._truncated_label): diff --git a/lib/sqlalchemy/sql/crud.py b/lib/sqlalchemy/sql/crud.py index ca7448b58b..265b15c1e9 100644 --- a/lib/sqlalchemy/sql/crud.py +++ b/lib/sqlalchemy/sql/crud.py @@ -236,7 +236,7 @@ def _get_crud_params( stmt_parameter_tuples = list(spd.items()) spd_str_key = {_column_as_key(key) for key in spd} else: - stmt_parameter_tuples = spd = spd_str_key = None + stmt_parameter_tuples = spd_str_key = None # if we have statement parameters - set defaults in the # compiled params diff --git a/lib/sqlalchemy/sql/lambdas.py b/lib/sqlalchemy/sql/lambdas.py index 8d70f800e7..ce755c1f83 100644 --- a/lib/sqlalchemy/sql/lambdas.py +++ b/lib/sqlalchemy/sql/lambdas.py @@ -256,10 +256,7 @@ class LambdaElement(elements.ClauseElement): self.closure_cache_key = cache_key - try: - rec = lambda_cache[tracker_key + cache_key] - except KeyError: - rec = None + rec = lambda_cache.get(tracker_key + cache_key) else: cache_key = _cache_key.NO_CACHE rec = None @@ -1173,7 +1170,7 @@ class AnalyzedFunction: closure_pywrappers.append(bind) else: value = fn.__globals__[name] - new_globals[name] = bind = PyWrapper(fn, name, value) + new_globals[name] = PyWrapper(fn, name, value) # rewrite the original fn. things that look like they will # become bound parameters are wrapped in a PyWrapper.