Skip to content

Commit

Permalink
feat: Add show_signature rendering option
Browse files Browse the repository at this point in the history
Co-authored-by: Will Da Silva <will@willdasilva.xyz>
  • Loading branch information
pawamoy and WillDaSilva committed Dec 27, 2021
1 parent 8f0aa42 commit 0f07c2e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 30 deletions.
4 changes: 3 additions & 1 deletion src/mkdocstrings/handlers/python/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class PythonRenderer(BaseRenderer):
"show_object_full_path": False,
"show_category_heading": False,
"show_if_no_docstring": False,
"show_signature": True,
"show_signature_annotations": False,
"separate_signature": False,
"line_length": 60,
Expand All @@ -91,7 +92,8 @@ 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_signature`** | `bool` | Show method and function signatures. | `True`
**`show_signature_annotations`** | `bool` | Show the type annotations in method and function signatures. | `False`
**`separate_signature`** | `bool` | Whether to put the whole signature in a foldable code block below the heading. | `False`
**`line_length`** | `int` | Maximum line length when formatting code. | `60`
**`show_source`** | `bool` | Show the source code of this object. | `True`
Expand Down
61 changes: 32 additions & 29 deletions src/mkdocstrings/templates/python/material/_base/signature.html
Original file line number Diff line number Diff line change
@@ -1,39 +1,42 @@
{{ log.debug() }}
{%- with -%}
{%- set ns = namespace(render_pos_only_separator=True, render_kw_only_separator=True, equal="=") -%}
{%- if config.show_signature -%}
{%- with -%}

{%- if config.show_signature_annotations -%}
{%- set ns.equal = " = " -%}
{%- endif -%}
{%- set ns = namespace(render_pos_only_separator=True, render_kw_only_separator=True, equal="=") -%}

(
{%- for parameter in function.parameters -%}
{%- if config.show_signature_annotations -%}
{%- set ns.equal = " = " -%}
{%- endif -%}

{%- if parameter.kind.value == "positional-only" -%}
{%- if ns.render_pos_only_separator -%}
{%- set ns.render_pos_only_separator = False %}/, {% endif -%}
{%- elif parameter.kind.value == "keyword-only" -%}
{%- if ns.render_kw_only_separator -%}
{%- set ns.render_kw_only_separator = False %}*, {% endif -%}
{%- endif -%}
(
{%- for parameter in function.parameters -%}

{%- if config.show_signature_annotations and parameter.annotation is not none -%}
{%- set annotation = ": " + parameter.annotation|safe -%}
{%- endif -%}
{%- if parameter.kind.value == "positional-only" -%}
{%- if ns.render_pos_only_separator -%}
{%- set ns.render_pos_only_separator = False %}/, {% endif -%}
{%- elif parameter.kind.value == "keyword-only" -%}
{%- if ns.render_kw_only_separator -%}
{%- set ns.render_kw_only_separator = False %}*, {% endif -%}
{%- endif -%}

{%- if parameter.default is not none and parameter.kind.value != "variadic positional" and parameter.kind.value != "variadic keyword" -%}
{%- set default = ns.equal + parameter.default|safe -%}
{%- endif -%}
{%- if config.show_signature_annotations and parameter.annotation is not none -%}
{%- set annotation = ": " + parameter.annotation|safe -%}
{%- endif -%}

{%- if parameter.kind.value == "variadic positional" -%}
{%- set ns.render_kw_only_separator = False -%}
{%- endif -%}
{%- if parameter.default is not none and parameter.kind.value != "variadic positional" and parameter.kind.value != "variadic keyword" -%}
{%- set default = ns.equal + parameter.default|safe -%}
{%- endif -%}

{{ parameter.name }}{{ annotation }}{{ default }}
{%- if not loop.last %}, {% endif -%}
{%- if parameter.kind.value == "variadic positional" -%}
{%- set ns.render_kw_only_separator = False -%}
{%- endif -%}

{%- endfor -%}
)
{%- if config.show_signature_annotations and "return_annotation" in signature %} -> {{ signature.return_annotation }}{%- endif -%}
{{ parameter.name }}{{ annotation }}{{ default }}
{%- if not loop.last %}, {% endif -%}

{%- endwith -%}
{%- endfor -%}
)
{%- if config.show_signature_annotations and "return_annotation" in signature %} -> {{ signature.return_annotation }}{%- endif -%}

{%- endwith -%}
{%- endif -%}

0 comments on commit 0f07c2e

Please sign in to comment.