Skip to main content

Isn't it Pythonic?

·1 min

I really should be over this by now.

Here’s how you test exceptions in Python 2.7 and later:

with self.assertRaises(SomeException) as cm:

the_exception = cm.exception
self.assertEqual(the_exception.error_code, 3)

(as recommended in the unittest documentation)

Here’s how you test exceptions in earlier Pythons, using libraries like testtools.

the_exception = self.assertRaises(SomeException, do_something)
self.assertEqual(the_exception.error_code, 3)

I still like the second way. It’s simpler, uses less code, and doesn’t rely on new syntax. I’m sorry it didn’t work out that way.

I guess this is how Barry feels about the diamond operator.