:version: 1.0.12
:released:
+ .. change::
+ :tags: bug, sql
+ :tickets: 3642
+
+ Fixed issue where inadvertent use of the Python ``__contains__``
+ override with a column expression (e.g. by using ``'x' in col``)
+ would cause an endless loop in the case of an ARRAY type, as Python
+ defers this to ``__getitem__`` access which never raises for this
+ type. Overall, all use of ``__contains__`` now raises
+ NotImplementedError.
+
.. change::
:tags: bug, engine, mysql
:tickets: 2696
"getitem": (_getitem_impl,),
"lshift": (_unsupported_impl,),
"rshift": (_unsupported_impl,),
+ "contains": (_unsupported_impl,),
}
from operator import (
and_, or_, inv, add, mul, sub, mod, truediv, lt, le, ne, gt, ge, eq, neg,
- getitem, lshift, rshift
+ getitem, lshift, rshift, contains
)
if util.py2k:
"""
return self.operate(neg)
+ def __contains__(self, other):
+ return self.operate(contains, other)
+
def __getitem__(self, index):
"""Implement the [] operator.
checkparams={'param_1': 4, 'param_3': 6, 'param_2': 5}
)
+ def test_contains_override_raises(self):
+ col = column('x', postgresql.ARRAY(Integer))
+
+ assert_raises_message(
+ NotImplementedError,
+ "Operator 'contains' is not supported on this expression",
+ lambda: 'foo' in col
+ )
+
def test_array_contained_by(self):
col = column('x', postgresql.ARRAY(Integer))
self.assert_compile(
from sqlalchemy.schema import Column, Table, MetaData
from sqlalchemy.sql import compiler
from sqlalchemy.types import TypeEngine, TypeDecorator, UserDefinedType, \
- Boolean, NullType, MatchType, Indexable, Concatenable, ARRAY, JSON
+ Boolean, NullType, MatchType, Indexable, Concatenable, ARRAY, JSON, \
+ DateTime
from sqlalchemy.dialects import mysql, firebird, postgresql, oracle, \
sqlite, mssql
from sqlalchemy import util
expr.operator, operator.add
)
+ def test_contains_override_raises(self):
+ for col in [
+ Column('x', String),
+ Column('x', Integer),
+ Column('x', DateTime)
+ ]:
+ assert_raises_message(
+ NotImplementedError,
+ "Operator 'contains' is not supported on this expression",
+ lambda: 'foo' in col
+ )
+
class CustomUnaryOperatorTest(fixtures.TestBase, testing.AssertsCompiledSQL):
__dialect__ = 'default'
checkparams={'x_1': 5}
)
+ def test_contains_override_raises(self):
+ col = Column('x', self.MyType())
+
+ assert_raises_message(
+ NotImplementedError,
+ "Operator 'contains' is not supported on this expression",
+ lambda: 'foo' in col
+ )
+
def test_getindex_sqlexpr(self):
col = Column('x', self.MyType())