From f3f0c9cd5ca59db1adea9f61cb17735fffa4ebf3 Mon Sep 17 00:00:00 2001 From: Priyansh Agrawal Date: Tue, 8 Oct 2024 21:53:26 +0100 Subject: [PATCH 1/3] Apply typehints_formatter to signature --- src/sphinx_autodoc_typehints/__init__.py | 12 +++++++- tests/test_integration.py | 35 ++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/sphinx_autodoc_typehints/__init__.py b/src/sphinx_autodoc_typehints/__init__.py index d2b1b06..b3fff66 100644 --- a/src/sphinx_autodoc_typehints/__init__.py +++ b/src/sphinx_autodoc_typehints/__init__.py @@ -354,9 +354,19 @@ def process_signature( # noqa: C901, PLR0913, PLR0917 obj = inspect.unwrap(obj) sph_signature = sphinx_signature(obj, type_aliases=app.config["autodoc_type_aliases"]) + typehints_formatter: Callable[..., str | None] | None = getattr(app.config, "typehints_formatter", None) + + def _get_formatted_annotation(annotation): + if typehints_formatter is None: + return annotation + formatted_name = typehints_formatter(annotation) + return annotation if not isinstance(formatted_name, str) else TypeVar(formatted_name) + + if app.config.typehints_use_signature_return: + sph_signature = sph_signature.replace(return_annotation=_get_formatted_annotation(sph_signature.return_annotation)) if app.config.typehints_use_signature: - parameters = list(sph_signature.parameters.values()) + parameters = [param.replace(annotation=_get_formatted_annotation(param.annotation)) for param in sph_signature.parameters.values()] else: parameters = [param.replace(annotation=inspect.Parameter.empty) for param in sph_signature.parameters.values()] diff --git a/tests/test_integration.py b/tests/test_integration.py index 3339916..8cc69c6 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -1385,6 +1385,41 @@ def has_doctest1() -> None: """ +Unformatted = TypeVar("Unformatted") + +@warns("cannot cache unpickable configuration value: 'typehints_formatter'") +@expected( + """ + mod.typehints_formatter_applied_to_signature(param: Formatted) -> Formatted + + Do nothing + + Parameters: + **param** (Formatted) -- A parameter + + Return type: + Formatted + + Returns: + The return value + """, + typehints_use_signature=True, + typehints_use_signature_return=True, + typehints_formatter=lambda ann, cfg=None: "Formatted", +) +def typehints_formatter_applied_to_signature(param: Unformatted) -> Unformatted: + """ + Do nothing + + Args: + param: A parameter + + Returns: + The return value + """ + return param + + # Config settings for each test run. # Config Name: Sphinx Options as Dict. configs = { From c6b88ed6e4d7572b583765c76539aed3807d26ed Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 8 Oct 2024 20:57:52 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/sphinx_autodoc_typehints/__init__.py | 9 +++++++-- tests/test_integration.py | 1 + 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/sphinx_autodoc_typehints/__init__.py b/src/sphinx_autodoc_typehints/__init__.py index b3fff66..8ea71c4 100644 --- a/src/sphinx_autodoc_typehints/__init__.py +++ b/src/sphinx_autodoc_typehints/__init__.py @@ -363,10 +363,15 @@ def _get_formatted_annotation(annotation): return annotation if not isinstance(formatted_name, str) else TypeVar(formatted_name) if app.config.typehints_use_signature_return: - sph_signature = sph_signature.replace(return_annotation=_get_formatted_annotation(sph_signature.return_annotation)) + sph_signature = sph_signature.replace( + return_annotation=_get_formatted_annotation(sph_signature.return_annotation) + ) if app.config.typehints_use_signature: - parameters = [param.replace(annotation=_get_formatted_annotation(param.annotation)) for param in sph_signature.parameters.values()] + parameters = [ + param.replace(annotation=_get_formatted_annotation(param.annotation)) + for param in sph_signature.parameters.values() + ] else: parameters = [param.replace(annotation=inspect.Parameter.empty) for param in sph_signature.parameters.values()] diff --git a/tests/test_integration.py b/tests/test_integration.py index 8cc69c6..9fe8ac6 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -1387,6 +1387,7 @@ def has_doctest1() -> None: Unformatted = TypeVar("Unformatted") + @warns("cannot cache unpickable configuration value: 'typehints_formatter'") @expected( """ From d23c260b8332261726be79f3843be7f906d22184 Mon Sep 17 00:00:00 2001 From: Priyansh Agrawal Date: Tue, 8 Oct 2024 22:17:26 +0100 Subject: [PATCH 3/3] Fix lint --- src/sphinx_autodoc_typehints/__init__.py | 2 +- tests/test_integration.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sphinx_autodoc_typehints/__init__.py b/src/sphinx_autodoc_typehints/__init__.py index 8ea71c4..e67180f 100644 --- a/src/sphinx_autodoc_typehints/__init__.py +++ b/src/sphinx_autodoc_typehints/__init__.py @@ -356,7 +356,7 @@ def process_signature( # noqa: C901, PLR0913, PLR0917 sph_signature = sphinx_signature(obj, type_aliases=app.config["autodoc_type_aliases"]) typehints_formatter: Callable[..., str | None] | None = getattr(app.config, "typehints_formatter", None) - def _get_formatted_annotation(annotation): + def _get_formatted_annotation(annotation: TypeVar) -> TypeVar: if typehints_formatter is None: return annotation formatted_name = typehints_formatter(annotation) diff --git a/tests/test_integration.py b/tests/test_integration.py index 9fe8ac6..93aa1e5 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -1406,7 +1406,7 @@ def has_doctest1() -> None: """, typehints_use_signature=True, typehints_use_signature_return=True, - typehints_formatter=lambda ann, cfg=None: "Formatted", + typehints_formatter=lambda _, __=None: "Formatted", ) def typehints_formatter_applied_to_signature(param: Unformatted) -> Unformatted: """