Repository: sympy/sympy
File: doc/ext/numpydoc.py (Python)

Code before:
if sphinx.__version__ < '1.0.1':
    raise RuntimeError("Sphinx 1.0.1 or newer is required")

from docscrape_sphinx import get_doc_object, SphinxDocString


def mangle_docstrings(app, what, name, obj, options, lines,
                      reference_offset=[0]):

    cfg = {'use_plots': app.config.numpydoc_use_plots,
           'show_class_members': app.config.numpydoc_show_class_members,
           'show_inherited_class_members':
           app.config.numpydoc_show_inherited_class_members,
           'class_members_toctree': app.config.numpydoc_class_members_toctree}

    u_NL = '\n'
    if what == 'module':
        # Strip top title
        pattern = '^\\s*[#*=]{4,}\\n[a-z0-9 -]+\\n[#*=]{4,}\\s*'
        title_re = re.compile(pattern, re.IGNORECASE | re.DOTALL)
        lines[:] = title_re.sub('', u_NL.join(lines)).split(u_NL)
    else:
        if inspect.isbuiltin(obj):
            return
        doc = get_doc_object(obj, what, u_NL.join(lines), config=cfg)
        doc = str(doc)
        lines[:] = doc.split(u_NL)

    if (app.config.numpydoc_edit_link and hasattr(obj, '__name__') and
            obj.__name__):
        if hasattr(obj, '__module__'):
            v = {"full_name": "{}.{}".format(obj.__module__, obj.__name__)}
        else:
            v = {"full_name": obj.__name__}
        lines += ['', '.. htmlonly::', '']
        lines += ['    %s' % x for x in
                  (app.config.numpydoc_edit_link % v).split("\n")]

    # replace reference numbers so that there are no duplicates
    references = []
    for line in lines:
        line = line.strip()
        m = re.match('^.. \\[([a-z0-9_.-])\\]', line, re.IGNORECASE)
        if m:
            references.append(m.group(1))

    # start renaming from the longest string, to avoid overwriting parts
    references.sort(key=lambda x: -len(x))
    if references:
        for i, line in enumerate(lines):
            for r in references:

Review comment (suggestion):
Not all builtin functions have invalid signatures:
```
In [7]: import math

In [8]: import inspect, math, functools

In [9]: inspect.signature(math.cos)
Out[9]: <Signature (x, /)>

In [10]: inspect.signature(functools.reduce)
...
ValueError: <built-in function reduce> builtin has invalid signature 
```
Probably the best thing to do here is catch the exception from signature somewhere.

Code after:
if sphinx.__version__ < '1.0.1':
    raise RuntimeError("Sphinx 1.0.1 or newer is required")

from docscrape_sphinx import get_doc_object, SphinxDocString


def mangle_docstrings(app, what, name, obj, options, lines,
                      reference_offset=[0]):

    cfg = {'use_plots': app.config.numpydoc_use_plots,
           'show_class_members': app.config.numpydoc_show_class_members,
           'show_inherited_class_members':
           app.config.numpydoc_show_inherited_class_members,
           'class_members_toctree': app.config.numpydoc_class_members_toctree}

    u_NL = '\n'
    if what == 'module':
        # Strip top title
        pattern = '^\\s*[#*=]{4,}\\n[a-z0-9 -]+\\n[#*=]{4,}\\s*'
        title_re = re.compile(pattern, re.IGNORECASE | re.DOTALL)
        lines[:] = title_re.sub('', u_NL.join(lines)).split(u_NL)
    else:
        try:
            doc = get_doc_object(obj, what, u_NL.join(lines), config=cfg)
        except ValueError:
            return
        doc = str(doc)
        lines[:] = doc.split(u_NL)

    if (app.config.numpydoc_edit_link and hasattr(obj, '__name__') and
            obj.__name__):
        if hasattr(obj, '__module__'):
            v = {"full_name": "{}.{}".format(obj.__module__, obj.__name__)}
        else:
            v = {"full_name": obj.__name__}
        lines += ['', '.. htmlonly::', '']
        lines += ['    %s' % x for x in
                  (app.config.numpydoc_edit_link % v).split("\n")]

    # replace reference numbers so that there are no duplicates
    references = []
    for line in lines:
        line = line.strip()
        m = re.match('^.. \\[([a-z0-9_.-])\\]', line, re.IGNORECASE)
        if m:
            references.append(m.group(1))

    # start renaming from the longest string, to avoid overwriting parts
    references.sort(key=lambda x: -len(x))
    if references:
        for i, line in enumerate(lines):