Skip to content

Commit 3adda36

Browse files
authored
Merge pull request #127 from rtts/mypy
Type annotations
2 parents 7fbf0ef + b3c52dd commit 3adda36

10 files changed

Lines changed: 210 additions & 113 deletions

File tree

.github/workflows/main.yml

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,22 @@ name: CI
33
on:
44
push:
55
branches:
6-
- main
6+
- main
77
pull_request:
88

99
jobs:
10-
tests:
11-
name: Python ${{ matrix.python-version }}
12-
runs-on: ubuntu-24.04
10+
pre-commit:
11+
name: Run pre-commit hooks
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@main
15+
- uses: actions/setup-python@main
16+
- run: python -m pip install --upgrade pre-commit
17+
- run: pre-commit run -a
1318

19+
tests:
20+
name: Run tests using Python ${{ matrix.python-version }}
21+
runs-on: ubuntu-latest
1422
strategy:
1523
matrix:
1624
python-version:
@@ -19,18 +27,10 @@ jobs:
1927
- '3.11'
2028
- '3.12'
2129
- '3.13'
22-
2330
steps:
24-
- uses: actions/checkout@v2
25-
26-
- uses: actions/setup-python@v2
27-
with:
28-
python-version: ${{ matrix.python-version }}
29-
30-
- name: Install dependencies
31-
run: |
32-
python -m pip install --upgrade pip setuptools wheel
33-
python -m pip install --upgrade nox
34-
35-
- name: Run tox targets for ${{ matrix.python-version }}
36-
run: nox --session tests-${{ matrix.python-version }}
31+
- uses: actions/checkout@main
32+
- uses: actions/setup-python@main
33+
with:
34+
python-version: ${{ matrix.python-version }}
35+
- run: python -m pip install --upgrade nox
36+
- run: nox --session tests-${{ matrix.python-version }}

.pre-commit-config.yaml

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
repos:
2-
- repo: https://github.com/pycqa/isort
3-
rev: 5.12.0
4-
hooks:
5-
- id: isort
6-
- repo: https://github.com/psf/black
7-
rev: 23.1.0
8-
hooks:
9-
- id: black
10-
- repo: https://github.com/pycqa/flake8
11-
rev: 6.0.0
12-
hooks:
13-
- id: flake8
2+
- repo: https://github.com/pycqa/isort
3+
rev: 6.0.1
4+
hooks:
5+
- id: isort
6+
- repo: https://github.com/psf/black
7+
rev: 25.1.0
8+
hooks:
9+
- id: black
10+
- repo: https://github.com/pycqa/flake8
11+
rev: 7.3.0
12+
hooks:
13+
- id: flake8
14+
- repo: https://github.com/pre-commit/mirrors-mypy
15+
rev: v1.17.1
16+
hooks:
17+
- id: mypy
18+
exclude: ^noxfile\.py$

djhtml/__main__.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@
1515
1616
"""
1717

18+
from __future__ import annotations
19+
1820
import sys
21+
from collections.abc import Iterator
1922
from pathlib import Path
2023

21-
from . import modes, options
24+
from . import modes
25+
from .options import options
2226

2327

24-
def main():
28+
def main() -> None:
2529
changed_files = 0
2630
unchanged_files = 0
2731
problematic_files = 0
@@ -54,7 +58,7 @@ def main():
5458
source = input_file.read()
5559
except Exception as e:
5660
problematic_files += 1
57-
_error(e)
61+
_error(str(e))
5862
continue
5963

6064
# Guess tabwidth
@@ -70,8 +74,9 @@ def main():
7074
guess = probabilities.index(max(probabilities))
7175

7276
# Indent input file
77+
extra_blocks = dict(options.extra_block or ())
7378
try:
74-
result = Mode(source, extra_blocks=options.extra_block).indent(
79+
result = Mode(source, extra_blocks=extra_blocks).indent(
7580
options.tabwidth or guess or 4
7681
)
7782
except modes.MaxLineLengthExceeded:
@@ -103,7 +108,7 @@ def main():
103108
except Exception as e:
104109
changed_files -= 1
105110
problematic_files += 1
106-
_error(e)
111+
_error(str(e))
107112
continue
108113
_info(f"reindented {output_file.name}")
109114
elif changed and filename != "-":
@@ -134,7 +139,7 @@ def main():
134139
sys.exit(0)
135140

136141

137-
def _generate_filenames(paths, suffixes):
142+
def _generate_filenames(paths: list[str], suffixes: list[str]) -> Iterator[str]:
138143
for filename in paths:
139144
if filename == "-":
140145
yield filename
@@ -143,18 +148,20 @@ def _generate_filenames(paths, suffixes):
143148
if path.is_dir():
144149
yield from _generate_filenames_from_directory(path, suffixes)
145150
else:
146-
yield path
151+
yield str(path)
147152

148153

149-
def _generate_filenames_from_directory(directory, suffixes):
154+
def _generate_filenames_from_directory(
155+
directory: Path, suffixes: list[str]
156+
) -> Iterator[str]:
150157
for path in directory.iterdir():
151158
if path.is_file() and path.suffix in suffixes:
152-
yield path
159+
yield str(path)
153160
elif path.is_dir():
154161
yield from _generate_filenames_from_directory(path, suffixes)
155162

156163

157-
def _verify_changed(source, result):
164+
def _verify_changed(source: str, result: str) -> bool:
158165
output_lines = result.split("\n")
159166
changed = False
160167
for line_nr, line in enumerate(source.split("\n")):
@@ -165,7 +172,7 @@ def _verify_changed(source, result):
165172
return changed
166173

167174

168-
def _get_depth(line):
175+
def _get_depth(line: str) -> int:
169176
count = 0
170177
for char in line:
171178
if char == " ":
@@ -177,11 +184,11 @@ def _get_depth(line):
177184
return count
178185

179186

180-
def _info(msg):
187+
def _info(msg: str) -> None:
181188
print(msg, file=sys.stderr)
182189

183190

184-
def _error(msg):
191+
def _error(msg: str) -> None:
185192
_info(f"Error: {msg}")
186193

187194

djhtml/lines.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
4+
5+
if TYPE_CHECKING:
6+
from .tokens import Token
7+
8+
19
class Line:
210
"""
311
A single output line not including the final newline.
412
513
"""
614

7-
def __init__(self, tokens=None, level=0, offset=0, ignore=False):
15+
def __init__(
16+
self,
17+
tokens: list[Token.BaseToken] | None = None,
18+
level: int = 0,
19+
offset: int = 0,
20+
ignore: bool = False,
21+
) -> None:
822
"""
923
Lines are currently never instantiated with arguments, but
1024
that doesn't mean they can't.
@@ -15,15 +29,15 @@ def __init__(self, tokens=None, level=0, offset=0, ignore=False):
1529
self.offset = offset
1630
self.ignore = ignore
1731

18-
def append(self, token):
32+
def append(self, token: Token.BaseToken) -> None:
1933
"""
2034
Append token to line.
2135
2236
"""
2337
self.tokens.append(token)
2438

2539
@property
26-
def text(self):
40+
def text(self) -> str:
2741
"""
2842
The text of this line including the original
2943
leading/trailing spaces.
@@ -32,7 +46,7 @@ def text(self):
3246
return "".join([token.text for token in self.tokens])
3347

3448
@property
35-
def indents(self):
49+
def indents(self) -> bool:
3650
"""
3751
Whether this line has more opening than closing tokens.
3852
@@ -41,7 +55,7 @@ def indents(self):
4155
[token for token in self.tokens if token.dedents]
4256
)
4357

44-
def indent(self, tabwidth):
58+
def indent(self, tabwidth: int) -> str:
4559
"""
4660
The final, indented text of this line.
4761
@@ -52,7 +66,7 @@ def indent(self, tabwidth):
5266
return " " * (tabwidth * self.level + self.offset) + text
5367
return ""
5468

55-
def __len__(self):
69+
def __len__(self) -> int:
5670
"""
5771
The length of the line (so far), excluding the whitespace
5872
at the beginning. Be careful calling len() because it might
@@ -62,7 +76,7 @@ def __len__(self):
6276
"""
6377
return len(self.text.lstrip())
6478

65-
def __repr__(self):
79+
def __repr__(self) -> str:
6680
kwargs = ""
6781
for attr in ["level", "offset", "ignore"]:
6882
if value := getattr(self, attr):

0 commit comments

Comments
 (0)