pythonzope.interface

Where do I put exception definitions common to an interface?


I am getting into the habit of using zope.interface when designing certain classes, which is all well and nice, but I am a bit unsure about exceptions. I haven't seen any docs talking about them (albeit after cursory searching only). Primarily, I am wondering where to put a certain exception X that is common to any implementation of an interface.

I could always put the exception in its own module. But a whole module for what may be a simple exception? I wouldn't mind putting it in the same module that defines the interface in question, but I am wondering whether that is considered bad form in some way or another?

Basically I'd like to hear from someone that has used interfaces a bit and knows the conventions for this.


Solution

  • Best practice is to have a interfaces module, that includes all documented interfaces and corresponding exceptions, constants and events.

    This way, you define the API of your package in one well-defined place, and someone looking up an interface can see that any exceptions being thrown are defined in the same location.

    Here are some example interface.py files from various projects: