bohrsty
bohrsty

Reputation: 426

Is there a shortcut for python docstring to document implicit raised exception?

I'm working on a few python modules whose methods (by design) don't catch exceptions from functions called in their bodies. As far as I understand the results of my research, you should document any relevant exceptions in docstring :raises ...: blocks, in my case to primarily help users or their IDEs, not for automated documentation.

As given in the sample below, I'd have to repeat the :raises ...: blocks in several methods with the same information:

def myFunction(param1, param2):
  """
  :param str param1: first parameter...
  :param int param2: second parameter...
  :return None:
  :raises MyException1: in case...
  :raises MyException2: (from myFunction2) in case...
  :raises ValueError: (from myFunction3) in case...
  """
  if param1 is None:
    raise MyException1
  
  myFunction2(param1, param2)
  
def myFunction2(param1, param2):
  """
  :param str param1: first parameter...
  :param int param2: second parameter...
  :return void:
  :raises MyException2: in case...
  :raises ValueError: (from myFunction3) in case...
  """
  if param2 is None:
    raise MyException2
  
  myFunction3(param1)
  
def myFunction3(param):
  """
  :param str param: parameter...
  :return None:
  :raises ValueError: in case...
  """
  if param is None:
    raise ValueError

Is there a way to say in myFunction1 "here could be raised: myException1 and all exceptions from myFunction2 recursively" and so on in myFunction2?

I.e.:

def myFunction(param1, param2):
  """
  :param str param1: first parameter...
  :param int param2: second parameter...
  :return None:
  :raises MyException1: in case...
  :raisesfrom myFunction2: ??? does anything like this exists, or some other way ???
  """
  if param1 is None:
    raise MyException1
  
  myFunction2(param1, param2)
  
  ...

Upvotes: 0

Views: 71

Answers (0)

Related Questions