Skip to content

Commit

Permalink
fix scalar_subquery warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
kvesteri committed Apr 9, 2021
1 parent 8587bcb commit e298168
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 4 deletions.
3 changes: 2 additions & 1 deletion sqlalchemy_utils/aggregates.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ class Rating(Base):
from sqlalchemy.ext.declarative import declared_attr
from sqlalchemy.sql.functions import _FunctionGenerator

from .compat import get_scalar_subquery
from .functions.orm import get_column_key
from .relationships import (
chained_join,
Expand Down Expand Up @@ -452,7 +453,7 @@ def aggregate_query(self):
self.relationships[0].mapper.class_
)

return query.as_scalar()
return get_scalar_subquery(query)

def update_query(self, objects):
table = self.class_.__table__
Expand Down
5 changes: 5 additions & 0 deletions sqlalchemy_utils/compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def get_scalar_subquery(query):
try:
return query.scalar_subquery()
except:
return query.as_scalar()
3 changes: 2 additions & 1 deletion tests/functions/test_cast_if.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from sqlalchemy.ext.declarative import declarative_base

from sqlalchemy_utils import cast_if
from sqlalchemy_utils.compat import get_scalar_subquery


@pytest.fixture(scope='class')
Expand Down Expand Up @@ -39,7 +40,7 @@ def test_synonym(self, article_cls):
assert cast_if(expr, sa.String) is expr

def test_scalar_selectable(self, article_cls):
expr = sa.select([article_cls.id]).as_scalar()
expr = get_scalar_subquery(sa.select([article_cls.id]))
assert cast_if(expr, sa.Integer) is expr

def test_scalar(self):
Expand Down
3 changes: 2 additions & 1 deletion tests/functions/test_get_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sqlalchemy as sa

from sqlalchemy_utils import get_type
from sqlalchemy_utils.compat import get_scalar_subquery


@pytest.fixture
Expand Down Expand Up @@ -45,5 +46,5 @@ def test_relationship_property(self, Article, User):
assert get_type(Article.author) == User

def test_scalar_select(self, Article):
query = sa.select([Article.id]).as_scalar()
query = get_scalar_subquery(sa.select([Article.id]))
assert isinstance(get_type(query), sa.Integer)
3 changes: 2 additions & 1 deletion tests/types/test_int_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sqlalchemy as sa

from sqlalchemy_utils import IntRangeType
from sqlalchemy_utils.compat import get_scalar_subquery

intervals = None
inf = -1
Expand Down Expand Up @@ -283,7 +284,7 @@ def test_eq_with_query_arg(self, session, Building, create_building):
session.query(Building)
.filter(
Building.persons_at_night ==
session.query(Building.persons_at_night)
get_scalar_subquery(session.query(Building.persons_at_night))
).order_by(Building.persons_at_night).limit(1)
)
assert query.count()
Expand Down

0 comments on commit e298168

Please sign in to comment.