66import textwrap
77import types
88from ast import FunctionDef , Module , stmt
9+ from functools import lru_cache
910from typing import Any , AnyStr , Callable , ForwardRef , NewType , TypeVar , get_type_hints
1011
1112from sphinx .application import Sphinx
@@ -694,6 +695,29 @@ def validate_config(app: Sphinx, env: BuildEnvironment, docnames: list[str]) ->
694695 raise ValueError (f"typehints_formatter needs to be callable or `None`, not { formatter } " )
695696
696697
698+ @lru_cache () # A cute way to make sure the function only runs once.
699+ def fix_autodoc_typehints_for_overloaded_methods () -> None :
700+ """
701+ sphinx-autodoc-typehints responds to the "autodoc-process-signature" event
702+ to remove types from the signature line of functions.
703+
704+ Normally, `FunctionDocumenter.format_signature` and
705+ `MethodDocumenter.format_signature` call `super().format_signature` which
706+ ends up going to `Documenter.format_signature`, and this last method emits
707+ the `autodoc-process-signature` event. However, if there are overloads,
708+ `FunctionDocumenter.format_signature` does something else and the event
709+ never occurs.
710+
711+ Here we remove this alternative code path by brute force.
712+
713+ See https://github.com/tox-dev/sphinx-autodoc-typehints/issues/296
714+ """
715+ from sphinx .ext .autodoc import FunctionDocumenter , MethodDocumenter
716+
717+ del FunctionDocumenter .format_signature
718+ del MethodDocumenter .format_signature
719+
720+
697721def setup (app : Sphinx ) -> dict [str , bool ]:
698722 app .add_config_value ("always_document_param_types" , False , "html" )
699723 app .add_config_value ("typehints_fully_qualified" , False , "env" )
@@ -707,6 +731,7 @@ def setup(app: Sphinx) -> dict[str, bool]:
707731 app .connect ("env-before-read-docs" , validate_config ) # config may be changed after “config-inited” event
708732 app .connect ("autodoc-process-signature" , process_signature )
709733 app .connect ("autodoc-process-docstring" , process_docstring )
734+ fix_autodoc_typehints_for_overloaded_methods ()
710735 return {"parallel_read_safe" : True }
711736
712737
0 commit comments