Skip to content

Commit 823c146

Browse files
laggykillerAWhetter
authored andcommitted
Correct handling of __init__.pyi
Closes #405
1 parent bbb50f6 commit 823c146

File tree

8 files changed

+73
-2
lines changed

8 files changed

+73
-2
lines changed

autoapi/mappers/python/mapper.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ def _find_files(self, patterns, dirs, ignore):
268268
dir_root = dir_
269269
if (
270270
os.path.exists(os.path.join(dir_, "__init__.py"))
271+
or os.path.exists(os.path.join(dir_, "__init__.pyi"))
271272
or self._use_implicit_namespace
272273
):
273274
dir_root = os.path.abspath(os.path.join(dir_, os.pardir))

autoapi/mappers/python/parser.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def _get_full_name(self, name):
2424
def _parse_file(self, file_path, condition):
2525
directory, filename = os.path.split(file_path)
2626
module_parts = []
27-
if filename != "__init__.py":
27+
if filename != "__init__.py" and filename != "__init__.pyi":
2828
module_part = os.path.splitext(filename)[0]
2929
module_parts = [module_part]
3030
module_parts = collections.deque(module_parts)
@@ -40,7 +40,10 @@ def _parse_file(self, file_path, condition):
4040
def parse_file(self, file_path):
4141
return self._parse_file(
4242
file_path,
43-
lambda directory: os.path.isfile(os.path.join(directory, "__init__.py")),
43+
lambda directory: (
44+
os.path.isfile(os.path.join(directory, "__init__.py"))
45+
or os.path.isfile(os.path.join(directory, "__init__.pyi"))
46+
),
4447
)
4548

4649
def parse_file_in_namespace(self, file_path, dir_root):

docs/changes/398.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix submodule with `__init__.pyi` documented as `__init__` instead of submodule name
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# -*- coding: utf-8 -*-
2+
3+
templates_path = ["_templates"]
4+
source_suffix = ".rst"
5+
master_doc = "index"
6+
project = "pyisubmoduleinit"
7+
copyright = "2015, readthedocs"
8+
author = "readthedocs"
9+
version = "0.1"
10+
release = "0.1"
11+
language = "en"
12+
exclude_patterns = ["_build"]
13+
pygments_style = "sphinx"
14+
todo_include_todos = False
15+
html_theme = "alabaster"
16+
htmlhelp_basename = "pyisubmoduleinitdoc"
17+
extensions = ["sphinx.ext.autodoc", "autoapi.extension"]
18+
autoapi_dirs = ["example"]

tests/python/pyisubmoduleinit/example/example.py

Whitespace-only changes.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# -*- coding: utf-8 -*-
2+
"""Example __init__ in submodule foo
3+
4+
Documentation generated for this file
5+
should be titled submodule_foo instead of __init__
6+
7+
This is a description
8+
"""
9+
10+
class Foo(object):
11+
"""
12+
This is a description
13+
"""
14+
15+
def bar(self, a: int) -> None:
16+
"""
17+
This is a description
18+
"""
19+
...
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Welcome to pyisubmoduleinit's documentation!
2+
============================================
3+
4+
.. toctree::
5+
6+
autoapi/index
7+
8+
9+
Indices and tables
10+
==================
11+
12+
* :ref:`genindex`
13+
* :ref:`modindex`
14+
* :ref:`search`
15+

tests/python/test_pyintegration.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,20 @@ def test_integration(self, parse):
300300
assert foo_sig
301301

302302

303+
class TestStubInitModuleInSubmodule:
304+
@pytest.fixture(autouse=True, scope="class")
305+
def built(self, builder):
306+
builder("pyisubmoduleinit", warningiserror=True)
307+
308+
def test_integration(self, parse):
309+
example_file = parse("_build/html/autoapi/example/index.html")
310+
311+
# Documentation should list
312+
# submodule_foo instead of __init__
313+
assert example_file.find(title="submodule_foo")
314+
assert not example_file.find(title="__init__")
315+
316+
303317
class TestPy3Module:
304318
@pytest.fixture(autouse=True, scope="class")
305319
def built(self, builder):

0 commit comments

Comments
 (0)