Skip to content

Commit

Permalink
Move RowProxy and ResultProxy imports into type checking. Fixes #46
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Mar 22, 2021
1 parent bb56974 commit 9d21b6a
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions mautrix/util/db/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from typing import Iterator, Optional, TypeVar, Type, Dict, List, Any, cast
from typing import Iterator, Optional, TypeVar, Type, Dict, List, Any, cast, TYPE_CHECKING
from contextlib import contextmanager

from sqlalchemy import Table, Constraint
from sqlalchemy.engine.base import Engine, Connection
from sqlalchemy.engine.result import RowProxy, ResultProxy
from sqlalchemy.sql.base import ImmutableColumnCollection
from sqlalchemy.sql.expression import Select, ClauseElement, and_
from sqlalchemy.ext.declarative import as_declarative, declarative_base
from sqlalchemy.dialects.postgresql import insert as pg_insert

if TYPE_CHECKING:
from sqlalchemy.engine.result import RowProxy, ResultProxy

T = TypeVar('T', bound='BaseClass')


Expand Down Expand Up @@ -45,7 +47,7 @@ def copy(cls, bind: Optional[Engine] = None, rebase: Optional[declarative_base]
return copy

@classmethod
def _one_or_none(cls: Type[T], rows: ResultProxy) -> Optional[T]:
def _one_or_none(cls: Type[T], rows: 'ResultProxy') -> Optional[T]:
"""
Try scanning one row from a ResultProxy and return ``None`` if it fails.
Expand All @@ -61,7 +63,7 @@ def _one_or_none(cls: Type[T], rows: ResultProxy) -> Optional[T]:
return None

@classmethod
def _all(cls: Type[T], rows: ResultProxy) -> Iterator[T]:
def _all(cls: Type[T], rows: 'ResultProxy') -> Iterator[T]:
"""
Scan all rows from a ResultProxy.
Expand All @@ -75,7 +77,7 @@ def _all(cls: Type[T], rows: ResultProxy) -> Iterator[T]:
yield cls.scan(row)

@classmethod
def scan(cls: Type[T], row: RowProxy) -> T:
def scan(cls: Type[T], row: 'RowProxy') -> T:
"""
Read the data from a row into an object.
Expand Down

0 comments on commit 9d21b6a

Please sign in to comment.