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

Added options to retain original typehints in signatures #278

Merged
merged 6 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -83,6 +83,8 @@ The following configuration options are accepted:
- `typehints_formatter` (default: `None`): If set to a function, this function will be called with `annotation` as first
argument and `sphinx.config.Config` argument second. The function is expected to return a string with reStructuredText
code or `None` to fall back to the default formatter.
- `typehints_use_signature` (default: `False`): If `True`, typehints for parameters in the signature are shown.
- `typehints_use_signature_return` (default: `False`): If `True`, return annotations in the signature are shown.

## How it works

Expand Down
13 changes: 11 additions & 2 deletions src/sphinx_autodoc_typehints/__init__.py
Expand Up @@ -254,7 +254,11 @@ def process_signature(

obj = inspect.unwrap(obj)
sph_signature = sphinx_signature(obj)
parameters = [param.replace(annotation=inspect.Parameter.empty) for param in sph_signature.parameters.values()]

if app.config.typehints_use_signature:
parameters = list(sph_signature.parameters.values())
else:
parameters = [param.replace(annotation=inspect.Parameter.empty) for param in sph_signature.parameters.values()]

# if we have parameters we may need to delete first argument that's not documented, e.g. self
start = 0
Expand All @@ -277,7 +281,10 @@ def process_signature(
if not isinstance(method_object, (classmethod, staticmethod)):
start = 1

sph_signature = sph_signature.replace(parameters=parameters[start:], return_annotation=inspect.Signature.empty)
sph_signature = sph_signature.replace(parameters=parameters[start:])
if not app.config.typehints_use_signature_return:
sph_signature = sph_signature.replace(return_annotation=inspect.Signature.empty)

return stringify_signature(sph_signature).replace("\\", "\\\\"), None


Expand Down Expand Up @@ -634,6 +641,8 @@ def setup(app: Sphinx) -> dict[str, bool]:
app.add_config_value("typehints_defaults", None, "env")
app.add_config_value("simplify_optional_unions", True, "env")
app.add_config_value("typehints_formatter", None, "env")
app.add_config_value("typehints_use_signature", False, "env")
app.add_config_value("typehints_use_signature_return", False, "env")
app.connect("env-before-read-docs", validate_config) # config may be changed after “config-inited” event
app.connect("autodoc-process-signature", process_signature)
app.connect("autodoc-process-docstring", process_docstring)
Expand Down