7
7
import types
8
8
from ast import FunctionDef , Module , stmt
9
9
from dataclasses import dataclass
10
- from functools import lru_cache
11
10
from typing import Any , AnyStr , Callable , ForwardRef , NewType , TypeVar , get_type_hints
12
11
13
12
from docutils .frontend import OptionParser
19
18
from sphinx .environment import BuildEnvironment
20
19
from sphinx .ext .autodoc import Options
21
20
from sphinx .ext .autodoc .mock import mock
22
- from sphinx .ext .napoleon .docstring import GoogleDocstring
23
21
from sphinx .util import logging
24
22
from sphinx .util .inspect import signature as sphinx_signature
25
23
from sphinx .util .inspect import stringify_signature
26
24
27
- from .attributes_patch import patch_attribute_handling
25
+ from .patches import install_patches
28
26
from .version import __version__
29
27
30
28
_LOGGER = logging .getLogger (__name__ )
@@ -786,82 +784,6 @@ def validate_config(app: Sphinx, env: BuildEnvironment, docnames: list[str]) ->
786
784
raise ValueError (f"typehints_formatter needs to be callable or `None`, not { formatter } " )
787
785
788
786
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
-
865
787
def setup (app : Sphinx ) -> dict [str , bool ]:
866
788
app .add_config_value ("always_document_param_types" , False , "html" )
867
789
app .add_config_value ("typehints_fully_qualified" , False , "env" )
@@ -875,10 +797,7 @@ def setup(app: Sphinx) -> dict[str, bool]:
875
797
app .connect ("env-before-read-docs" , validate_config ) # config may be changed after “config-inited” event
876
798
app .connect ("autodoc-process-signature" , process_signature )
877
799
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 )
882
801
return {"parallel_read_safe" : True }
883
802
884
803
0 commit comments