77import types
88from ast import FunctionDef , Module , stmt
99from dataclasses import dataclass
10- from functools import lru_cache
1110from typing import Any , AnyStr , Callable , ForwardRef , NewType , TypeVar , get_type_hints
1211
1312from docutils .frontend import OptionParser
1918from sphinx .environment import BuildEnvironment
2019from sphinx .ext .autodoc import Options
2120from sphinx .ext .autodoc .mock import mock
22- from sphinx .ext .napoleon .docstring import GoogleDocstring
2321from sphinx .util import logging
2422from sphinx .util .inspect import signature as sphinx_signature
2523from sphinx .util .inspect import stringify_signature
2624
27- from .attributes_patch import patch_attribute_handling
25+ from .patches import install_patches
2826from .version import __version__
2927
3028_LOGGER = logging .getLogger (__name__ )
@@ -786,82 +784,6 @@ def validate_config(app: Sphinx, env: BuildEnvironment, docnames: list[str]) ->
786784 raise ValueError (f"typehints_formatter needs to be callable or `None`, not { formatter } " )
787785
788786
789- @lru_cache () # A cute way to make sure the function only runs once.
790- def fix_autodoc_typehints_for_overloaded_methods () -> None :
791- """
792- sphinx-autodoc-typehints responds to the "autodoc-process-signature" event
793- to remove types from the signature line of functions.
794-
795- Normally, `FunctionDocumenter.format_signature` and
796- `MethodDocumenter.format_signature` call `super().format_signature` which
797- ends up going to `Documenter.format_signature`, and this last method emits
798- the `autodoc-process-signature` event. However, if there are overloads,
799- `FunctionDocumenter.format_signature` does something else and the event
800- never occurs.
801-
802- Here we remove this alternative code path by brute force.
803-
804- See https://github.com/tox-dev/sphinx-autodoc-typehints/issues/296
805- """
806- from sphinx .ext .autodoc import FunctionDocumenter , MethodDocumenter
807-
808- del FunctionDocumenter .format_signature
809- del MethodDocumenter .format_signature
810-
811-
812- def napoleon_numpy_docstring_return_type_processor (
813- app : Sphinx , what : str , name : str , obj : Any , options : Options | None , lines : list [str ] # noqa: U100
814- ) -> None :
815- """Insert a : under Returns: to tell napoleon not to look for a return type."""
816- if what not in ["function" , "method" ]:
817- return
818- if not getattr (app .config , "napoleon_numpy_docstring" , False ):
819- return
820-
821- # Search for the returns header:
822- # Returns:
823- # --------
824- for idx , line in enumerate (lines [:- 2 ]):
825- if line .lower ().strip (":" ) not in ["return" , "returns" ]:
826- continue
827- # Underline detection.
828- chars = set (lines [idx + 1 ].strip ())
829- # Napoleon allows the underline to consist of a bunch of weirder things...
830- if len (chars ) != 1 or list (chars )[0 ] not in "=-~_*+#" :
831- continue
832- idx = idx + 2
833- break
834- else :
835- return
836-
837- lines .insert (idx , ":" )
838-
839-
840- def fix_napoleon_numpy_docstring_return_type (app : Sphinx ) -> None :
841- """
842- If no return type is explicitly provided, numpy docstrings will mess up and
843- use the return type text as return types.
844- """
845- # standard priority is 500. Setting priority to 499 ensures this runs before
846- # napoleon's docstring processor.
847- app .connect ("autodoc-process-docstring" , napoleon_numpy_docstring_return_type_processor , priority = 499 )
848-
849-
850- def patched_lookup_annotation (* _args : Any ) -> str : # noqa: U101
851- """GoogleDocstring._lookup_annotation sometimes adds incorrect type
852- annotations to constructor parameters (and otherwise does nothing). Disable
853- it so we can handle this on our own.
854- """
855- return ""
856-
857-
858- def patch_google_docstring_lookup_annotation () -> None :
859- """Fix issue 308:
860- https://github.com/tox-dev/sphinx-autodoc-typehints/issues/308
861- """
862- GoogleDocstring ._lookup_annotation = patched_lookup_annotation # type: ignore[assignment]
863-
864-
865787def setup (app : Sphinx ) -> dict [str , bool ]:
866788 app .add_config_value ("always_document_param_types" , False , "html" )
867789 app .add_config_value ("typehints_fully_qualified" , False , "env" )
@@ -875,10 +797,7 @@ def setup(app: Sphinx) -> dict[str, bool]:
875797 app .connect ("env-before-read-docs" , validate_config ) # config may be changed after “config-inited” event
876798 app .connect ("autodoc-process-signature" , process_signature )
877799 app .connect ("autodoc-process-docstring" , process_docstring )
878- fix_autodoc_typehints_for_overloaded_methods ()
879- patch_attribute_handling (app )
880- patch_google_docstring_lookup_annotation ()
881- fix_napoleon_numpy_docstring_return_type (app )
800+ install_patches (app )
882801 return {"parallel_read_safe" : True }
883802
884803
0 commit comments