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

typehints_formatter not applied to signature #429

Open
aeisenbarth opened this issue Feb 23, 2024 · 1 comment
Open

typehints_formatter not applied to signature #429

aeisenbarth opened this issue Feb 23, 2024 · 1 comment

Comments

@aeisenbarth
Copy link

aeisenbarth commented Feb 23, 2024

When type hints are shown in the signature, they are not passed through typehints_formatter.

Example

package
├── docs
│   ├── conf.py
│   └── index.rst
├── my_module
│   └── __init__.py
└── pyproject.toml

conf.py

extensions = [
    "sphinx.ext.autodoc",
    "sphinx.ext.napoleon",
    "sphinx_autodoc_typehints",
    "sphinx.ext.autosummary",
]

# sphinx.ext.napoleon
napoleon_use_rtype = typehints_use_rtype = False

# sphinx.ext.autodoc
# For this issue, this makes no difference regarding the signature, only regarding the description.
# Note that "description" and "both" add a separate "Return type" section which is also not
# formatted by typehints_formatter. 
autodoc_typehints = "both"

# sphinx.ext.autosummary
autosummary_generate = True

# sphinx_autodoc_typehints
typehints_document_rtype = False
typehints_use_signature = True
typehints_use_signature_return = True


def typehints_formatter(annotation, config):
    from typing import TypeVar

    if isinstance(annotation, TypeVar) and annotation.__name__ == "Unformatted":
        return "Formatted"

index.rst

.. autosummary::

   my_module.my_function

my_module/__init__.py

from typing import TypeVar

Unformatted = TypeVar("Unformatted")


def my_function(arg: Unformatted) -> Unformatted:
    """
    Do nothing

    Args:
        arg: An argument

    Returns:
        The argument returned
    """
    return arg

Expected behavior

  • Types are formatted everywhere:
    • Parameter types in signature (because typehints_use_signature = True)
    • Return type in signature (because typehints_use_signature_return = True)
    • Types in "Parameters" section of description (because autodoc_typehints = "both")
    • Types in "Returns" section of description (because autodoc_typehints = "both")
    • Types in "Return type" section of description, if shown (if napoleon_use_rtype = typehints_use_rtype = True)

Actual behavior

  • Parameter types in signature are not formatted.
  • Return type in signature is not formatted.

image

Versions

Platform:              linux; (Linux-5.15.0-91-generic-x86_64-with-glibc2.35)
Python version:        3.9.17 (main, Jul  5 2023, 20:41:20) 
[GCC 11.2.0])
Python implementation: CPython
Sphinx version:        7.2.6
Docutils version:      0.20.1
Jinja2 version:        3.1.2
Pygments version:      2.16.1
sphinx-autodoc-typehints: 2.0.0
@gaborbernat
Copy link
Member

PR welcome to fix this bug 👍

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

2 participants