Skip to content

Commit

Permalink
Adopt Ruff and use stricter MyPy settings
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner committed Jul 28, 2024
1 parent dfd5da0 commit ca445c4
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 34 deletions.
4 changes: 0 additions & 4 deletions .flake8

This file was deleted.

4 changes: 3 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
env: [flake8, mypy]
env:
- ruff
- mypy

steps:
- uses: actions/checkout@v3
Expand Down
53 changes: 53 additions & 0 deletions .ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
target-version = "py39" # Pin Ruff to Python 3.9
output-format = "full"
line-length = 95

[lint]
preview = true
select = [
# "ANN", # flake8-annotations
"C4", # flake8-comprehensions
"COM", # flake8-commas
"B", # flake8-bugbear
"DTZ", # flake8-datetimez
"E", # pycodestyle
"EM", # flake8-errmsg
"EXE", # flake8-executable
"F", # pyflakes
"FA", # flake8-future-annotations
"FLY", # flynt
"FURB", # refurb
"G", # flake8-logging-format
"I", # isort
"ICN", # flake8-import-conventions
"INT", # flake8-gettext
"LOG", # flake8-logging
"PERF", # perflint
"PGH", # pygrep-hooks
"PIE", # flake8-pie
"PT", # flake8-pytest-style
"SIM", # flake8-simplify
"SLOT", # flake8-slots
"TCH", # flake8-type-checking
"UP", # pyupgrade
"W", # pycodestyle
"YTT", # flake8-2020
]
ignore = [
"E116",
"E241",
"E251",
]

[lint.per-file-ignores]
"tests/*" = [
"ANN", # tests don't need annotations
]

[lint.isort]
forced-separate = [
"tests",
]
required-imports = [
"from __future__ import annotations",
]
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ clean-mypyfiles:

.PHONY: style-check
style-check:
@flake8
@ruff check

.PHONY: type-check
type-check:
Expand Down
36 changes: 33 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ test = [
"pytest",
]
lint = [
"flake8",
"ruff==0.5.5",
"mypy",
"docutils-stubs",
"types-docutils",
]
standalone = [
"Sphinx>=5",
Expand All @@ -72,4 +72,34 @@ include = [
]

[tool.mypy]
ignore_missing_imports = true
python_version = "3.9"
packages = [
"sphinxcontrib",
"tests",
]
exclude = [
"tests/roots",
]
check_untyped_defs = true
disallow_any_generics = true
disallow_incomplete_defs = true
disallow_subclassing_any = true
disallow_untyped_calls = true
disallow_untyped_decorators = true
disallow_untyped_defs = true
explicit_package_bases = true
extra_checks = true
no_implicit_reexport = true
show_column_numbers = true
show_error_context = true
strict_optional = true
warn_redundant_casts = true
warn_unused_configs = true
warn_unused_ignores = true
enable_error_code = [
"type-arg",
"redundant-self",
"truthy-iterable",
"ignore-without-code",
"unused-awaitable",
]
20 changes: 9 additions & 11 deletions sphinxcontrib/devhelp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,21 @@
import os
import re
from os import path
from typing import Any
from typing import TYPE_CHECKING, Any

from docutils import nodes
from sphinx import addnodes
from sphinx.application import Sphinx
from sphinx.builders.html import StandaloneHTMLBuilder
from sphinx.environment.adapters.indexentries import IndexEntries
from sphinx.locale import get_translation
from sphinx.util import logging
from sphinx.util.nodes import NodeMatcher
from sphinx.util.osutil import make_filename

try:
import xml.etree.ElementTree as etree
except ImportError:
import lxml.etree as etree # type: ignore
if TYPE_CHECKING:
from sphinx.application import Sphinx

import xml.etree.ElementTree as etree

__version__ = '1.0.6'
__version_info__ = (1, 0, 6)
Expand Down Expand Up @@ -80,8 +79,7 @@ def build_devhelp(self, outdir: str | os.PathLike[str], outname: str) -> None:
self.config.master_doc, self, prune_toctrees=False)

def write_toc(node: nodes.Node, parent: etree.Element) -> None:
if isinstance(node, addnodes.compact_paragraph) or \
isinstance(node, nodes.bullet_list):
if isinstance(node, (addnodes.compact_paragraph, nodes.bullet_list)):
for subnode in node:
write_toc(subnode, parent)
elif isinstance(node, nodes.list_item):
Expand All @@ -93,7 +91,7 @@ def write_toc(node: nodes.Node, parent: etree.Element) -> None:
parent.attrib['name'] = node.astext()

matcher = NodeMatcher(addnodes.compact_paragraph, toctree=Any)
for node in tocdoc.findall(matcher): # type: addnodes.compact_paragraph
for node in tocdoc.findall(matcher):
write_toc(node, chapters)

# Index
Expand All @@ -115,7 +113,7 @@ def write_index(title: str, refs: list[Any], subitems: Any) -> None:
if subitems:
parent_title = re.sub(r'\s*\(.*\)\s*$', '', title)
for subitem in subitems:
write_index("%s %s" % (parent_title, subitem[0]),
write_index(f'{parent_title} {subitem[0]}',
subitem[1], [])

for (_group_key, group) in index:
Expand All @@ -125,7 +123,7 @@ def write_index(title: str, refs: list[Any], subitems: Any) -> None:
# Dump the XML file
xmlfile = path.join(outdir, outname + '.devhelp.gz')
with gzip.GzipFile(filename=xmlfile, mode='w', mtime=0) as f:
tree.write(f, 'utf-8') # type: ignore
tree.write(f, 'utf-8')


def setup(app: Sphinx) -> dict[str, Any]:
Expand Down
15 changes: 6 additions & 9 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
from __future__ import annotations

from pathlib import Path

import pytest

import sphinx

pytest_plugins = 'sphinx.testing.fixtures'
pytest_plugins = (
'sphinx.testing.fixtures',
)


@pytest.fixture(scope='session')
def rootdir():
if sphinx.version_info[:2] < (7, 2):
from sphinx.testing.path import path

return path(__file__).parent.abspath() / 'roots'

def rootdir() -> Path:
return Path(__file__).resolve().parent / 'roots'
11 changes: 9 additions & 2 deletions tests/test_devhelp.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
"""Test for devhelp extension."""

from __future__ import annotations

from time import sleep
from typing import TYPE_CHECKING

import pytest

if TYPE_CHECKING:
from sphinx.application import Sphinx


@pytest.mark.sphinx('devhelp', testroot='basic')
def test_basic(app, status, warning):
def test_basic(app: Sphinx) -> None:
app.builder.build_all()


@pytest.mark.sphinx('devhelp', testroot='basic', freshenv=True)
def test_basic_deterministic_build(app):
def test_basic_deterministic_build(app: Sphinx) -> None:
app.config.devhelp_basename, output_filename = 'testing', 'testing.devhelp.gz'

app.builder.build_all()
Expand Down
6 changes: 3 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
minversion = 2.4.0
envlist =
py{39,310,311,312,313},
flake8,
ruff,
mypy
isolated_build = True

Expand All @@ -17,14 +17,14 @@ setenv =
commands=
pytest --durations 25 {posargs}

[testenv:flake8]
[testenv:ruff]
description =
Run style checks.
extras =
test
lint
commands=
flake8
ruff check

[testenv:mypy]
description =
Expand Down

0 comments on commit ca445c4

Please sign in to comment.