Skip to content

Commit

Permalink
typing: add support for type checking and type hints
Browse files Browse the repository at this point in the history
Add 'check-typing' make target and github workflow to run mypy on the
source. Add the minimal type hints to make it pass.
  • Loading branch information
jnikula committed Sep 11, 2024
1 parent 4766a17 commit 11b0ba4
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 5 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/makefile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ jobs:
- name: reStructuredText check
run: make check-rst

typing:
name: Check typing
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
- name: Type check
run: |
. venv
make check-typing
examples:
name: Check examples
runs-on: ubuntu-latest
Expand Down
6 changes: 5 additions & 1 deletion Makefile.local
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ RST := $(RST) CHANGELOG.rst CONTRIBUTING.rst README.rst

# Static analysis
.PHONY: check
check: check-style check-rst check-examples
check: check-style check-typing check-rst check-examples

.PHONY: check-style
check-style:
flake8 src/hawkmoth test

.PHONY: check-typing
check-typing:
mypy

.PHONY: check-rst
check-rst:
rst-lint --level warning $(RST)
Expand Down
17 changes: 17 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ dependencies = [
[project.optional-dependencies]
dev = [
"flake8",
"mypy",
"pytest",
"pytest-cov",
"pytest-xdist",
"restructuredtext_lint",
"strictyaml",
"types-docutils",
]

[project.scripts]
Expand Down Expand Up @@ -68,6 +70,21 @@ exclude = [
"**/Makefile*",
]

[tool.mypy]
files = [
"src",
"test",
]
exclude = "(test/conf\\.py|test/update-examples\\.py)"

[[tool.mypy.overrides]]
module = "clang.*"
ignore_missing_imports = true

[[tool.mypy.overrides]]
module = "strictyaml.*"
ignore_missing_imports = true

[tool.pytest.ini_options]
minversion = "6.0"
addopts = "--strict-markers"
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
flake8
mypy
pytest
pytest-cov
pytest-xdist
restructuredtext_lint
sphinx
strictyaml
types-docutils
4 changes: 2 additions & 2 deletions src/hawkmoth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class _AutoBaseDirective(SphinxDirective):
}
has_content = False

_domain = None
_docstring_types = None
_domain: str | None = None
_docstring_types: list[type[docstring.Docstring]] | None = None

def __display_parser_diagnostics(self, errors):
# Map parser diagnostic level to Sphinx level name
Expand Down
4 changes: 2 additions & 2 deletions src/hawkmoth/ext/javadoc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class _block_with_end_command(_handler):
"""Paragraph with a dedicated command to end it.
For example, @code/@endcode."""
_end_command = None
_end_command: str | None = None

def end_command(self):
"""Get the name of the command that ends this paragraph."""
Expand Down Expand Up @@ -135,7 +135,7 @@ def header(self):

class _field_list(_handler):
"""Paragraph which becomes a single field list item."""
_field_name = None
_field_name: str | None = None
_indented_paragraph = True

def field_name(self):
Expand Down

0 comments on commit 11b0ba4

Please sign in to comment.