]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
refactor: simplify and clean up dialect-specific code
authorShamil <ashm.tech@proton.me>
Thu, 17 Apr 2025 15:23:21 +0000 (11:23 -0400)
committerFederico Caselli <cfederico87@gmail.com>
Thu, 24 Apr 2025 19:49:13 +0000 (21:49 +0200)
**Title:** Removed unused variables and redundant functions across multiple dialects. Improves code readability and reduces maintenance complexity without altering functionality.
### Description
This pull request introduces several minor refactorings across different dialect modules:
- **MSSQL:**
    - Simplified the initialization of the `fkeys` dictionary in `_get_foreign_keys` using `util.defaultdict` directly.

- **MySQL:** Removed the unused variable in `_get_table_comment`. `rp`
- **PostgreSQL (_psycopg_common):** Removed the unused variable `cursor` in `do_ping`.
- **PostgreSQL (base):** Removed the unused variable `args` in `_get_column_info`.
- **SQLite:** Removed the unused variable `new_filename` in `generate_driver_url`.

These changes focus purely on code cleanup and simplification, removing dead code and improving clarity. They do not alter the existing logic or functionality of the dialects.
### Checklist
This pull request is:
- [ ] A documentation / typographical / small typing error fix
- [x] A short code fix
    - _Note: This is a general cleanup refactor rather than a fix for a specific reported issue._

- [ ] A new feature implementation

**Have a nice day!**

Closes: #12534
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/12534
Pull-request-sha: 2c7ae17b73192ba6bff6bec953b307a88ea31847

Change-Id: I1ec3b48f42aea7e45bc20f81add03051eb30bb98
(cherry picked from commit bb5bfb4beb35450ee8db7a173b9b438e065a90a9)

lib/sqlalchemy/dialects/mssql/base.py
lib/sqlalchemy/dialects/mysql/base.py
lib/sqlalchemy/dialects/postgresql/_psycopg_common.py
lib/sqlalchemy/dialects/postgresql/base.py
lib/sqlalchemy/dialects/sqlite/provision.py

index 916809e768475e1cb1ad61c1460e61de1b6902ee..f641ff03ea89c73779c48a9910205c250f019e75 100644 (file)
@@ -3991,10 +3991,8 @@ index_info AS (
         )
 
         # group rows by constraint ID, to handle multi-column FKs
-        fkeys = []
-
-        def fkey_rec():
-            return {
+        fkeys = util.defaultdict(
+            lambda: {
                 "name": None,
                 "constrained_columns": [],
                 "referred_schema": None,
@@ -4002,8 +4000,7 @@ index_info AS (
                 "referred_columns": [],
                 "options": {},
             }
-
-        fkeys = util.defaultdict(fkey_rec)
+        )
 
         for r in connection.execute(s).all():
             (
index 50976310a75b14651060c97f2cc52f440c37fbf2..a78c4e0f747f9d440847a44f4c5a17e4fa87d56d 100644 (file)
@@ -3478,7 +3478,6 @@ class MySQLDialect(default.DefaultDialect):
             full_name = self.identifier_preparer.format_table(table)
         st = "SHOW CREATE TABLE %s" % full_name
 
-        rp = None
         try:
             rp = connection.execution_options(
                 skip_user_error_events=True
index d827e054ccf414cb50c73df6f66308845f645dbe..9b09868bd3af69eb212c9be16f7f09647a1d9e6c 100644 (file)
@@ -171,7 +171,6 @@ class _PGDialect_common_psycopg(PGDialect):
         connection.autocommit = value
 
     def do_ping(self, dbapi_connection):
-        cursor = None
         before_autocommit = dbapi_connection.autocommit
 
         if not before_autocommit:
index f8c1d5c3bb1eab1d803adc46c208f8d04d2d7994..9d4257cf0e480e82580bbb042badeac4c8c7bef7 100644 (file)
@@ -3935,7 +3935,6 @@ class PGDialect(default.DefaultDialect):
                 schema_type = ENUM
                 enum = enums[enum_or_domain_key]
 
-                args = tuple(enum["labels"])
                 kwargs["name"] = enum["name"]
 
                 if not enum["visible"]:
index 97f882e7f2807c25f51f05d2f085e168484814ef..e1df005e72ccd51f760be51f40dc6bc05bd037ff 100644 (file)
@@ -52,8 +52,6 @@ def _format_url(url, driver, ident):
         assert "test_schema" not in filename
         tokens = re.split(r"[_\.]", filename)
 
-        new_filename = f"{driver}"
-
         for token in tokens:
             if token in _drivernames:
                 if driver is None: