A PEP Style Guide for Error Messages?

This is less a full fledged idea and more of an invitation to get thoughts and feedback.

Is there a widely established and uniform (or semi-uniform) style guide for error message handling?

In PEP8 currently, it mentions exceptions briefly, stating about exception hierarchies:

"Aim to answer the question “What went wrong?” programmatically, rather than only stating that “A problem occurred…”

But PEP8 doesn’t state best practices for logging errors, such as:

  • when to include a repr in an error message (if at all)
  • what to include in the error message
  • whether or not to include a traceback, and if so, how
  • what form a recovery message for an error should take (e.g. something along the lines of NNGroup’s Error Message Guidelines for user experience design)

I think a PEP that spells out the above could go a long way in helping new coders write better code that’s easier to diagnose and fix when errors arise. But I also acquiesce that I don’t know all the answers or best practices and I’d like to hear folks’ thoughts on this.

1 Like

I like the idea of centralising good patterns for Python exceptions.

There are a range of tools and examples in popular libraries, but I agree that PEP8 and the Python docs are maybe a bit light on details.

A general discussion of what makes a good message would be beneficial, but I don’t know if that belongs in docs.python.org or somewhere more general in nature?

In terms of ‘style’, there is also the consideration of how to create and use custom exceptions.

Here are a few examples and linters that I’ve used to try to make useful and clear custom exceptions:

3 Likes