diff options
| author | Federico Caselli <cfederico87@gmail.com> | 2020-01-23 17:51:38 -0500 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2020-01-25 18:03:48 -0500 |
| commit | 1de64504d8e68e2c0d14669c7638cf6f6d74973f (patch) | |
| tree | bcb2f19fe89efc629408c4576c355f3fe998578b /test/sql/test_compiler.py | |
| parent | 411637fbcf679f36448f1b094afef375158df15e (diff) | |
| download | sqlalchemy-1de64504d8e68e2c0d14669c7638cf6f6d74973f.tar.gz | |
Deprecate empty or_() and and_()
Creating an :func:`.and_` or :func:`.or_` construct with no arguments or
empty ``*args`` will now emit a deprecation warning, as the SQL produced is
a no-op (i.e. it renders as a blank string). This behavior is considered to
be non-intuitive, so for empty or possibly empty :func:`.and_` or
:func:`.or_` constructs, an appropriate default boolean should be included,
such as ``and_(True, *args)`` or ``or_(False, *args)``. As has been the
case for many major versions of SQLAlchemy, these particular boolean
values will not render if the ``*args`` portion is non-empty.
As there are some internal cases where an empty and_() construct is used
in order to build an optional WHERE expression, a private
utility function is added to suit this use case.
Co-authored-by: Mike Bayer <mike_mp@zzzcomputing.com>
Fixes: #5054
Closes: #5062
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/5062
Pull-request-sha: 5ca2f27281977d74e390148c0fb8deaa0e0e4ad9
Change-Id: I599b9c8befa64d9a59a35ad7dd84ff400e3aa647
Diffstat (limited to 'test/sql/test_compiler.py')
| -rw-r--r-- | test/sql/test_compiler.py | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/test/sql/test_compiler.py b/test/sql/test_compiler.py index b49fb455c..b53acf61e 100644 --- a/test/sql/test_compiler.py +++ b/test/sql/test_compiler.py @@ -70,7 +70,9 @@ from sqlalchemy.ext.compiler import compiles from sqlalchemy.sql import column from sqlalchemy.sql import compiler from sqlalchemy.sql import label +from sqlalchemy.sql import operators from sqlalchemy.sql import table +from sqlalchemy.sql.elements import BooleanClauseList from sqlalchemy.sql.expression import ClauseList from sqlalchemy.sql.expression import HasPrefixes from sqlalchemy.testing import assert_raises @@ -1393,10 +1395,20 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL): ) self.assert_compile( select([t]).where( + and_(or_(or_(t.c.x == 12), and_(or_(and_(t.c.x == 8))))) + ), + "SELECT t.x FROM t WHERE t.x = :x_1 OR t.x = :x_2", + ) + self.assert_compile( + select([t]).where( and_( or_( or_(t.c.x == 12), - and_(or_(), or_(and_(t.c.x == 8)), and_()), + and_( + BooleanClauseList._construct_raw(operators.or_), + or_(and_(t.c.x == 8)), + BooleanClauseList._construct_raw(operators.and_), + ), ) ) ), @@ -1451,11 +1463,15 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL): def test_where_empty(self): self.assert_compile( - select([table1.c.myid]).where(and_()), + select([table1.c.myid]).where( + BooleanClauseList._construct_raw(operators.and_) + ), "SELECT mytable.myid FROM mytable", ) self.assert_compile( - select([table1.c.myid]).where(or_()), + select([table1.c.myid]).where( + BooleanClauseList._construct_raw(operators.or_) + ), "SELECT mytable.myid FROM mytable", ) |
