Skip to content

Commit

Permalink
feat: Add option to show type annotations in signatures
Browse files Browse the repository at this point in the history
Issue: #106
  • Loading branch information
pawamoy committed Sep 28, 2020
1 parent 54c4f2b commit f94ce9b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
16 changes: 4 additions & 12 deletions docs/handlers/python.md
Expand Up @@ -71,19 +71,11 @@ Option | Type | Description | Default

### Rendering

These options affect how the documentation is rendered.
::: mkdocstrings.handlers.python:PythonRenderer.default_config
rendering:
show_root_toc_entry: false

Option | Type | Description | Default
------ | ---- | ----------- | -------
**`show_root_heading`** | `bool` | Show the heading of the object at the root of the documentation tree. | `False`
**`show_root_toc_entry`** | `bool` | If the root heading is not shown, at least add a ToC entry for it. | `True`
**`show_root_full_path`** | `bool` | Show the full Python path for the root object heading. | `True`
**`show_object_full_path`** | `bool` | Show the full Python path of every object. | `False`
**`show_category_heading`** | `bool` | When grouped by categories, show a heading for each category. | `False`
**`show_if_no_docstring`** | `bool` | Show the object heading even if it has no docstring or children with docstrings. | `False`
**`show_source`** | `bool` | Show the source code of this object. | `True`
**`group_by_category`** | `bool` | Group the object's children by categories: attributes, classes, functions, methods, and modules. | `True`
**`heading_level`** | `int` | The initial heading level to use. | `2`
These options affect how the documentation is rendered.

!!! example "Configuration example"
=== "Global"
Expand Down
2 changes: 2 additions & 0 deletions src/mkdocstrings/handlers/python.py
Expand Up @@ -43,6 +43,7 @@ class PythonRenderer(BaseRenderer):
"show_object_full_path": False,
"show_category_heading": False,
"show_if_no_docstring": False,
"show_signature_annotations": False,
"show_source": True,
"group_by_category": True,
"heading_level": 2,
Expand All @@ -59,6 +60,7 @@ class PythonRenderer(BaseRenderer):
**`show_root_members_full_path`** | `bool` | Show the full Python path of objects that are children of the root object (for example, classes in a module). When False, `show_object_full_path` overrides. | `False`
**`show_category_heading`** | `bool` | When grouped by categories, show a heading for each category. | `False`
**`show_if_no_docstring`** | `bool` | Show the object heading even if it has no docstring or children with docstrings. | `False`
**`show_signature_annotations`** | `bool` | Show the type annotations in methods and functions signatures. | `False`
**`show_source`** | `bool` | Show the source code of this object. | `True`
**`group_by_category`** | `bool` | Group the object's children by categories: attributes, classes, functions, methods, and modules. | `True`
**`heading_level`** | `int` | The initial heading level to use. | `2`
Expand Down
4 changes: 2 additions & 2 deletions src/mkdocstrings/references.py
Expand Up @@ -28,7 +28,7 @@ class Placeholder:
substitution again.
"""

def __init__(self, seed_length=16) -> None:
def __init__(self, seed_length: int = 16) -> None:
"""
Initialize the object.
Expand Down Expand Up @@ -125,7 +125,7 @@ def relative_url(url_a: str, url_b: str) -> str:
return f"{relative}#{anchor}"


def fix_ref(url_map, from_url, unmapped: List[str]) -> Callable: # noqa: WPS231 (not that complex)
def fix_ref(url_map: Dict[str, str], from_url: str, unmapped: List[str]) -> Callable: # noqa: WPS231 (not that complex)
"""
Return a `repl` function for [`re.sub`](https://docs.python.org/3/library/re.html#re.sub).
Expand Down
16 changes: 12 additions & 4 deletions src/mkdocstrings/templates/python/material/signature.html
@@ -1,6 +1,10 @@
{% if signature %}
{% with %}
{% set ns = namespace(render_pos_only_separator=True, render_kw_only_separator=True) %}
{% set ns = namespace(render_pos_only_separator=True, render_kw_only_separator=True, equal="=") %}

{% if config.show_signature_annotations %}
{% set ns.equal = " = " %}
{% endif %}

({% for parameter in signature.parameters %}{% if parameter.kind == "POSITIONAL_ONLY" %}
{% if ns.render_pos_only_separator %}
Expand All @@ -9,14 +13,18 @@
{% if ns.render_kw_only_separator %}
{% set ns.render_kw_only_separator = False %}*, {% endif %}
{% endif %}
{% if config.show_signature_annotations and "annotation" in parameter %}
{% set annotation = ": " + parameter.annotation|safe %}
{% endif %}
{% if "default" in parameter %}
{% set default = "=" + parameter.default|safe %}
{% set default = ns.equal + parameter.default|safe %}
{% endif %}
{% if parameter.kind == "VAR_POSITIONAL" %}*
{% set render_kw_only_separator = False %}
{% elif parameter.kind == "VAR_KEYWORD" %}**
{% endif %}{{ parameter.name }}{{ default }}{% if not loop.last %}, {% endif %}
{% endfor %})
{% endif %}{{ parameter.name }}{{ annotation }}{{ default }}{% if not loop.last %}, {% endif %}
{% endfor %}){% if config.show_signature_annotations and "return_annotation" in signature %} -> {{ signature.return_annotation }}
{% endif %}

{% endwith %}
{% endif %}

0 comments on commit f94ce9b

Please sign in to comment.