<!-- Thank you for contributing to Pyodide! All improvements are welcome,
     so don't be afraid to make a PR. -->

<!-- [IMPORTANT] Note on CI failures:
     Currently, we are having issues with selenium-based tests.
     Don't panic if the CI fails on your PR because of timeouts.
     It's probably not your fault. We will investigate :) -->

### Description

<!-- Please explain what your PR is about:
     - reasoning for the change
     - some details of updated code
     - any noteworthy choices to be aware of
	Please refer to any related issues by #<issue_id> -->

Working on using pre-commit properly. Will be a series of commits, and undrafted after they are complete.

Rough plan:

- [x] Commit 1: update pre-commit, prepare mypy to run correctly.
- [x] Next commit: apply pre-commit fixes
- [x] Next commit: use pre-commit instead of manual runs inside Makefile.
- [x] Somewhere in between commit: fix manual issues

### Checklists

<!-- Note on checklists:
     If you think some of these steps are not necessary for your PR,
     just remove those checkboxes, or mark them as checked. Otherwise,
     if some checkboxes are left unmarked, we might assume that your PR
     is not ready to be merged and we might keep you waiting -->

style: improve pre-commit. <!-- Thank you for contributing to Pyodide! All improvements are welcome,
     so don't be afraid to make a PR. -->

<!-- [IMPORTANT] Note on CI failures:
     Currently, we are having issues with selenium-based tests.
     Don't panic if the CI fails on your PR because of timeouts.
     It's probably not your fault. We will investigate :) -->

### Description

<!-- Please explain what your PR is about:
     - reasoning for the change
     - some details of updated code
     - any noteworthy choices to be aware of
	Please refer to any related issues by #<issue_id> -->

Working on using pre-commit properly. Will be a series of commits, and undrafted after they are complete.

Rough plan:

- [x] Commit 1: update pre-commit, prepare mypy to run correctly.
- [x] Next commit: apply pre-commit fixes
- [x] Next commit: use pre-commit instead of manual runs inside Makefile.
- [x] Somewhere in between commit: fix manual issues

### Checklists

<!-- Note on checklists:
     If you think some of these steps are not necessary for your PR,
     just remove those checkboxes, or mark them as checked. Otherwise,
     if some checkboxes are left unmarked, we might assume that your PR
     is not ready to be merged and we might keep you waiting -->

from textwrap import dedent


def dedent_docstring(docstring):
    """This removes initial spaces from the lines of the docstring.

    After the first line of the docstring, all other lines will include some
    spaces. This removes them.

    Examples
    --------
    >>> from _pyodide.docstring import dedent_docstring
    >>> dedent_docstring(dedent_docstring.__doc__).split("\\n")[2]
    'After the first line of the docstring, all other lines will include some'
    """
    first_newline = docstring.find("\n")
    if first_newline == -1:
        return docstring
    docstring = docstring[:first_newline] + dedent(docstring[first_newline:])
    return docstring


def get_cmeth_docstring(func):
    """Get the value to use for the PyMethodDef.ml_doc attribute for a builtin
    function. This is used in docstring.c.

    The ml_doc should start with a signature which cannot have any type
    annotations. The signature must end with the exact characters ")\n--\n\n".
    For example: "funcname(arg1, arg2)\n--\n\n"

    See:
    https://github.com/python/cpython/blob/v3.8.2/Objects/typeobject.c#L84

    Examples
    --------
    >>> from _pyodide.docstring import get_cmeth_docstring
    >>> get_cmeth_docstring(sum)[:80]
    "sum(iterable, /, start=0)\\n--\\n\\nReturn the sum of a 'start' value (default: 0) plu"
    """
    from inspect import signature, _empty

    sig = signature(func)
    # remove param and return annotations and
    for param in sig.parameters.values():
        param._annotation = _empty
    sig._return_annotation = _empty

    return func.__name__ + str(sig) + "\n--\n\n" + dedent_docstring(func.__doc__)

from textwrap import dedent


def dedent_docstring(docstring):
    """This removes initial spaces from the lines of the docstring.

    After the first line of the docstring, all other lines will include some
    spaces. This removes them.

    Examples
    --------
    >>> from _pyodide.docstring import dedent_docstring
    >>> dedent_docstring(dedent_docstring.__doc__).split("\\n")[2]
    'After the first line of the docstring, all other lines will include some'
    """
    first_newline = docstring.find("\n")
    if first_newline == -1:
        return docstring
    docstring = docstring[:first_newline] + dedent(docstring[first_newline:])
    return docstring


def get_cmeth_docstring(func):
    """Get the value to use for the PyMethodDef.ml_doc attribute for a builtin
    function. This is used in docstring.c.

    The ml_doc should start with a signature which cannot have any type
    annotations. The signature must end with the exact characters ")\n--\n\n".
    For example: "funcname(arg1, arg2)\n--\n\n"

    See:
    https://github.com/python/cpython/blob/v3.8.2/Objects/typeobject.c#L84

    Examples
    --------
    >>> from _pyodide.docstring import get_cmeth_docstring
    >>> get_cmeth_docstring(sum)[:80]
    "sum(iterable, /, start=0)\\n--\\n\\nReturn the sum of a 'start' value (default: 0) plu"
    """
    from inspect import signature, _empty

    sig = signature(func)
    # remove param and return annotations and
    for param in sig.parameters.values():
        param._annotation = _empty
    sig._return_annotation = _empty

    return func.__name__ + str(sig) + "\n--\n\n" + dedent_docstring(func.__doc__)
