Bitten By SqlAlchemy 0.6 Truthiness

After upgrading to SqlAlchemy 0.6, I was bitten by this
known gotcha.

Given that st is some SqlAlchemy Table (as defined below):

import logging
from sqlalchemy import *
summary = Table(
st = summary_table.c
query = select(st).where('US')

Then we try to log using it

logging.critical('%r', query)

we get this uninformative error:

TypeError: Boolean value of this clause is not defined

What is going on here? It seems that as part of logging, the logger
must evaluate the truthiness of each argument to the logging call, as shown
in this part of the (wrapped) traceback:

/usr/lib64/python2.4/logging/__init__.pyc in __init__
    (self, name, level, pathname, lineno, msg, args, exc_info)
    224         # For the use case of passing a dictionary, this should not be a
    225         # problem.
--> 226         if args and (len(args) == 1) and args[0] and (type(args[0]) == types.DictType):
    227             args = args[0]
    228         self.args = args

As the Gotcha point out, while the stringiness of a Query is unchanged,
the truthiness is now in question. Luckily, the fix is simple (if hackish):

logging.critical('%s', str(query))

2 Comments on “Bitten By SqlAlchemy 0.6 Truthiness”

  1. mike bayer says:

    I’d qualify that as a bug in Python logging. They shouldn’t assume the boolean value of incoming arguments and should instead compare to None.

  2. writeonly says:

    Pleasure to hear from you Mike! As always, SA is amazing.

    I never suggested this was a bug in SA, just that it’s a change that bit me. I will check to see if logging has the same behaviour in later Python versions.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s