From 24109875e1e1ce6732d03b08d61837dcdd13c812 Mon Sep 17 00:00:00 2001 From: Ashley Whetter Date: Sun, 31 Jan 2021 15:34:40 -0800 Subject: [PATCH] The overload signatures of __init__ methods are documented Fixes #260 --- CHANGELOG.rst | 7 +++++++ autoapi/templates/python/class.rst | 9 ++++++++- tests/python/py3example/example/example.py | 13 +++++++++++++ tests/python/test_pyintegration.py | 7 +++++++ 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 172fa2f0..fbcce48f 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -13,6 +13,12 @@ Features so that Sphinx can link to them. * Added support for Sphinx 3.3. and 3.4. +Bug Fixes +^^^^^^^^^ + +* `#260 `: + The overload signatures of ``__init__`` methods are documented. + V1.6.0 (2021-01-20) ------------------- @@ -54,6 +60,7 @@ V1.5.1 (2020-10-01) Bug Fixes ^^^^^^^^^ + * Fixed AttributeError when generating an inheritance diagram for a module. diff --git a/autoapi/templates/python/class.rst b/autoapi/templates/python/class.rst index 7efc34f3..4c9fc4c4 100644 --- a/autoapi/templates/python/class.rst +++ b/autoapi/templates/python/class.rst @@ -1,5 +1,12 @@ {% if obj.display %} -.. py:{{ obj.type }}:: {{ obj.short_name }}{% if obj.args %}({{ obj.args }}){% endif %} +.. {{ obj.type }}:: {{ obj.short_name }}{% if obj.args %}({{ obj.args }}){% endif %} +{% if obj.constructor %} + +{% for (args, return_annotation) in obj.constructor.overloads %} + {% if args and args.startswith("self, ") %}{% set args = args[6:] %}{% endif %} + {{ " " * (obj.type | length) }} {{ obj.short_name }}{% if args %}({{ args }}){% endif %} +{% endfor %} +{% endif %} {% if obj.bases %} diff --git a/tests/python/py3example/example/example.py b/tests/python/py3example/example/example.py index a433fe37..9938fffa 100644 --- a/tests/python/py3example/example/example.py +++ b/tests/python/py3example/example/example.py @@ -126,6 +126,19 @@ def overloaded_class_method(cls, a: Union[float, str]) -> Union[float, str]: return a * 2 +class C: + @overload + def __init__(self, a: int) -> None: + ... + + @typing.overload + def __init__(self, a: float) -> None: + ... + + def __init__(self, a: str): + ... + + async def async_function(wait: bool) -> int: """Blah. diff --git a/tests/python/test_pyintegration.py b/tests/python/test_pyintegration.py index ad50619b..6a58fd96 100644 --- a/tests/python/test_pyintegration.py +++ b/tests/python/test_pyintegration.py @@ -219,6 +219,13 @@ def test_overload(self): assert "undoc_overloaded_func" in example_file assert "undoc_overloaded_method" in example_file + assert "C(a: int" in example_file + assert "C(a: float" in example_file + assert "C(a: str" not in example_file + assert "C(self, a: int" not in example_file + assert "C(self, a: float" not in example_file + assert "C(self, a: str" not in example_file + def test_async(self): example_path = "_build/text/autoapi/example/index.txt" with io.open(example_path, encoding="utf8") as example_handle: