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

@overload expands common type unions #301

Open
mhostetter opened this issue Nov 5, 2023 · 5 comments
Open

@overload expands common type unions #301

mhostetter opened this issue Nov 5, 2023 · 5 comments

Comments

@mhostetter
Copy link
Contributor

I have noticed that when using overloads, type unions like npt.ArrayLike are expanded and become illegible. Not sure if this is a Sphinx or Sphinx Immaterial issue.

Here is a brief reproducer. Let me know if you need a full self-contained example.

from typing import Any, overload

import numpy.typing as npt


@overload
def function(x: npt.ArrayLike, return_int: bool = True) -> int:
    ...


@overload
def function(x: npt.ArrayLike, return_int: bool = False) -> float:
    ...


def function(x: Any, return_int: Any = False) -> Any:
    if return_int:
        return 42
    else:
        return 42.0

Renders as this:

image

With these relevant warnings:

/home/matt/repos/sdr/sdr.function:1: WARNING: py:class reference target not found: numpy._typing._array_like._SupportsArray
/home/matt/repos/sdr/sdr.function:1: WARNING: py:class reference target not found: numpy._typing._nested_sequence._NestedSequence
/home/matt/repos/sdr/sdr.function:1: WARNING: py:class reference target not found: numpy._typing._array_like._SupportsArray
/home/matt/repos/sdr/sdr.function:1: WARNING: py:class reference target not found: numpy._typing._nested_sequence._NestedSequence
/home/matt/repos/sdr/sdr.function:1: WARNING: py:class reference target not found: numpy._typing._array_like._SupportsArray
/home/matt/repos/sdr/sdr.function:1: WARNING: py:class reference target not found: numpy._typing._nested_sequence._NestedSequence
/home/matt/repos/sdr/sdr.function:1: WARNING: py:class reference target not found: numpy._typing._array_like._SupportsArray
/home/matt/repos/sdr/sdr.function:1: WARNING: py:class reference target not found: numpy._typing._nested_sequence._NestedSequence
@2bndy5
Copy link
Collaborator

2bndy5 commented Nov 5, 2023

The warnings should be satisfied when intersphinx is given URLs that define the references. Although the references named look like they're supposed to be private, so maybe they aren't included in public docs (& corresponding intersphinx inventories).

@2bndy5
Copy link
Collaborator

2bndy5 commented Nov 5, 2023

Not sure if this is a Sphinx or Sphinx Immaterial issue.

Me neither. Does this same behavior happen when using alabaster theme with autodoc?

@jbms
Copy link
Owner

jbms commented Nov 5, 2023

I think you need to use the future annotations feature to retain type aliases.

@mhostetter
Copy link
Contributor Author

I think you need to use the future annotations feature to retain type aliases.

I did have from __future__ import annotations at the top of my file. I forgot to include it in my reproducer.

@mhostetter
Copy link
Contributor Author

I still run into this issue, any thoughts? I find myself changing the APIs and functionality of functions to avoid these unreadable call signatures. I appreciate the help! 🙏

Reproducer file:

from __future__ import annotations

from typing import Any, overload

import numpy.typing as npt


@overload
def function(x: npt.ArrayLike, return_int: bool = True) -> int: ...


@overload
def function(x: npt.ArrayLike, return_int: bool = False) -> float: ...


def function(x: Any, return_int: Any = False) -> Any:
    """
    Group:
        dsp-fir-filtering
    """
    if return_int:
        return 42
    else:
        return 42.0

Relevant config:

nitpicky = True
nitpick_ignore = [
    ("py:class", "numpy.typing.ArrayLike"),  # Wish this would work
    ("py:class", "numpy.typing.DTypeLike"),  # Wish this would work
    ("py:class", "numpy.typing.NDArray"),  # Wish this would work
]
intersphinx_mapping = {
    "python": ("https://docs.python.org/3", None),
    "numpy": ("https://numpy.org/doc/stable/", None),
    "scipy": ("http://docs.scipy.org/doc/scipy/reference/", None),
    "matplotlib": ("http://matplotlib.sourceforge.net/", None),
    "galois": ("https://mhostetter.github.io/galois/latest/", None),
}
autodoc_type_aliases = {
    "npt.ArrayLike": "~numpy.typing.ArrayLike",
    "npt.DTypeLike": "~numpy.typing.DTypeLike",
    "npt.NDArray": "~numpy.typing.NDArray",
}
python_type_aliases = autodoc_type_aliases

Relevant warnings:

/home/matt/repos/sdr/sdr.function:1: WARNING: py:class reference target not found: numpy._typing._array_like._SupportsArray
/home/matt/repos/sdr/sdr.function:1: WARNING: py:class reference target not found: numpy._typing._nested_sequence._NestedSequence
/home/matt/repos/sdr/sdr.function:1: WARNING: py:class reference target not found: numpy._typing._array_like._SupportsArray
/home/matt/repos/sdr/sdr.function:1: WARNING: py:class reference target not found: numpy._typing._nested_sequence._NestedSequence
/home/matt/repos/sdr/sdr.function:1: WARNING: py:class reference target not found: numpy._typing._array_like._SupportsArray
/home/matt/repos/sdr/sdr.function:1: WARNING: py:class reference target not found: numpy._typing._nested_sequence._NestedSequence
/home/matt/repos/sdr/sdr.function:1: WARNING: py:class reference target not found: numpy._typing._array_like._SupportsArray
/home/matt/repos/sdr/sdr.function:1: WARNING: py:class reference target not found: numpy._typing._nested_sequence._NestedSequence

Output:

image

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

No branches or pull requests

3 participants