Skip to content

Commit 3588162

Browse files
authored
Add mypy check to CI and tox (#42)
Update code to be compatible with newest mypy
1 parent a6fe043 commit 3588162

File tree

5 files changed

+43
-22
lines changed

5 files changed

+43
-22
lines changed

src/sphinx_c_autodoc/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,9 +452,12 @@ def generate(
452452
"""
453453
generate stuff
454454
"""
455+
# The autodoc::Documenter is using the implied optional
456+
# `real_modname: str = None` and mypy complains that this shouldn't be
457+
# optional
455458
super().generate(
456459
more_content=more_content,
457-
real_modname=real_modname,
460+
real_modname=real_modname, # type: ignore
458461
check_module=check_module,
459462
all_members=all_members,
460463
)
@@ -749,7 +752,7 @@ class CModule(CObject):
749752

750753
object_type = "module"
751754

752-
def run(self) -> nodes.Node:
755+
def run(self) -> List[nodes.Node]:
753756
"""
754757
Not sure yet
755758
"""

src/sphinx_c_autodoc/loader.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ class DocumentedObject:
4343
"""
4444
A representation of a c object for documentation purposes.
4545
46+
Arguments:
47+
node (:class:`~clang.cindex.Cursor`): The node representing this object.
48+
4649
Attributes:
4750
type_ (str): The type of this item one of:
4851
@@ -69,15 +72,15 @@ class DocumentedObject:
6972
the type as well as the name.
7073
_line_range (Tuple[int, int]): The line range of the C construct,
7174
this will include any leading or trailing comments that may be
72-
part of the constructs documentation.
75+
part of the construct's documentation.
7376
"""
7477

7578
type_ = "object"
7679

77-
def __init__(self) -> None:
80+
def __init__(self, node: Cursor) -> None:
7881
self.doc = ""
7982
self.name = ""
80-
self.node: Cursor = None
83+
self.node = node
8184
self._children: Optional[OrderedDict] = None
8285
self._soup: Optional[BeautifulSoup] = None
8386
self._declaration: Optional[str] = None
@@ -88,7 +91,7 @@ def line_range(self) -> Tuple[int, int]:
8891
The lines in the source file that this object covers.
8992
9093
This will include any leading or trailing comments that may be part
91-
of the constructs documentation.
94+
of the construct's documentation.
9295
"""
9396
if self._line_range is None:
9497
node_extent = self.node.extent
@@ -554,9 +557,9 @@ def get_parsed_declaration(self) -> str:
554557
# Function prototypes need to be handled different. When clang can't
555558
# successfully parse the file it falls back to naming the return type
556559
# as the display name.
557-
# Unfortunatly some versions of clang behave a little differently, some
558-
# will return a POINTER while others will return FUNCITONNOPROTO. The
559-
# POINTER's are easy to derive the real type from, but the test
560+
# Unfortunately some versions of clang behave a little differently, some
561+
# will return a `POINTER` while others will return `FUNCITONNOPROTO`. The
562+
# `POINTER`s are easy to derive the real type from, but the test
560563
# environment doesn't use that version of clang.
561564
type_ = self.node.underlying_typedef_type
562565
if type_.kind == cindex.TypeKind.POINTER: # pragma: no cover
@@ -753,14 +756,13 @@ def object_from_cursor(cursor: Cursor) -> Optional[DocumentedObject]:
753756

754757
nested_cursor = get_nested_node(cursor)
755758
class_ = CURSORKIND_TO_OBJECT_CLASS.get(nested_cursor.kind, DocumentedObject)
756-
doc = class_()
759+
doc = class_(nested_cursor)
757760

758761
doc.name = name
759762
psuedo_comment = PsuedoToken(
760763
nested_cursor.raw_comment, nested_cursor.comment_extent
761764
)
762765
doc.doc = parse_comment(psuedo_comment)
763-
doc.node = nested_cursor
764766

765767
return doc
766768

@@ -821,7 +823,9 @@ def get_file_comment(cursor: Cursor, child: Optional[Cursor]) -> str:
821823
return ""
822824

823825

824-
def get_compilation_args(filename: str, compilation_database: str = None) -> List[str]:
826+
def get_compilation_args(
827+
filename: str, compilation_database: Optional[str] = None
828+
) -> List[str]:
825829
"""
826830
Get the compilation args for `filename` for the compilation database found in
827831
`compilation_db_dir`
@@ -857,8 +861,8 @@ def get_compilation_args(filename: str, compilation_database: str = None) -> Lis
857861
def load(
858862
filename: str,
859863
contents: str,
860-
compilation_database: str = None,
861-
compilation_args: Sequence[str] = None,
864+
compilation_database: Optional[str] = None,
865+
compilation_args: Optional[Sequence[str]] = None,
862866
) -> DocumentedObject:
863867
"""
864868
Load a C file into a tree of :class:`DocumentedObject`\'s
@@ -881,12 +885,14 @@ def load(
881885
tu = cindex.TranslationUnit.from_source(
882886
filename,
883887
args=args,
884-
unsaved_files=((filename, contents),),
888+
unsaved_files=[
889+
(filename, contents),
890+
],
885891
options=cindex.TranslationUnit.PARSE_DETAILED_PROCESSING_RECORD,
886892
)
887893
cursor = tu.cursor
888894

889-
root_document = DocumentedFile()
895+
root_document = DocumentedFile(cursor)
890896

891897
# Some nodes show up from header includes as well as compiler defines, so
892898
# skip those. Macro instantiations are the locations where macros are
@@ -903,7 +909,7 @@ def load(
903909
]
904910

905911
# Macro definitions always come first in the child list, but that may not
906-
# be their location in the file, so sort all of the nodes by location
912+
# be their location in the file, so sort all the nodes by location
907913
sorted_nodes = sorted(child_nodes, key=lambda x: x.extent.start.offset)
908914

909915
comment_nodes(cursor, sorted_nodes)

src/sphinx_c_autodoc/napoleon/__init__.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,17 @@ def __init__(
3333
if not hasattr(self, "_sections"):
3434
self._sections = self.get_default_sections()
3535

36-
super().__init__(docstring, config, app, what, name, obj, options)
36+
# The GoogleDocstring is using the implied optional `config: Config = None`
37+
# and mypy complains that these shouldn't be optional
38+
super().__init__(
39+
docstring,
40+
config, # type: ignore
41+
app, # type: ignore
42+
what,
43+
name,
44+
obj,
45+
options,
46+
)
3747

3848
def get_default_sections(self) -> Dict[str, Callable]:
3949
"""

src/sphinx_c_autodoc/viewcode/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ def doctree_read(app: Sphinx, doctree: Node) -> None:
391391

392392

393393
def _add_pending_source_cross_reference(
394-
app: Sphinx, signode: Node, fullname: str
394+
app: Sphinx, signode: addnodes.desc, fullname: str
395395
) -> None:
396396
"""
397397
Adds a pending source cross reference to the signature in the doctree, `signode`.
@@ -441,7 +441,9 @@ def _add_pending_source_cross_reference(
441441
signode += html_node
442442

443443

444-
def _add_pending_back_reference(app: Sphinx, signode: Node, fullname: str) -> None:
444+
def _add_pending_back_reference(
445+
app: Sphinx, signode: addnodes.desc, fullname: str
446+
) -> None:
445447
"""
446448
Updates the ``doc_links`` entry of the modules stored in
447449
``_viewcode_c_modules`` so that they can later be added to the source

tox.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ mintoxversion = 3.4
1212
# e.g.: tox -e py37-cov
1313
# Note: the sphinx versions are spelled out so tox doesn't try to resolve them
1414
# as python versions
15-
envlist = py{37, 38, 39, 310, 311}{,-cov}, check-{pylint,pycodestyle,black}, docs, sphinx-{three, four},
15+
envlist = py{37, 38, 39, 310, 311}{,-cov}, check-{pylint,pycodestyle,black,mypy}, docs, sphinx-{three, four},
1616

1717
[testenv]
1818
deps =
@@ -23,7 +23,7 @@ commands =
2323
pycodestyle: pycodestyle {toxinidir}/src/sphinx_c_autodoc
2424
flake8: flake8 {toxinidir}/src/sphinx_c_autodoc
2525
black: black --check {toxinidir}/src/sphinx_c_autodoc {toxinidir}/tests {posargs}
26-
mypy: mypy {toxinidir}/src/sphinx_c_autodoc {posargs}
26+
mypy: poetry run mypy {toxinidir}/src/sphinx_c_autodoc {posargs}
2727
cov: poetry run pytest --cov=sphinx_c_autodoc --cov-config={[coverage]file} --cov-report=xml --cov-fail-under=100 {posargs}
2828
docs: python -m sphinx {toxinidir}/docs {toxinidir}/docs/_build
2929

0 commit comments

Comments
 (0)