Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
4b54532
Enable strict typing
JaapJoris Jul 22, 2025
836d079
First batch of type fixes
JaapJoris Jul 22, 2025
7d9b58e
Second batch of type fixes
JaapJoris Jul 23, 2025
027f732
Third batch of type fixes
JaapJoris Jul 24, 2025
9fdf70b
Fix Python 3.9 compatibility
JaapJoris Jul 24, 2025
ba7ec3b
Final batch of type fixes
JaapJoris Jul 26, 2025
042e5c5
Add mypy pre-commit hook
JaapJoris Jul 26, 2025
b813040
Get rid of redundant `options.pyi`
JaapJoris Jul 28, 2025
79a366b
Stop using `self` as the namespace for command-line arguments
JaapJoris Aug 10, 2025
a98222f
Add typing to test suite
JaapJoris Aug 10, 2025
85ae8b6
Run `pre-commit run -a`
JaapJoris Aug 10, 2025
175684b
Unpin dependencies in pipeline to catch breakages early, and add `pre…
JaapJoris Aug 10, 2025
851acff
Re-add `@rev` to workflow actions
JaapJoris Aug 10, 2025
3965037
Prevent workflow jobs from running twice
JaapJoris Aug 10, 2025
ae31e87
Upgrade pre-commit hooks
JaapJoris Aug 10, 2025
a17d1e4
Improve typing of class variables
JaapJoris Aug 10, 2025
6965313
Improve typing off places that use `OffsetDict`
JaapJoris Aug 10, 2025
79400bc
Rename `_Base` to `BaseToken`
JaapJoris Aug 10, 2025
6f9390c
Write out all arguments to `__init__()` explicitly
JaapJoris Aug 10, 2025
0401c49
Convert `extra_blocks` to `dict[str, str]` at an earlier stage
JaapJoris Aug 10, 2025
903f4f5
Remove obsolute self variable and its associated comment
JaapJoris Aug 10, 2025
0c801f9
Unquote types
JaapJoris Aug 12, 2025
272186d
Move type of `inside_attr` to class-level
JaapJoris Aug 12, 2025
b3c52dd
Replace `[]` with `()` for no practical reason
JaapJoris Aug 12, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@ name: CI
on:
push:
branches:
- main
- main
pull_request:

jobs:
tests:
name: Python ${{ matrix.python-version }}
runs-on: ubuntu-24.04
pre-commit:
name: Run pre-commit hooks
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@main
- uses: actions/setup-python@main
- run: python -m pip install --upgrade pre-commit
- run: pre-commit run -a

tests:
name: Run tests using Python ${{ matrix.python-version }}
runs-on: ubuntu-latest
strategy:
matrix:
python-version:
Expand All @@ -19,18 +27,10 @@ jobs:
- '3.11'
- '3.12'
- '3.13'

steps:
- uses: actions/checkout@v2

- uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip setuptools wheel
python -m pip install --upgrade nox

- name: Run tox targets for ${{ matrix.python-version }}
run: nox --session tests-${{ matrix.python-version }}
- uses: actions/checkout@main
- uses: actions/setup-python@main
with:
python-version: ${{ matrix.python-version }}
- run: python -m pip install --upgrade nox
- run: nox --session tests-${{ matrix.python-version }}
29 changes: 17 additions & 12 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
repos:
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
- repo: https://github.com/psf/black
rev: 23.1.0
hooks:
- id: black
- repo: https://github.com/pycqa/flake8
rev: 6.0.0
hooks:
- id: flake8
- repo: https://github.com/pycqa/isort
rev: 6.0.1
hooks:
- id: isort
- repo: https://github.com/psf/black
rev: 25.1.0
hooks:
- id: black
- repo: https://github.com/pycqa/flake8
rev: 7.3.0
hooks:
- id: flake8
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.17.1
hooks:
- id: mypy
exclude: ^noxfile\.py$
33 changes: 20 additions & 13 deletions djhtml/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@

"""

from __future__ import annotations

import sys
from collections.abc import Iterator
from pathlib import Path

from . import modes, options
from . import modes
from .options import options


def main():
def main() -> None:
changed_files = 0
unchanged_files = 0
problematic_files = 0
Expand Down Expand Up @@ -54,7 +58,7 @@ def main():
source = input_file.read()
except Exception as e:
problematic_files += 1
_error(e)
_error(str(e))
continue

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

# Indent input file
extra_blocks = dict(options.extra_block or ())
try:
result = Mode(source, extra_blocks=options.extra_block).indent(
result = Mode(source, extra_blocks=extra_blocks).indent(
options.tabwidth or guess or 4
)
except modes.MaxLineLengthExceeded:
Expand Down Expand Up @@ -103,7 +108,7 @@ def main():
except Exception as e:
changed_files -= 1
problematic_files += 1
_error(e)
_error(str(e))
continue
_info(f"reindented {output_file.name}")
elif changed and filename != "-":
Expand Down Expand Up @@ -134,7 +139,7 @@ def main():
sys.exit(0)


def _generate_filenames(paths, suffixes):
def _generate_filenames(paths: list[str], suffixes: list[str]) -> Iterator[str]:
for filename in paths:
if filename == "-":
yield filename
Expand All @@ -143,18 +148,20 @@ def _generate_filenames(paths, suffixes):
if path.is_dir():
yield from _generate_filenames_from_directory(path, suffixes)
else:
yield path
yield str(path)


def _generate_filenames_from_directory(directory, suffixes):
def _generate_filenames_from_directory(
directory: Path, suffixes: list[str]
) -> Iterator[str]:
for path in directory.iterdir():
if path.is_file() and path.suffix in suffixes:
yield path
yield str(path)
elif path.is_dir():
yield from _generate_filenames_from_directory(path, suffixes)


def _verify_changed(source, result):
def _verify_changed(source: str, result: str) -> bool:
output_lines = result.split("\n")
changed = False
for line_nr, line in enumerate(source.split("\n")):
Expand All @@ -165,7 +172,7 @@ def _verify_changed(source, result):
return changed


def _get_depth(line):
def _get_depth(line: str) -> int:
count = 0
for char in line:
if char == " ":
Expand All @@ -177,11 +184,11 @@ def _get_depth(line):
return count


def _info(msg):
def _info(msg: str) -> None:
print(msg, file=sys.stderr)


def _error(msg):
def _error(msg: str) -> None:
_info(f"Error: {msg}")


Expand Down
28 changes: 21 additions & 7 deletions djhtml/lines.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
from __future__ import annotations

from typing import TYPE_CHECKING

if TYPE_CHECKING:
from .tokens import Token


class Line:
"""
A single output line not including the final newline.

"""

def __init__(self, tokens=None, level=0, offset=0, ignore=False):
def __init__(
self,
tokens: list[Token.BaseToken] | None = None,
level: int = 0,
offset: int = 0,
ignore: bool = False,
) -> None:
"""
Lines are currently never instantiated with arguments, but
that doesn't mean they can't.
Expand All @@ -15,15 +29,15 @@ def __init__(self, tokens=None, level=0, offset=0, ignore=False):
self.offset = offset
self.ignore = ignore

def append(self, token):
def append(self, token: Token.BaseToken) -> None:
"""
Append token to line.

"""
self.tokens.append(token)

@property
def text(self):
def text(self) -> str:
"""
The text of this line including the original
leading/trailing spaces.
Expand All @@ -32,7 +46,7 @@ def text(self):
return "".join([token.text for token in self.tokens])

@property
def indents(self):
def indents(self) -> bool:
"""
Whether this line has more opening than closing tokens.

Expand All @@ -41,7 +55,7 @@ def indents(self):
[token for token in self.tokens if token.dedents]
)

def indent(self, tabwidth):
def indent(self, tabwidth: int) -> str:
"""
The final, indented text of this line.

Expand All @@ -52,7 +66,7 @@ def indent(self, tabwidth):
return " " * (tabwidth * self.level + self.offset) + text
return ""

def __len__(self):
def __len__(self) -> int:
"""
The length of the line (so far), excluding the whitespace
at the beginning. Be careful calling len() because it might
Expand All @@ -62,7 +76,7 @@ def __len__(self):
"""
return len(self.text.lstrip())

def __repr__(self):
def __repr__(self) -> str:
kwargs = ""
for attr in ["level", "offset", "ignore"]:
if value := getattr(self, attr):
Expand Down
Loading
Loading