The inspect.cleandoc
function essentially implements the trim
algorithm from PEP 257. You could use that to do your trimming.
Since your new docstring has no indentation, in order to combine it with the old one, you need to get rid of the indentation in the old one. So what you'll probably have to do is trim the existing docstring and then append your new one. That is, change the next-to-last line of record_number
to:
add_and_record.__doc__ = inspect.cleandoc(func.__doc__) + decorator_doc
This gives the right result when I try it with your example, although I don't know for sure if there would be edge cases where it gives weird output for docstrings with more complex formatting.