Skip to content

Commit

Permalink
[MAINTENANCE] SQLAlchemy 2 typing (great-expectations#10112)
Browse files Browse the repository at this point in the history
Co-authored-by: Bill Dirks <bill@greatexpectations.io>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Anthony Burdi <anthony@greatexpectations.io>
Co-authored-by: Anthony Burdi <anthonyburdi@users.noreply.github.com>
Co-authored-by: Nathan Farmer <NathanFarmer@users.noreply.github.com>
  • Loading branch information
6 people authored Sep 5, 2024
1 parent b6042b6 commit d67eafc
Show file tree
Hide file tree
Showing 53 changed files with 309 additions and 288 deletions.
8 changes: 4 additions & 4 deletions great_expectations/compatibility/aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@
redshiftdialect = REDSHIFT_NOT_IMPORTED

try:
import pyathena
import pyathena # type: ignore[import-not-found]
except ImportError:
pyathena = ATHENA_NOT_IMPORTED # type: ignore[assignment]
pyathena = ATHENA_NOT_IMPORTED

try:
from pyathena import sqlalchemy_athena
except (ImportError, AttributeError):
sqlalchemy_athena = ATHENA_NOT_IMPORTED # type: ignore[assignment]
sqlalchemy_athena = ATHENA_NOT_IMPORTED

try:
from pyathena.sqlalchemy_athena import types as athenatypes
from pyathena.sqlalchemy_athena import types as athenatypes # type: ignore[import-not-found]
except (ImportError, AttributeError):
athenatypes = ATHENA_NOT_IMPORTED
2 changes: 1 addition & 1 deletion great_expectations/compatibility/databricks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
)

try:
from databricks import connect # type: ignore[import-not-found]
from databricks import connect # type: ignore[import-untyped]
except ImportError:
connect = DATABRICKS_CONNECT_NOT_IMPORTED
4 changes: 2 additions & 2 deletions great_expectations/compatibility/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
# DeprecationWarning: pkg_resources is deprecated as an API
warnings.simplefilter(action="ignore", category=DeprecationWarning)
try:
from google.cloud import secretmanager
from google.cloud import secretmanager # type: ignore[attr-defined]
except (ImportError, AttributeError):
secretmanager = GOOGLE_CLOUD_STORAGE_NOT_IMPORTED # type: ignore[assignment]
secretmanager = GOOGLE_CLOUD_STORAGE_NOT_IMPORTED

try:
from google.api_core.exceptions import GoogleAPIError
Expand Down
96 changes: 48 additions & 48 deletions great_expectations/compatibility/sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,119 +10,119 @@
try:
import sqlalchemy
except ImportError:
sqlalchemy = SQLALCHEMY_NOT_IMPORTED
sqlalchemy = SQLALCHEMY_NOT_IMPORTED # type: ignore[assignment]

try:
from sqlalchemy.sql.selectable import Subquery
except (ImportError, AttributeError):
Subquery = SQLALCHEMY_NOT_IMPORTED
Subquery = SQLALCHEMY_NOT_IMPORTED # type: ignore[misc,assignment]

try:
from sqlalchemy import engine
except ImportError:
engine = SQLALCHEMY_NOT_IMPORTED
engine = SQLALCHEMY_NOT_IMPORTED # type: ignore[assignment]

try:
from sqlalchemy import dialects
except ImportError:
dialects = SQLALCHEMY_NOT_IMPORTED
dialects = SQLALCHEMY_NOT_IMPORTED # type: ignore[assignment]

try:
from sqlalchemy import inspect
except ImportError:
inspect = SQLALCHEMY_NOT_IMPORTED
inspect = SQLALCHEMY_NOT_IMPORTED # type: ignore[assignment]

try:
from sqlalchemy.dialects import sqlite
except (ImportError, AttributeError):
sqlite = SQLALCHEMY_NOT_IMPORTED
sqlite = SQLALCHEMY_NOT_IMPORTED # type: ignore[assignment]

try:
from sqlalchemy.dialects import registry
except (ImportError, AttributeError):
registry = SQLALCHEMY_NOT_IMPORTED
registry = SQLALCHEMY_NOT_IMPORTED # type: ignore[assignment]

try:
from sqlalchemy.engine import Dialect
except (ImportError, AttributeError):
Dialect = SQLALCHEMY_NOT_IMPORTED
Dialect = SQLALCHEMY_NOT_IMPORTED # type: ignore[misc,assignment]

try:
from sqlalchemy.engine import Inspector
except (ImportError, AttributeError):
Inspector = SQLALCHEMY_NOT_IMPORTED
Inspector = SQLALCHEMY_NOT_IMPORTED # type: ignore[misc,assignment]

try:
from sqlalchemy.engine import reflection
except (ImportError, AttributeError):
reflection = SQLALCHEMY_NOT_IMPORTED
reflection = SQLALCHEMY_NOT_IMPORTED # type: ignore[assignment]

try:
from sqlalchemy.engine import Connection
except (ImportError, AttributeError):
Connection = SQLALCHEMY_NOT_IMPORTED
Connection = SQLALCHEMY_NOT_IMPORTED # type: ignore[misc,assignment]

try:
from sqlalchemy.engine import Engine
except (ImportError, AttributeError):
Engine = SQLALCHEMY_NOT_IMPORTED
Engine = SQLALCHEMY_NOT_IMPORTED # type: ignore[misc,assignment]

try:
from sqlalchemy.engine import Row
except (ImportError, AttributeError):
Row = SQLALCHEMY_NOT_IMPORTED
Row = SQLALCHEMY_NOT_IMPORTED # type: ignore[misc,assignment]

try:
from sqlalchemy.engine.row import RowProxy
except (ImportError, AttributeError):
RowProxy = SQLALCHEMY_NOT_IMPORTED
RowProxy = SQLALCHEMY_NOT_IMPORTED # type: ignore[misc,assignment]

try:
from sqlalchemy.engine.row import LegacyRow
from sqlalchemy.engine.row import LegacyRow # type: ignore[attr-defined]
except (ImportError, AttributeError):
LegacyRow = SQLALCHEMY_NOT_IMPORTED

try:
from sqlalchemy.engine.default import DefaultDialect
except (ImportError, AttributeError):
DefaultDialect = SQLALCHEMY_NOT_IMPORTED
DefaultDialect = SQLALCHEMY_NOT_IMPORTED # type: ignore[misc,assignment]

try:
from sqlalchemy.engine import url
from sqlalchemy.engine.url import URL
except (ImportError, AttributeError):
url = SQLALCHEMY_NOT_IMPORTED
URL = SQLALCHEMY_NOT_IMPORTED
url = SQLALCHEMY_NOT_IMPORTED # type: ignore[assignment]
URL = SQLALCHEMY_NOT_IMPORTED # type: ignore[misc,assignment]

try:
from sqlalchemy.exc import DatabaseError
except (ImportError, AttributeError):
DatabaseError = SQLALCHEMY_NOT_IMPORTED
DatabaseError = SQLALCHEMY_NOT_IMPORTED # type: ignore[misc,assignment]

try:
from sqlalchemy.exc import IntegrityError
except (ImportError, AttributeError):
IntegrityError = SQLALCHEMY_NOT_IMPORTED
IntegrityError = SQLALCHEMY_NOT_IMPORTED # type: ignore[misc,assignment]

try:
from sqlalchemy.exc import NoSuchTableError
except (ImportError, AttributeError):
NoSuchTableError = SQLALCHEMY_NOT_IMPORTED
NoSuchTableError = SQLALCHEMY_NOT_IMPORTED # type: ignore[misc,assignment]

try:
from sqlalchemy.exc import OperationalError
except (ImportError, AttributeError):
OperationalError = SQLALCHEMY_NOT_IMPORTED
OperationalError = SQLALCHEMY_NOT_IMPORTED # type: ignore[misc,assignment]

try:
from sqlalchemy.exc import ProgrammingError
except (ImportError, AttributeError):
ProgrammingError = SQLALCHEMY_NOT_IMPORTED
ProgrammingError = SQLALCHEMY_NOT_IMPORTED # type: ignore[misc,assignment]

try:
from sqlalchemy.exc import SQLAlchemyError
except (ImportError, AttributeError):
SQLAlchemyError = SQLALCHEMY_NOT_IMPORTED
SQLAlchemyError = SQLALCHEMY_NOT_IMPORTED # type: ignore[misc,assignment]

try:
from sqlalchemy.orm import declarative_base
Expand All @@ -132,122 +132,122 @@
try:
from sqlalchemy.sql import functions
except (ImportError, AttributeError):
functions = SQLALCHEMY_NOT_IMPORTED
functions = SQLALCHEMY_NOT_IMPORTED # type: ignore[assignment]

try:
from sqlalchemy.sql import Insert
except (ImportError, AttributeError):
Insert = SQLALCHEMY_NOT_IMPORTED
Insert = SQLALCHEMY_NOT_IMPORTED # type: ignore[misc,assignment]

try:
from sqlalchemy.sql.elements import literal
except (ImportError, AttributeError):
literal = SQLALCHEMY_NOT_IMPORTED
literal = SQLALCHEMY_NOT_IMPORTED # type: ignore[assignment]

try:
from sqlalchemy.sql.elements import TextClause
except (ImportError, AttributeError):
TextClause = SQLALCHEMY_NOT_IMPORTED
TextClause = SQLALCHEMY_NOT_IMPORTED # type: ignore[misc,assignment]

try:
from sqlalchemy.sql.elements import quoted_name
except (ImportError, AttributeError):
quoted_name = SQLALCHEMY_NOT_IMPORTED
quoted_name = SQLALCHEMY_NOT_IMPORTED # type: ignore[misc,assignment]

try:
from sqlalchemy.sql.elements import _anonymous_label
except (ImportError, AttributeError):
_anonymous_label = SQLALCHEMY_NOT_IMPORTED
_anonymous_label = SQLALCHEMY_NOT_IMPORTED # type: ignore[misc,assignment]

try:
from sqlalchemy.sql.elements import ColumnElement
except (ImportError, AttributeError):
ColumnElement = SQLALCHEMY_NOT_IMPORTED
ColumnElement = SQLALCHEMY_NOT_IMPORTED # type: ignore[misc,assignment]

try:
from sqlalchemy.sql.expression import Cast
except (ImportError, AttributeError):
Cast = SQLALCHEMY_NOT_IMPORTED
Cast = SQLALCHEMY_NOT_IMPORTED # type: ignore[misc,assignment]

try:
from sqlalchemy.sql.expression import ColumnOperators
except (ImportError, AttributeError):
ColumnOperators = SQLALCHEMY_NOT_IMPORTED
ColumnOperators = SQLALCHEMY_NOT_IMPORTED # type: ignore[misc,assignment]

try:
from sqlalchemy.sql.expression import CTE
except (ImportError, AttributeError):
CTE = SQLALCHEMY_NOT_IMPORTED
CTE = SQLALCHEMY_NOT_IMPORTED # type: ignore[misc,assignment]

try:
from sqlalchemy.sql.expression import BinaryExpression
except (ImportError, AttributeError):
BinaryExpression = SQLALCHEMY_NOT_IMPORTED
BinaryExpression = SQLALCHEMY_NOT_IMPORTED # type: ignore[misc,assignment]

try:
from sqlalchemy.sql.expression import BooleanClauseList
except (ImportError, AttributeError):
BooleanClauseList = SQLALCHEMY_NOT_IMPORTED
BooleanClauseList = SQLALCHEMY_NOT_IMPORTED # type: ignore[misc,assignment]

try:
from sqlalchemy.sql.expression import ColumnClause
except (ImportError, AttributeError):
ColumnClause = SQLALCHEMY_NOT_IMPORTED
ColumnClause = SQLALCHEMY_NOT_IMPORTED # type: ignore[misc,assignment]

try:
from sqlalchemy.sql.expression import Label
except (ImportError, AttributeError):
Label = SQLALCHEMY_NOT_IMPORTED
Label = SQLALCHEMY_NOT_IMPORTED # type: ignore[misc,assignment]

try:
from sqlalchemy.sql.expression import Select
except (ImportError, AttributeError):
Select = SQLALCHEMY_NOT_IMPORTED
Select = SQLALCHEMY_NOT_IMPORTED # type: ignore[misc,assignment]

try:
from sqlalchemy.sql import Selectable
except (ImportError, AttributeError):
Selectable = SQLALCHEMY_NOT_IMPORTED
Selectable = SQLALCHEMY_NOT_IMPORTED # type: ignore[misc,assignment]

try:
from sqlalchemy.sql.expression import TableClause
except (ImportError, AttributeError):
TableClause = SQLALCHEMY_NOT_IMPORTED
TableClause = SQLALCHEMY_NOT_IMPORTED # type: ignore[misc,assignment]

try:
from sqlalchemy.sql.expression import TextualSelect
except (ImportError, AttributeError):
TextualSelect = SQLALCHEMY_NOT_IMPORTED
TextualSelect = SQLALCHEMY_NOT_IMPORTED # type: ignore[misc,assignment]

try:
from sqlalchemy.sql.expression import WithinGroup
except (ImportError, AttributeError):
WithinGroup = SQLALCHEMY_NOT_IMPORTED
WithinGroup = SQLALCHEMY_NOT_IMPORTED # type: ignore[misc,assignment]

try:
from sqlalchemy.sql.operators import custom_op
except (ImportError, AttributeError):
custom_op = SQLALCHEMY_NOT_IMPORTED
custom_op = SQLALCHEMY_NOT_IMPORTED # type: ignore[misc,assignment]

try:
from sqlalchemy.engine.cursor import LegacyCursorResult
from sqlalchemy.engine.cursor import LegacyCursorResult # type: ignore[attr-defined]
except (ImportError, AttributeError):
LegacyCursorResult = SQLALCHEMY_NOT_IMPORTED

try:
from sqlalchemy.engine.cursor import CursorResult
except (ImportError, AttributeError):
CursorResult = SQLALCHEMY_NOT_IMPORTED
CursorResult = SQLALCHEMY_NOT_IMPORTED # type: ignore[misc,assignment]

try:
from sqlalchemy.pool import StaticPool
except (ImportError, AttributeError):
StaticPool = SQLALCHEMY_NOT_IMPORTED
StaticPool = SQLALCHEMY_NOT_IMPORTED # type: ignore[misc,assignment]

try:
from sqlalchemy import Table
except (ImportError, AttributeError):
Table = SQLALCHEMY_NOT_IMPORTED
Table = SQLALCHEMY_NOT_IMPORTED # type: ignore[misc,assignment]

try:
__version__: str | None = sqlalchemy.__version__
Expand Down
4 changes: 2 additions & 2 deletions great_expectations/core/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
if not LegacyRow:
LegacyRow = SQLALCHEMY_NOT_IMPORTED

if not Row:
Row = SQLALCHEMY_NOT_IMPORTED
if not Row: # type: ignore[truthy-function]
Row = SQLALCHEMY_NOT_IMPORTED # type: ignore[misc]

SCHEMAS = {
"api_np": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@
from great_expectations.validator.validator import Validator

SQLAlchemyError = sqlalchemy.SQLAlchemyError
if not SQLAlchemyError:
if not SQLAlchemyError: # type: ignore[truthy-function]
# We'll redefine this error in code below to catch ProfilerError, which is caught above, so SA errors will # noqa: E501
# just fall through
SQLAlchemyError = gx_exceptions.ProfilerError
SQLAlchemyError = gx_exceptions.ProfilerError # type: ignore[misc]


if TYPE_CHECKING:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,9 @@ def _set(self, key, value, allow_update=True, **kwargs) -> None:
.values(**cols)
)
else:
ins = self._table.insert().values(**cols)
ins = self._table.insert().values(**cols) # type: ignore[assignment]
else:
ins = self._table.insert().values(**cols)
ins = self._table.insert().values(**cols) # type: ignore[assignment]

try:
with self.engine.begin() as connection:
Expand Down
2 changes: 1 addition & 1 deletion great_expectations/data_context/store/query_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
if is_version_greater_or_equal(sa.__version__, "1.4.0"):
url_create_fn = sqlalchemy.URL.create
else:
url_create_fn = sqlalchemy.URL
url_create_fn = sqlalchemy.URL # type: ignore[assignment]


logger = logging.getLogger(__name__)
Expand Down
2 changes: 1 addition & 1 deletion great_expectations/data_context/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
try:
import sqlalchemy as sa # noqa: TID251
except ImportError:
sa = None
sa = None # type: ignore[assignment]

logger = logging.getLogger(__name__)

Expand Down
2 changes: 1 addition & 1 deletion great_expectations/datasource/fluent/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
_MISSING: Final = object()

JSON_ENCODERS: dict[Type, Callable] = {}
if TextClause:
if TextClause: # type: ignore[truthy-function]
JSON_ENCODERS[TextClause] = lambda v: str(v)

T = TypeVar("T")
Expand Down
Loading

0 comments on commit d67eafc

Please sign in to comment.