Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to document argument that takes multiple types

In Python 2, is there a canonical way to document that a method may be more than one type?

Here's how I've done it:

def __init__(self, work_order_number):
    """This message communicates that a job is being rerun.  Olio will
    forget everything it knows about the job.

    Args:
        work_order_number (int or str): Work order number
    """
    payload = {
        'work_order_number': str(work_order_number),
    }
    self.content = json.dumps(payload)

where I used the notation (int or str) to indicate that the argument may be either an integer or a string.

How do Python 2 programs usually document that a method argument can be of more than one type? Is there a standard or best practice?

Due to forces beyond my control, I cannot use Python 3 and therefore cannot use annotations

like image 968
Wayne Conrad Avatar asked Mar 25 '15 16:03

Wayne Conrad


1 Answers

The way you did it is fine. There is no exact format that is necessary; you just need to make the intent clear using commas or "or". See for instance various functions from matplotlib or pandas which take arguments documented "str or None", "float, sequence or None", "list-like or integer", etc.

like image 73
BrenBarn Avatar answered Oct 30 '22 15:10

BrenBarn