[Python-Dev] Should assert continue to do a LOAD_GLOBAL on AssertionError?

Benjamin Peterson benjamin at python.org
Wed Oct 3 22:38:34 EDT 2018

On Wed, Oct 3, 2018, at 08:59, Steven D'Aprano wrote:
> On the bug tracker, there's a discussion about the current behaviour of 
> the assert statement, where shadowing AssertionError will change the 
> behaviour of the assertion.
> https://bugs.python.org/issue34880
> Currently, assert does a LOAD_GLOBAL on AssertionError, which means if 
> you shadow the name, you get a different exception. This behaviour goes 
> back to Python 1.5.
> I'm looking for guidance here, is this the intended behaviour, or an 
> accident? Should it be changed to better match other builtins?

The behavior certainly has been relied on historically by py.test. By replacing builtins.AssertionError, you can improve the error message of the AssertionError by, e.g., inspecting the failing frame. py.test's code to do this was deleted in 2016, but other code bases may still be relying on this hack. It's probably okay to change the behavior in 3.8 with the understanding that a revert may be necessary if some clever hack surfaces.

More information about the Python-Dev mailing list