Skip to content

Commit

Permalink
Refine overload support
Browse files Browse the repository at this point in the history
  • Loading branch information
ciscorn committed Aug 16, 2020
1 parent e0daf89 commit aa7217f
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 34 deletions.
2 changes: 1 addition & 1 deletion autoapi/directives.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def get_items(self, names):
for name in names:
obj = mapper.all_objects[name]
if isinstance(obj, PythonFunction):
if len(obj.signatures) > 1:
if obj.overloads:
sig = "(\u2026)"
else:
sig = "({})".format(obj.args)
Expand Down
7 changes: 2 additions & 5 deletions autoapi/mappers/python/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,8 @@ def __init__(self, obj, **kwargs):
:type: list(str)
"""
self.signatures = obj["signatures"]
"""The list of all signatures ``[(args, return_annotation), ...]`` of this function.
When this function is not overloaded,
it must be the same as ``[(self.args, self.return_annotation)]``.
self.overloads = obj["overloads"]
"""The list of overloaded signatures ``[(args, return_annotation), ...]`` of this function.
:type: list(tuple(str, str))
"""
Expand Down
12 changes: 3 additions & 9 deletions autoapi/mappers/python/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ def parse_functiondef(self, node): # pylint: disable=too-many-branches
"return_annotation": return_annotation,
"properties": properties,
"is_overload": astroid_utils.is_decorated_with_overload(node),
"overloads": [],
}

if type_ in ("method", "property"):
Expand Down Expand Up @@ -291,21 +292,14 @@ def _parse_child(node, child_data, overloads, base=None, name=None):
name = single_data["name"]
if name in overloads:
grouped = overloads[name]
if single_data["doc"]:
grouped["doc"] += "\n\n" + single_data["doc"]
grouped["doc"] = single_data["doc"]
if single_data["is_overload"]:
grouped["signatures"].append(
grouped["overloads"].append(
(single_data["args"], single_data["return_annotation"])
)
else:
grouped["args"] = single_data["args"]
grouped["return_annotation"] = single_data["return_annotation"]
continue
if single_data["is_overload"] and name not in overloads:
overloads[name] = single_data
single_data["signatures"] = [
(single_data["args"], single_data["return_annotation"])
]

if base:
single_data["inherited"] = base is not node
Expand Down
9 changes: 3 additions & 6 deletions autoapi/templates/python/function.rst
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
{% if obj.display %}
{% for (args, return_annotation) in obj.signatures %}
{% if loop.index0 == 0 %}
.. function:: {{ obj.short_name }}({{ args }}){% if return_annotation is not none %} -> {{ return_annotation }}{% endif %}
.. function:: {{ obj.short_name }}({{ obj.args }}){% if obj.return_annotation is not none %} -> {{ obj.return_annotation }}{% endif %}

{% else %}
{% for (args, return_annotation) in obj.overloads %}
{{ obj.short_name }}({{ args }}){% if return_annotation is not none %} -> {{ return_annotation }}{% endif %}

{% endif %}
{% endfor %}
{% endfor %}
{% if sphinx_version >= (2, 1) %}
{% for property in obj.properties %}
:{{ property }}:
Expand Down
19 changes: 6 additions & 13 deletions autoapi/templates/python/method.rst
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
{%- if obj.display %}
{% if sphinx_version >= (2, 1) %}
{% for (args, return_annotation) in obj.signatures %}
{% if loop.index0 == 0 %}
.. method:: {{ obj.short_name }}({{ args }}){% if return_annotation is not none %} -> {{ return_annotation }}{% endif %}
.. method:: {{ obj.short_name }}({{ obj.args }}){% if obj.return_annotation is not none %} -> {{ obj.return_annotation }}{% endif %}

{% else %}
{% for (args, return_annotation) in obj.overloads %}
{{ obj.short_name }}({{ args }}){% if return_annotation is not none %} -> {{ return_annotation }}{% endif %}

{% endif %}
{% endfor %}
{% if obj.properties %}
{% for property in obj.properties %}
Expand All @@ -18,15 +15,11 @@

{% endif %}
{% else %}
{% for (args, return_annotation) in obj.signatures %}
{% if loop.index0 == 0 %}
.. {{ obj.method_type }}:: {{ obj.short_name }}({{ args }})
{% else %}
:: {{ obj.short_name }}({{ args }})

{% endif %}
.. {{ obj.method_type }}:: {{ obj.short_name }}({{ obj.args }})
{% for (args, return_annotation) in obj.overloads %}
{{ " " * (obj.method_type | length) }} {{ obj.short_name }}({{ args }})
{% endfor %}

{% endif %}
{% if obj.docstring %}
{{ obj.docstring|prepare_docstring|indent(3) }}
Expand Down

0 comments on commit aa7217f

Please sign in to comment.