| 1 |
{#- Template for signatures. |
| 2 |
|
| 3 |
This template renders the signature of a function or method. |
| 4 |
It iterates over the parameters of the function to rebuild the signature. |
| 5 |
The signature is the list of parameters of a function or method, including their names, default values, and annotations. |
| 6 |
|
| 7 |
Context: |
| 8 |
function (griffe.Function): The function or method to render. |
| 9 |
config (dict): The configuration options. |
| 10 |
-#} |
| 11 |
|
| 12 |
{%- if config.show_signature -%} |
| 13 |
{%- block logs scoped -%} |
| 14 |
{#- Logging block. |
| 15 |
|
| 16 |
This block can be used to log debug messages, deprecation messages, warnings, etc. |
| 17 |
-#} |
| 18 |
{{ log.debug("Rendering signature") }} |
| 19 |
{%- endblock logs -%} |
| 20 |
{%- with -%} |
| 21 |
|
| 22 |
{%- set ns = namespace( |
| 23 |
has_pos_only=False, |
| 24 |
render_pos_only_separator=True, |
| 25 |
render_kw_only_separator=True, |
| 26 |
annotation="", |
| 27 |
equal="=", |
| 28 |
default=False, |
| 29 |
) -%} |
| 30 |
|
| 31 |
( |
| 32 |
{%- for parameter in function.parameters -%} |
| 33 |
{%- if parameter.name not in ("self", "cls") or loop.index0 > 0 or not (function.parent and function.parent.is_class) -%} |
| 34 |
|
| 35 |
{#- Handle parameter kind. -#} |
| 36 |
{%- if parameter.kind.value == "positional-only" -%} |
| 37 |
{%- set ns.has_pos_only = True -%} |
| 38 |
{%- else -%} |
| 39 |
{%- if ns.has_pos_only and ns.render_pos_only_separator -%} |
| 40 |
{%- set ns.render_pos_only_separator = False %}/, {% endif -%} |
| 41 |
{%- if parameter.kind.value == "keyword-only" -%} |
| 42 |
{%- if ns.render_kw_only_separator -%} |
| 43 |
{%- set ns.render_kw_only_separator = False %}*, {% endif -%} |
| 44 |
{%- endif -%} |
| 45 |
{%- endif -%} |
| 46 |
|
| 47 |
{#- Prepare type annotation. -#} |
| 48 |
{%- if config.show_signature_annotations and parameter.annotation is not none -%} |
| 49 |
{%- set ns.equal = " = " -%} |
| 50 |
{%- if config.separate_signature -%} |
| 51 |
{%- with expression = parameter.annotation -%} |
| 52 |
{%- set ns.annotation -%}: {% with backlink_type = "used-by" -%} |
| 53 |
{%- include "expression.html.jinja" with context -%} |
| 54 |
{%- endwith -%}{%- endset -%} |
| 55 |
{%- endwith -%} |
| 56 |
{%- else -%} |
| 57 |
{%- set ns.annotation = ": " + parameter.annotation|safe -%} |
| 58 |
{%- endif -%} |
| 59 |
{%- else -%} |
| 60 |
{%- set ns.equal = "=" -%} |
| 61 |
{%- set ns.annotation = "" -%} |
| 62 |
{%- endif -%} |
| 63 |
|
| 64 |
{#- Prepare default value. -#} |
| 65 |
{%- if parameter.default is not none and parameter.kind.value != "variadic positional" and parameter.kind.value != "variadic keyword" -%} |
| 66 |
{%- set ns.default = True -%} |
| 67 |
{%- else -%} |
| 68 |
{%- set ns.default = False -%} |
| 69 |
{%- endif -%} |
| 70 |
|
| 71 |
{#- TODO: Move inside kind handling above? -#} |
| 72 |
{%- if parameter.kind.value == "variadic positional" -%} |
| 73 |
{%- set ns.render_kw_only_separator = False -%} |
| 74 |
{%- endif -%} |
| 75 |
|
| 76 |
{#- Prepare name. -#} |
| 77 |
{%- set param_prefix -%} |
| 78 |
{%- if parameter.kind.value == "variadic positional" -%} |
| 79 |
* |
| 80 |
{%- elif parameter.kind.value == "variadic keyword" -%} |
| 81 |
** |
| 82 |
{%- endif -%} |
| 83 |
{%- endset -%} |
| 84 |
|
| 85 |
{#- Render parameter name with optional cross-reference to its heading. -#} |
| 86 |
{{ param_prefix }} |
| 87 |
{%- if config.separate_signature and config.parameter_headings and config.signature_crossrefs -%} |
| 88 |
{%- filter stash_crossref(length=parameter.name|length) -%} |
| 89 |
{%- with func_path = function.path -%} |
| 90 |
{%- if config.merge_init_into_class and func_path.endswith(".__init__") -%} |
| 91 |
{%- set func_path = func_path[:-9] -%} |
| 92 |
{%- endif -%} |
| 93 |
<autoref identifier="{{ func_path }}({{ param_prefix }}{{ parameter.name }})" optional>{{ parameter.name }}</autoref> |
| 94 |
{%- endwith -%} |
| 95 |
{%- endfilter -%} |
| 96 |
{%- else -%} |
| 97 |
{{ parameter.name }} |
| 98 |
{%- endif -%} |
| 99 |
|
| 100 |
{#- Render parameter annotation. -#} |
| 101 |
{{ ns.annotation }} |
| 102 |
|
| 103 |
{#- Render parameter default value. -#} |
| 104 |
{%- if ns.default -%} |
| 105 |
{{ ns.equal }} |
| 106 |
{%- if config.signature_crossrefs and config.separate_signature -%} |
| 107 |
{%- with expression = parameter.default, backlink_type = "used-by" -%} |
| 108 |
{%- include "expression.html.jinja" with context -%} |
| 109 |
{%- endwith -%} |
| 110 |
{%- else -%} |
| 111 |
{{ parameter.default|safe }} |
| 112 |
{%- endif -%} |
| 113 |
{%- endif -%} |
| 114 |
|
| 115 |
{%- if not loop.last %}, {% endif -%} |
| 116 |
|
| 117 |
{%- endif -%} |
| 118 |
{%- endfor -%} |
| 119 |
) |
| 120 |
|
| 121 |
{#- Render return type. -#} |
| 122 |
{%- if config.show_signature_annotations |
| 123 |
and function.annotation |
| 124 |
and not (config.merge_init_into_class and function.name == "__init__" ) |
| 125 |
%} -> {% if config.separate_signature and config.signature_crossrefs -%} |
| 126 |
{%- with expression = function.annotation, backlink_type = "returned-by" -%} |
| 127 |
{%- include "expression.html.jinja" with context -%} |
| 128 |
{%- endwith -%} |
| 129 |
{%- else -%} |
| 130 |
{{ function.annotation|safe }} |
| 131 |
{%- endif -%} |
| 132 |
{%- endif -%} |
| 133 |
|
| 134 |
{%- endwith -%} |
| 135 |
{%- endif -%} |