Turning SAWarnings into exceptions

147 views
Skip to first unread message

Chris Withers

unread,
Sep 15, 2016, 8:07:45 AM9/15/16
to sqlalchemy
Hi All,

How can I turn SAWarnings into exceptions?

I'm struggling with what to put into the PYTHONWARNINGS environment
variable :-S

cheers,

Chris

Mike Bayer

unread,
Sep 15, 2016, 8:44:03 AM9/15/16
to sqlal...@googlegroups.com

import warnings

warnings.simplefilter("error")

Chris Withers

unread,
Sep 15, 2016, 9:39:03 AM9/15/16
to sqlal...@googlegroups.com
Right, but then a bunch of other errors (ImportWarning?!,
DeprecationWarning, etc) stop execution even reaching the code which
might be causing the SAWarning.

It's really quite disappointing that Python's warning mechanisms don't
report a full traceback...

Simon King

unread,
Sep 15, 2016, 10:10:09 AM9/15/16
to sqlal...@googlegroups.com
According to https://docs.python.org/2/using/cmdline.html#cmdoption-W,
the full form of -W (and PYTHONWARNINGS) is:

action:message:category:module:line

Empty fields are ignored, and unused trailing fields can be left out,
so maybe "error::SAWarning" would work?

Simon
> --
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sqlalchemy+...@googlegroups.com.
> To post to this group, send email to sqlal...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

Simon King

unread,
Sep 15, 2016, 10:21:02 AM9/15/16
to sqlal...@googlegroups.com
OK, so it ought to be:

error::sqlalchemy.exc.SAWarning

...instead, but that doesn't work because of
https://bugs.python.org/issue22543, so you need to do it in code. I
have this in my top-level py.test conftest.py:

@pytest.fixture(scope='session', autouse=True)
def sqlalchemywarnings():
"Convert SQLAlchemy warnings to exceptions"
import sqlalchemy.exc
warnings.simplefilter(
'error',
sqlalchemy.exc.SAWarning
)


Simon

Michal Petrucha

unread,
Sep 15, 2016, 10:22:34 AM9/15/16
to sqlal...@googlegroups.com
On Thu, Sep 15, 2016 at 03:09:44PM +0100, Simon King wrote:
> According to https://docs.python.org/2/using/cmdline.html#cmdoption-W,
> the full form of -W (and PYTHONWARNINGS) is:
>
> action:message:category:module:line
>
> Empty fields are ignored, and unused trailing fields can be left out,
> so maybe "error::SAWarning" would work?

The problem with this is that -W and PYTHONWARNINGS are evaluated
*before* any user code gets imported, which means there is no
SAWarning at that time, so this doesn't work. At least it didn't work
for me when I was trying to do this myself a few months ago, and I
found this as an explanation. (I didn't save a reference, though,
unfortunately.)

What I ended up doing was to enable all warnings, and then whitelist
some of them one by one (IIRC, I only had to restore the default
behavior for ImportError and DeprecationWarning, but I'm not entirely
certain anymore – I'm pretty sure it wasn't more than a small handful,
though, of course, this will also depend on the packages you use).

Good luck,

Michal
signature.asc
Reply all
Reply to author
Forward
0 new messages