Thursday, 12 July 2018

Altering traceback of a non-callable module

I'm a minor contributor to a package where people are meant to do this (Foo.Bar.Bar is a class):

>>> from Foo.Bar import Bar
>>> s = Bar('a')

Sometimes people do this by mistake (Foo.Bar is a module):

>>> from Foo import Bar   
>>> s = Bar('a')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable

This might seems simple, but users still fail to debug it, I would like to make it easier. I can't change the names of Foo or Bar but I would like to add a more informative traceback like:

TypeError("'module' object is not callable, perhaps you meant to call 'Bar.Bar()'")

I read the Callable modules Q&A, and I know that I can't add a __call__ method to a module (and I don't want to wrap the whole module in a class just for this). Anyway, I don't want the module to be callable, I just want a custom traceback. Is there a clean solution for Python 3.x and 2.7+?



from Altering traceback of a non-callable module

No comments:

Post a Comment