summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/engine/base.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2021-11-26 10:17:38 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2021-11-29 13:46:23 -0500
commitdb85d28a857945ce021e27a187a14999eeb5c89e (patch)
tree3e402d745aa1d01a51198154ddda25fe96296056 /lib/sqlalchemy/engine/base.py
parent5eb407f84bdabdbcd68975dbf76dc4c0809d7373 (diff)
downloadsqlalchemy-db85d28a857945ce021e27a187a14999eeb5c89e.tar.gz
provide connectionfairy on initialize
This is so that dialect methods that are called within init can assume the same argument structure as when they are called in other places; we can nail down the type of object as well. This change seems to mostly impact the isolation level routines in the dialects, as these are called during initialize() as well as on established connections. these methods can now assume a non-proxied DBAPI connection object in all cases, as it is commonly required that attributes like ".autocommit" are set on the object which don't work well in a proxied situation. Other changes: * adds an interface for the "connectionfairy" concept called PoolProxiedConnection. * Removes ``Connectable`` superclass of Connection. ``Connectable`` was originally meant to provide for the "method which accepts connection or engine" theme. As this pattern is greatly reduced in 2.0 and Engine no longer extends from it, the ``Connectable`` superclass doesnt serve any real purpose. Leading from that, to set this in I also applied pep 484 annotations to the Dialect base, and then in the interests of seeing some of the typing information show up in my IDE did a little bit for Engine, Connection and others. I hope that it's feasible that we can add annotations to specific classes and attributes ahead of when we actually try to mass-populate the whole library. This was the original spirit of pep-484 that we can apply annotations gradually. I do of course want to try to do a mass-populate although i think even in that case we will end up doing a lot of manual work anyway (in particular for the changes here which are distinct from what the stubs have). Fixes: #7122 Change-Id: I5dd7fbff8a7ae520a81c165091af12a6a68826db
Diffstat (limited to 'lib/sqlalchemy/engine/base.py')
-rw-r--r--lib/sqlalchemy/engine/base.py35
1 files changed, 23 insertions, 12 deletions
diff --git a/lib/sqlalchemy/engine/base.py b/lib/sqlalchemy/engine/base.py
index bfadcbce6..fbd8fe7df 100644
--- a/lib/sqlalchemy/engine/base.py
+++ b/lib/sqlalchemy/engine/base.py
@@ -6,9 +6,13 @@
# the MIT License: https://www.opensource.org/licenses/mit-license.php
import contextlib
import sys
+import typing
+from typing import Any
+from typing import Mapping
+from typing import Optional
+from typing import Union
from .interfaces import BindTyping
-from .interfaces import Connectable
from .interfaces import ConnectionEventsTarget
from .interfaces import ExceptionContext
from .util import _distill_params_20
@@ -20,6 +24,11 @@ from .. import util
from ..sql import compiler
from ..sql import util as sql_util
+if typing.TYPE_CHECKING:
+ from .interfaces import Dialect
+ from .url import URL
+ from ..pool import Pool
+ from ..pool import PoolProxiedConnection
"""Defines :class:`_engine.Connection` and :class:`_engine.Engine`.
@@ -29,7 +38,7 @@ _EMPTY_EXECUTION_OPTS = util.immutabledict()
NO_OPTIONS = util.immutabledict()
-class Connection(Connectable):
+class Connection(ConnectionEventsTarget):
"""Provides high-level functionality for a wrapped DB-API connection.
The :class:`_engine.Connection` object is procured by calling
@@ -364,7 +373,7 @@ class Connection(Connectable):
return self._dbapi_connection is None and not self.closed
@property
- def connection(self):
+ def connection(self) -> "PoolProxiedConnection":
"""The underlying DB-API connection managed by this Connection.
This is a SQLAlchemy connection-pool proxied connection
@@ -422,7 +431,9 @@ class Connection(Connectable):
"""
try:
- return self.dialect.get_isolation_level(self.connection)
+ return self.dialect.get_isolation_level(
+ self.connection.dbapi_connection
+ )
except BaseException as e:
self._handle_dbapi_exception(e, None, None, None, None)
@@ -2296,14 +2307,14 @@ class Engine(ConnectionEventsTarget, log.Identified):
def __init__(
self,
- pool,
- dialect,
- url,
- logging_name=None,
- echo=None,
- query_cache_size=500,
- execution_options=None,
- hide_parameters=False,
+ pool: "Pool",
+ dialect: "Dialect",
+ url: "URL",
+ logging_name: Optional[str] = None,
+ echo: Union[None, str, bool] = None,
+ query_cache_size: int = 500,
+ execution_options: Optional[Mapping[str, Any]] = None,
+ hide_parameters: bool = False,
):
self.pool = pool
self.url = url