views:

436

answers:

7

Can I make assert throw an exception that I choose instead of AssertionError?

UPDATE:

I'll explain my motivation: Up to now, I've had assertion-style tests that raised my own exceptions; For example, when you created a Node object with certain arguments, it would check if the arguments were good for creating a node, and if not it would raise NodeError.

But I know that Python has a -o mode in which asserts are skipped, which I would like to have available because it would make my program faster. But I would still like to have my own exceptions. That's why I want to use assert with my own exceptions.

+5  A: 

This will work. But it's kind of crazy.

try:
    assert False, "A Message"
except AssertionError, e:
    raise Exception( e.args )

Why not the following? This is less crazy.

if not someAssertion: raise Exception( "Some Message" )

It's only a little wordier than the assert statement, but doesn't violate our expectation that assert failures raise AssertionError.

Consider this.

def myAssert( condition, action ):
    if not condition: raise action

Then you can more-or-less replace your existing assertions with something like this.

myAssert( {{ the original condition }}, MyException( {{ the original message }} ) )

Once you've done this, you are now free to fuss around with enable or disabling or whatever it is you're trying to do.

Also, read up on the warnings module. This may be exactly what you're trying to do.

S.Lott
I want to be able to enjoy the `-o` option. Your crazy solution might be okay, if the try doesn't cause too much overhead.
cool-RR
To check for the -o optiont you can simply say `if __debug__ and not someAssertion:`
Ber
@cool-RR: Try has no overhead.
S.Lott
Interesting! Then I might use your code.
cool-RR
I think try does have overhead. Try it yourself http://stackoverflow.com/questions/1569049/making-pythons-assert-throw-an-exception-that-i-choose/1569618#1569618
gnibbler
`try` has at least a few opcodes of overhead - `SETUP_EXCEPT`, `JUMP_FORWARD`/`JUMP_ABSOLUTE` and `POP_BLOCK`.
Nick Bastin
@Nick Bastin: Yes, there's some overhead. try/except sometimes turns out faster than if. Haven't run any benchmarks because it's never been the bottleneck for me.
S.Lott
@S.Lott: Sure, but the whole goal here is to not even use `if` in production, and only test in development.
Nick Bastin
@Nick Bastin: I think it would be more clear to use a function definition and with two implementations. One that has an `if` statement and one that has `pass`. This would be less astonishing for folks who will have to maintain and adapt this.
S.Lott
+2  A: 

In Python 2.6.3 at least, this will also work:

class MyAssertionError (Exception):
    pass

AssertionError = MyAssertionError

assert False, "False"


Traceback (most recent call last):
  File "assert.py", line 8, in <module>
    assert False, "False"
__main__.MyAssertionError: False
Ber
Cute :) But too hacky.
cool-RR
Yeah, it's hacky, but maybe the question is hacky, too :)
Ber
Actually, to me this doesn't look too bad, though it only works at global level. You can't assign AssertionError inside a function and have the assert raise your exception.
Ned Batchelder
Assigning something to AssertionError is probably the only way. If you think you have a good idea and syntax why not write a PEP
gnibbler
The only time I can recall using asserts lately is in unittests, which is a good place for them
gnibbler
+2  A: 

How about this?


>>> def myraise(e): raise e
... 
>>> cond=False
>>> assert cond or myraise(RuntimeError)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 1, in myraise
RuntimeError

gnibbler
The force is strong with this one!
cool-RR
A: 

You can let a context manager do the conversion for you, inside a with block (which may contain more than one assertion, or more code and function calls or what you want.

from __future__ import with_statement
import contextlib

@contextlib.contextmanager
def myassert(exctype):
    try:
        yield
    except AssertionError, exc:
        raise exctype(*exc.args)

with myassert(ValueError):
    assert 0, "Zero is bad for you"

See a previous version of this answer for substituting constructed exception objects directly (KeyError("bad key")), instead of reusing the assertions' argument(s).

kaizer.se
+1  A: 

Never use an assertion for logic! Only for optional testing checks. Remember, if Python is running with optimizations turned on, asserts aren't even compiled into the bytecode. If you're doing this, you obviously care about the exception being raised and if you care, then you're using asserts wrong in the first place.

ironfroggy
This way you at least put the "optimization" to work. Who ever uses optimization!? It seems pointless at the outset; using more assertions like this at least provides you with more optimization choice, so it's interesting.
kaizer.se
+1  A: 
gnibbler
+2  A: 

Python also skips if __debug__: blocks when run with -o option. The following code is more verbose, but does what you need without hacks:

def my_assert(condition, message=None):
    if not condition:
        raise MyAssertError(message)

if __debug__: my_assert(condition, message)

You can make it shorter by moving if __debug__: condition inside my_assert(), but then it will be called (without any action inside) when optimization is enabled.

Denis Otkidach
Thanks, I did not know __debug__ worked like that. I added it to my testcases. The answer currently with the most votes *does* have overhead however.
gnibbler