Skip to content

Commit 5da2d3a

Browse files
committed
Use mypy
1 parent 88ec01c commit 5da2d3a

File tree

12 files changed

+90
-40
lines changed

12 files changed

+90
-40
lines changed

.github/workflows/mypy.yml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Lint with MyPy
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
mypy:
7+
timeout-minutes: 10
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v3
11+
12+
- name: Set up Python
13+
uses: actions/setup-python@v4
14+
with:
15+
python-version: 3.11
16+
17+
- name: Install dependencies
18+
run: |
19+
python -m pip install -c ci-constraints.txt . -r mypy-requirements.txt
20+
21+
- name: Run MyPy
22+
run: |
23+
mypy .

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,17 @@ This will run all tests. This includes
3535
- Unit tests from `tests/`.
3636
- Doctests from `mkdocstrings_vba/`.
3737
- Full builds from `examples/`.
38+
39+
## Linting
40+
41+
Fix code style using `black`:
42+
43+
```shell
44+
black .
45+
```
46+
47+
Check types using `mypy`:
48+
49+
```shell
50+
mypy .
51+
```

mkdocstrings_handlers/vba/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""This package implements a handler for the VBA language."""
22

3-
from mkdocstrings_handlers.vba._handler import get_handler
3+
from ._handler import get_handler
44

55
__all__ = ["get_handler"]

mkdocstrings_handlers/vba/_crossref.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ def do_multi_crossref(text: str, code: bool = True) -> Markup:
3434
group_number = 0
3535
variables = {}
3636

37-
def repl(match): # noqa: WPS430
38-
nonlocal group_number # noqa: WPS420
37+
def repl(match: re.Match[str]) -> str:
38+
nonlocal group_number
3939
group_number += 1
4040
path = match.group()
4141
path_var = f"path{group_number}"

mkdocstrings_handlers/vba/_handler.py

+22-15
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,21 @@
44

55
from __future__ import annotations
66

7+
import copy
78
import posixpath
89
from collections import ChainMap
910
from pathlib import Path
10-
from typing import Any, BinaryIO, Iterator, Optional, Tuple
11+
from typing import (
12+
Any,
13+
BinaryIO,
14+
Iterator,
15+
Optional,
16+
Tuple,
17+
MutableMapping,
18+
Dict,
19+
Mapping,
20+
Set,
21+
)
1122

1223
from griffe.logger import patch_loggers
1324
from markdown import Markdown
@@ -53,7 +64,7 @@ def __init__(self, *, base_dir: Path, **kwargs: Any) -> None:
5364
The theme to fall back to.
5465
"""
5566

56-
default_config: dict = {
67+
default_config = {
5768
"show_root_heading": False,
5869
"show_root_toc_entry": True,
5970
"show_root_full_path": True,
@@ -126,12 +137,10 @@ def load_inventory(
126137
def render(
127138
self,
128139
data: VbaModuleInfo,
129-
config: dict,
140+
config: Mapping[str, Any],
130141
) -> str:
131-
final_config = ChainMap(config, self.default_config)
132-
render_type = "module"
133-
134-
template = self.env.get_template(f"{render_type}.html")
142+
final_config = ChainMap(dict(copy.deepcopy(config)), self.default_config)
143+
template = self.env.get_template(f"module.html")
135144

136145
# Heading level is a "state" variable, that will change at each step
137146
# of the rendering recursion. Therefore, it's easier to use it as a plain value
@@ -148,18 +157,16 @@ def render(
148157
return template.render(
149158
**{
150159
"config": final_config,
151-
render_type: data,
160+
"module": data,
152161
"heading_level": heading_level,
153162
"root": True,
154163
},
155164
)
156165

157-
def get_anchors(self, data: VbaModuleInfo) -> list[str]:
158-
return list(
159-
{data.path.as_posix(), *(p.signature.name for p in data.procedures)}
160-
)
166+
def get_anchors(self, data: VbaModuleInfo) -> Set[str]:
167+
return {data.path.as_posix(), *(p.signature.name for p in data.procedures)}
161168

162-
def update_env(self, md: Markdown, config: dict) -> None:
169+
def update_env(self, md: Markdown, config: Dict[Any, Any]) -> None:
163170
super().update_env(md, config)
164171
self.env.trim_blocks = True
165172
self.env.lstrip_blocks = True
@@ -171,7 +178,7 @@ def update_env(self, md: Markdown, config: dict) -> None:
171178
def collect(
172179
self,
173180
identifier: str,
174-
config: dict,
181+
config: MutableMapping[str, Any],
175182
) -> VbaModuleInfo:
176183
"""Collect the documentation tree given an identifier and selection options.
177184
@@ -222,7 +229,7 @@ def get_handler(
222229
An instance of `VbaHandler`.
223230
"""
224231
return VbaHandler(
225-
base_dir=Path(config_file_path).parent,
232+
base_dir=Path(config_file_path or ".").parent,
226233
handler="vba",
227234
theme=theme,
228235
custom_templates=custom_templates,

mkdocstrings_handlers/vba/_types.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
@dataclass
99
class VbaArgumentInfo:
10-
1110
name: str
1211

1312
optional: bool
@@ -18,7 +17,7 @@ class VbaArgumentInfo:
1817

1918
default: Optional[str]
2019

21-
def render(self):
20+
def render(self) -> str:
2221
parts = []
2322
if self.optional:
2423
parts.append("Optional")
@@ -43,7 +42,6 @@ class VbaSignatureInfo:
4342

4443
@dataclass
4544
class VbaProcedureInfo:
46-
4745
signature: VbaSignatureInfo
4846

4947
docstring: Optional[Docstring]
@@ -67,7 +65,6 @@ def has_docstrings(self) -> bool:
6765

6866
@dataclass
6967
class VbaModuleInfo:
70-
7168
docstring: Optional[Docstring]
7269

7370
source: List[str]

mkdocstrings_handlers/vba/_util.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
from griffe.dataclasses import Docstring, Function, Parameters, Parameter
55
from griffe.docstrings import Parser
66

7-
from mkdocstrings_handlers.vba._regex import re_signature, re_arg
8-
from mkdocstrings_handlers.vba._types import (
7+
from ._regex import re_signature, re_arg
8+
from ._types import (
99
VbaArgumentInfo,
1010
VbaSignatureInfo,
1111
VbaProcedureInfo,
@@ -99,14 +99,14 @@ def parse_arg(arg: str) -> VbaArgumentInfo:
9999

100100
if match is None:
101101
raise RuntimeError(f"Failed to parse argument: {arg}")
102-
match = match.groupdict()
102+
groups = match.groupdict()
103103

104104
return VbaArgumentInfo(
105-
optional=bool(match["optional"]),
106-
modifier=match["modifier"],
107-
name=match["name"],
108-
arg_type=match["type"],
109-
default=match["default"],
105+
optional=bool(groups["optional"]),
106+
modifier=groups["modifier"],
107+
name=groups["name"],
108+
arg_type=groups["type"],
109+
default=groups["default"],
110110
)
111111

112112

@@ -120,14 +120,14 @@ def parse_signature(line: str) -> VbaSignatureInfo:
120120

121121
if match is None:
122122
raise RuntimeError(f"Failed to parse signature: {line}")
123-
match = match.groupdict()
123+
groups = match.groupdict()
124124

125125
return VbaSignatureInfo(
126-
visibility=match["visibility"],
127-
return_type=match["returnType"],
128-
procedure_type=match["type"],
129-
name=match["name"],
130-
args=list(parse_args(match["args"] or "")),
126+
visibility=groups["visibility"],
127+
return_type=groups["returnType"],
128+
procedure_type=groups["type"],
129+
name=groups["name"],
130+
args=list(parse_args(groups["args"] or "")),
131131
)
132132

133133

mypy-requirements.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
mypy==1.5.1
2+
types-setuptools
3+
types-Markdown

mypy.ini

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[mypy]
2+
strict = true
3+
exclude = build|venv
4+
5+
# Because not all dependencies maintain `__all__`.
6+
implicit_reexport = true

test/test_examples.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def tmp_build(config_file_path: Path) -> Generator[Path, None, None]:
2929

3030

3131
class TestExamples(unittest.TestCase):
32-
def test_example1(self):
32+
def test_example1(self) -> None:
3333
with tmp_build(examples_dir.joinpath("example1", "mkdocs.yml")) as tmp_dir:
3434
# TODO: Write assertions. For now, just check that it does not fail.
3535
pass

test/util/test_parse_args.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99

1010
class TestParseArg(unittest.TestCase):
11-
def test_1(self):
11+
def test_1(self) -> None:
1212
cases = [
1313
(
1414
"bar As listObject",
@@ -58,7 +58,7 @@ def test_1(self):
5858

5959

6060
class TestParseArgs(unittest.TestCase):
61-
def test_1(self):
61+
def test_1(self) -> None:
6262
cases = [
6363
("", []),
6464
(

test/util/test_parse_signature.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99

1010
class TestParseSignature(unittest.TestCase):
11-
def test_1(self):
11+
def test_1(self) -> None:
1212
cases = [
1313
(
1414
"Sub foo()",

0 commit comments

Comments
 (0)