Skip to content

Commit

Permalink
Added support for parsing stub files
Browse files Browse the repository at this point in the history
Closes #100
  • Loading branch information
AWhetter committed Apr 6, 2019
1 parent b5d83f8 commit dd58809
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 2 deletions.
2 changes: 1 addition & 1 deletion autoapi/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
)

default_file_mapping = {
"python": ["*.py"],
"python": ["*.py", "*.pyi"],
"dotnet": ["project.json", "*.csproj", "*.vbproj"],
"go": ["*.go"],
"javascript": ["*.js"],
Expand Down
2 changes: 1 addition & 1 deletion autoapi/mappers/python/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def parse_file(self, file_path):
module_parts.appendleft(module_part)

module_name = ".".join(module_parts)
node = astroid.MANAGER.ast_from_file(file_path, module_name)
node = astroid.MANAGER.ast_from_file(file_path, module_name, source=True)
return self.parse(node)

def parse_assign(self, node):
Expand Down
21 changes: 21 additions & 0 deletions tests/python/pyiexample/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-

templates_path = ["_templates"]
source_suffix = ".rst"
master_doc = "index"
project = u"pyexample"
copyright = u"2015, rtfd"
author = u"rtfd"
version = "0.1"
release = "0.1"
language = None
exclude_patterns = ["_build"]
pygments_style = "sphinx"
todo_include_todos = False
html_theme = "alabaster"
html_static_path = ["_static"]
htmlhelp_basename = "pyexampledoc"
extensions = ["sphinx.ext.autodoc", "autoapi.extension"]
autoapi_type = "python"
autoapi_dirs = ["example"]
autoapi_file_pattern = "*.py"
41 changes: 41 additions & 0 deletions tests/python/pyiexample/example/example.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# -*- coding: utf-8 -*-
"""Example module
This is a description
"""

class Foo(object):
"""Can we parse arguments from the class docstring?
:param attr: Set an attribute.
:type attr: str
"""

class_var = 42 #: Class var docstring

another_class_var = 42
"""Another class var docstring"""

class_var_without_value = ...
"""A class var without a value."""
class Meta(object):
"""A nested class just to test things out"""

@classmethod
def foo():
"""The foo class method"""
...
def __init__(self, attr):
"""Constructor docstring"""
...
def method_okay(self, foo=None, bar=None):
"""This method should parse okay"""
...
def method_multiline(self, foo=None, bar=None, baz=None):
"""This is on multiple lines, but should parse okay too
pydocstyle gives us lines of source. Test if this means that multiline
definitions are covered in the way we're anticipating here
"""
...
def method_without_docstring(self): ...
27 changes: 27 additions & 0 deletions tests/python/pyiexample/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
.. pyexample documentation master file, created by
sphinx-quickstart on Fri May 29 13:34:37 2015.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
Welcome to pyexample's documentation!
=====================================

.. toctree::

autoapi/index
manualapi

Contents:

.. toctree::
:maxdepth: 2



Indices and tables
==================

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`

26 changes: 26 additions & 0 deletions tests/python/test_pyintegration.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import io
import os
import shutil
import sys

import pytest
import sphinx
Expand Down Expand Up @@ -85,6 +86,31 @@ def test_napoleon_integration_not_loaded(self, builder):
assert "Returns" in example_file


@pytest.mark.skipif(
sys.version_info < (3,), reason="Ellipsis is invalid method contents in Python 2"
)
class TestSimpleStubModule(object):
@pytest.fixture(autouse=True, scope="class")
def built(self, builder):
builder("pyiexample")

def test_integration(self):
example_path = "_build/text/autoapi/example/index.txt"
with io.open(example_path, encoding="utf8") as example_handle:
example_file = example_handle.read()

assert "class example.Foo" in example_file
assert "class Meta" in example_file
assert "Another class var docstring" in example_file
assert "A class var without a value." in example_file
assert "method_okay(self, foo=None, bar=None)" in example_file
assert "method_multiline(self, foo=None, bar=None, baz=None)" in example_file
assert "method_without_docstring(self)" in example_file

# Are constructor arguments from the class docstring parsed?
assert "Set an attribute" in example_file


def test_napoleon_integration_loaded(builder):
confoverrides = {
"extensions": ["autoapi.extension", "sphinx.ext.autodoc", "sphinx.ext.napoleon"]
Expand Down

0 comments on commit dd58809

Please sign in to comment.