Skip to content

case() with single-item dict breaks #6097

Closed

Description

@ThiefMaster

Describe the bug
case() with a single-item dict fails; this used to work in 1.3

To Reproduce

from sqlalchemy import *
from sqlalchemy.orm import *


Base = declarative_base()


class Test(Base):
    __tablename__ = 'test'
    id = Column(Integer, primary_key=True)
    foo = Column(String)


print(case({'a': 'aa', 'b': 'bb'}, else_='c', value=Test.foo))
print()
print(case({'a': 'aa'}, else_='c', value=Test.foo))

Error

$ python /tmp/satest.py
CASE test.foo WHEN :param_1 THEN :param_2 ELSE :param_3 END

Traceback (most recent call last):
  File "/tmp/satest.py", line 16, in <module>
    print(case({'a': 'aa'}, else_='c', value=Test.foo))
  File "<string>", line 2, in case
  File "/home/adrian/dev/indico/py3/env/lib/python3.9/site-packages/sqlalchemy/sql/elements.py", line 2755, in __init__
    whenlist = [
  File "/home/adrian/dev/indico/py3/env/lib/python3.9/site-packages/sqlalchemy/sql/elements.py", line 2764, in <listcomp>
    for (c, r) in whens
ValueError: not enough values to unpack (expected 2, got 1)

Versions.

  • OS: Linux
  • Python: 3.9.1
  • SQLAlchemy: 1.4.1
  • Database: n/a
  • DBAPI: n/a

Activity

  1. added
    requires triage
    on Mar 19, 2021
  2. added
    bug
    regression
    on Mar 19, 2021
  3. added this to the 1.4.x milestone on Mar 19, 2021
  4. removed
    requires triage
    on Mar 19, 2021
  5. zzzeek commented on Mar 19, 2021

    @zzzeek
    Member

    workaround, pass whens w/ the param name:

    case(whens={'a': 'aa', 'b': 'bb'}, else_='c', value=Test.foo)
    
  6. sqla-tester commented on Mar 19, 2021

    @sqla-tester
    Collaborator

    Mike Bayer has proposed a fix for this issue in the master branch:

    Correct for coercion from list args to positional for case https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/2656

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugregressionsql

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions