A Hy macro for defining functions with docstrings on each argument
Posted March 19, 2017 at 07:47 PM | categories: python, hylang | tags:
Updated March 19, 2017 at 07:53 PM
For functions with a lot of arguments, python style docstrings leave something to be desired. For one, they are not that close to the arguments, so if you have a function with say 20 arguments, the docstring might take up a whole page! That means they are hard to keep synchronized too. Let's not argue now over the merits of a function with 20+ arguments, it is enough that they exist, and are a problem.
So what are typical documentation standards? Here is a Numpy style doc string:
def func(arg1, arg2): """multiply arg1 and arg2 Parameters ---------- arg1 : a number arg2 : a number """ return arg1 * arg2
It works well for a small number of arguments with limited descriptions. This is a proper docstring that is accessible by introspection and pydoc. With much longer argument lists, this falls apart. I will not pick on any code in particular here, but suffice it to say I was inspired today to think of a better way. There are some other documentation solutions at http://stackoverflow.com/questions/9195455/how-to-document-a-method-with-parameters, but None of them are better in my opinion. I want accessible docstrings by instrospection, and only if that is unavailable do I want to read the code! Finally, if I have to read the code, I want it to be easy to figure out, which means the documentation is close to the arguments.
There is bad news, I do not have one for vanilla python. Python does not even give you a way to deal with this. But, if we had a lisp, we could make a macro to help us out. In fact, we have a lisp with hy! And we can use a macro to make a syntax that lets us keep the docstring close to the argument, and that constructs a real docstring so we get help later!
Here it is:
(defmacro mydef [func args &optional docstring &rest body] `(defn ~func [~@(map (lambda [x] (nth x 0)) args)] ~(+ (if docstring (+ docstring "\n\n") "") "Parameters\n----------\n" (.join "\n" (map (lambda [x] (.format "{} : {}" (nth x 0) (nth x 1))) args))) ~@body))
We can checkout how it expands like this:
(print (macroexpand '(mydef f [(a "an int") (b "an int")] "some doc" (* a b))))
('setv' 'f' ('fn' ['a' 'b'] 'some doc\n\nParameters\n----------\na : an int\nb : an int' ('*' 'a' 'b')))
That looks ok. Now, for an example of using that. Here is the same function we defined before, but I put the documentation for each argument with the argument.
(mydef func ((arg1 "a number") (arg2 "a number")) "Multiply arg1 by arg2" (* arg1 arg2))
We can use the function now like a regular function.
(print (func 24 3))
72
And now for the help.
(help func)
Help on function func in module __main__: func(arg1, arg2) Multiply arg1 by arg2 Parameters ---------- arg1 : a number arg2 : a number
Now, that should amaze and astonish you if you are a vanilla Pythonista! We have our cake, and we eat it too. You just can not make up your own syntax that way in Python. Imagine, we could add type information, validation code, etc… into that macro. Maybe it could even be possible to store argument dependent documentation on the function, say in the function dictionary. That would require some conventions I guess, but they could become introspectable then. For example, in this vanilla Python:
def f(x): return x*x f.__dict__['args'] = {'x': 'A number'} print(f.__dict__)
{'args': {'x': 'A number'}}
In the end, this does not really solve all the problems I have with current docstrings in Python. It does solve a problem with writing and reading the code by keeping documentation close to the arguments, but ultimately the docstring from Python's point of view will basically look the same. It is pretty awesome that it is even possible. Hy lisp for the win here (again!).
Copyright (C) 2017 by John Kitchin. See the License for information about copying.
Org-mode version = 9.0.5