Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesn't interact correctly with typing.overload. #296

Closed
hoodmane opened this issue Jan 17, 2023 · 6 comments · Fixed by #297
Closed

Doesn't interact correctly with typing.overload. #296

hoodmane opened this issue Jan 17, 2023 · 6 comments · Fixed by #297

Comments

@hoodmane
Copy link
Collaborator

If you provide a @typing.overload for a function, the output is not correct. As far as I can tell, this problem is not fixable without changes to autodoc.

The is that in the presence of a typing.overload, autodoc will never emit an 'autodoc-process-signature' event. You can see the logic here in FunctionDocumenter.format_signature:
https://github.com/sphinx-doc/sphinx/blob/master/sphinx/ext/autodoc/__init__.py?plain=1#L2193
If self.analyzer.overloads has overloads for this function, it uses overloaded signatures, otherwise it calls super().format_signature. In this case super().format_signature is Documenter.format_signature which actually emits the `'autodoc-process-signature' event:
https://github.com/sphinx-doc/sphinx/blob/master/sphinx/ext/autodoc/__init__.py?plain=1#L513\

So when overloads exist, we fail to override default behavior. An easy fix for this is:

from sphinx.ext.autodoc import FunctionDocumenter
del FunctionDocumenter.format_signature
Example module
from typing import overload


@overload
def f(a : int, b : int) -> None:
    ...

@overload
def f(a : str, b : str) -> None:
    ...

def f(a : int | str, b : int | str) -> None:
    """
    f does the thing. The arguments can either be ints or strings but they must
    both have the same type.

    Parameters
    ----------
    a:
        The first thing
    b:
        The second thing
    """

Actual output

Actual output

Screenshot from 2023-01-16 19-29-54

Desired output

achieved with del FunctionDocumenter.format_signature

Desired output

Screenshot from 2023-01-16 19-30-47

@hoodmane
Copy link
Collaborator Author

I guess you also have to del MethodDocumenter.format_signature.

@gaborbernat
Copy link
Member

PR trying to address this is welcome 🤗

@hoodmane
Copy link
Collaborator Author

Are you okay with adding:

from sphinx.ext.autodoc import FunctionDocumenter, MethodDocumenter

del FunctionDocumenter.format_signature
del MethodDocumenter.format_signature

to setup?

hoodmane added a commit to hoodmane/pyodide that referenced this issue Jan 17, 2023
… functions

Two fixes:
1.
the `pyodide.ffi` stuff is defined in `_pyodide._core_docs`.
We don't want `_pyodide._core_docs` to appear in the documentation
because this isn't where you should import things from so we override
the `__name__` of `_pyodide._core_docs` to be `pyodide.ffi`. But then
Sphinx fails to locate the source for the stuff defined in
`_pyodide._core_docs`. This patches `ModuleAnalyzer` to tell it to
look for the source of things from `pyodide.ffi` in
`_pyodide._core_docs`.

2.
A second problem is that if a function has a typing.overload then
the code path that sphinx-autodoc takes is not one that
sphinx-autodoc-typehints can handle.
See tox-dev/sphinx-autodoc-typehints#296
@gaborbernat
Copy link
Member

I'm willing to try.

@hoodmane
Copy link
Collaborator Author

Okay, PR incoming.

hoodmane added a commit to hoodmane/sphinx-autodoc-typehints that referenced this issue Jan 17, 2023
gaborbernat pushed a commit that referenced this issue Jan 17, 2023
@hoodmane
Copy link
Collaborator Author

Closed by #297.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants