Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ exclude: >
repos:

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
rev: v6.0.0
hooks:
- id: check-json
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.7
rev: v0.16.0
hooks:
- id: ruff
args: ["--fix", "--show-fixes"]
- id: ruff-format

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.7.1
rev: v2.3.0
hooks:
- id: mypy
5 changes: 3 additions & 2 deletions sphinx_external_toc/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

import dataclasses as dc
import re
from re import Pattern
import sys
from typing import Any, Callable, Pattern, Type
from typing import Any, Callable, Type

from docutils.nodes import Element

Expand Down Expand Up @@ -93,7 +94,7 @@ def matches_re(regex: str | Pattern, flags: int = 0) -> ValidatorType:
if fullmatch:
match_func = pattern.fullmatch
else: # Python 2 fullmatch emulation (https://bugs.python.org/issue16203)
pattern = re.compile(r"(?:{})\Z".format(pattern.pattern), pattern.flags)
pattern = re.compile(rf"(?:{pattern.pattern})\Z", pattern.flags)
match_func = pattern.match

def _validator(inst, attr, value):
Expand Down
4 changes: 2 additions & 2 deletions sphinx_external_toc/api.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Defines the `SiteMap` object, for storing the parsed ToC."""

from collections.abc import MutableMapping
from collections.abc import Iterator, MutableMapping
from dataclasses import asdict, dataclass
from typing import Any, Dict, Iterator, List, Optional, Set, Union
from typing import Any, Dict, List, Optional, Set, Union

from ._compat import (
DC_SLOTS,
Expand Down
3 changes: 2 additions & 1 deletion sphinx_external_toc/collectors.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import gc
import copy
import gc

from docutils import nodes
from sphinx import addnodes as sphinxnodes
from sphinx.environment.collectors.toctree import TocTreeCollector
Expand Down
4 changes: 2 additions & 2 deletions sphinx_external_toc/parsing.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""Parse the ToC to a `SiteMap` object."""

from collections.abc import Mapping
from collections.abc import Mapping, Sequence
from dataclasses import dataclass, fields
from pathlib import Path
from typing import Any, Dict, List, Optional, Sequence, Set, Tuple, Union
from typing import Any, Dict, List, Optional, Set, Tuple, Union

import yaml

Expand Down
15 changes: 8 additions & 7 deletions sphinx_external_toc/tools.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import re
import shutil
from collections.abc import Mapping, Sequence
from fnmatch import fnmatch
from itertools import chain
from pathlib import Path, PurePosixPath
from typing import Any, Dict, List, Mapping, Optional, Sequence, Tuple, Union
import re
import shutil
from typing import Any, Dict, List, Optional, Tuple, Union

import yaml

Expand Down Expand Up @@ -61,11 +62,11 @@ def create_site_from_toc(
for docname in chain(site_map, additional_files):
# create document
filename = docname
if not any(docname.endswith(ext) for ext in {".rst", ".md"}):
if not any(docname.endswith(ext) for ext in (".rst", ".md")):
filename += default_ext
docpath = root_path.joinpath(PurePosixPath(filename))
if docpath.exists() and not overwrite:
raise IOError(f"Path already exists: {docpath}")
raise OSError(f"Path already exists: {docpath}")
docpath.parent.mkdir(parents=True, exist_ok=True)

content = []
Expand Down Expand Up @@ -116,7 +117,7 @@ def create_site_map_from_path(
root_path, suffixes, default_index, ignore_matches
)
if not root_index:
raise IOError(f"path does not contain a root file: {root_path}")
raise OSError(f"path does not contain a root file: {root_path}")

# create root item and child folders
root_item, indexed_folders = _doc_item_from_path(
Expand Down Expand Up @@ -232,7 +233,7 @@ def _assess_folder(
:returns: (index file name, other file names, folders)
"""
if not folder.is_dir():
raise IOError(f"path must be a directory: {folder}")
raise OSError(f"path must be a directory: {folder}")

def _strip_suffix(name: str) -> str:
for suffix in suffixes:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from pathlib import Path
from typing import List

import pytest
from click.testing import CliRunner
import pytest

from sphinx_external_toc import __version__
from sphinx_external_toc.cli import (
Expand Down
18 changes: 10 additions & 8 deletions tests/test_collectors.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import pytest
from unittest.mock import Mock, patch

import pytest
from sphinx.environment.collectors.toctree import TocTreeCollector

from sphinx_external_toc.collectors import (
TocTreeCollectorWithStyles,
disable_builtin_toctree_collector,
)
from sphinx.environment.collectors.toctree import TocTreeCollector


class TestDisableBuiltinToctreeCollector:
Expand Down Expand Up @@ -343,9 +345,9 @@ def test_to_roman_comprehensive(self, collector):
]
for num, expected in test_cases:
result = collector._TocTreeCollectorWithStyles__to_roman(num)
assert (
result == expected
), f"Failed for {num}: got {result}, expected {expected}"
assert result == expected, (
f"Failed for {num}: got {result}, expected {expected}"
)

def test_to_alpha_comprehensive(self, collector):
"""Test alphabetical conversion comprehensively."""
Expand All @@ -364,9 +366,9 @@ def test_to_alpha_comprehensive(self, collector):
]
for num, expected in test_cases:
result = collector._TocTreeCollectorWithStyles__to_alpha(num)
assert (
result == expected
), f"Failed for {num}: got {result}, expected {expected}"
assert result == expected, (
f"Failed for {num}: got {result}, expected {expected}"
)

def test_disable_builtin_multiple_collectors(self):
"""Test disabling with multiple collectors in memory."""
Expand Down
3 changes: 2 additions & 1 deletion tests/test_compat.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Tests for sphinx_external_toc._compat module."""

import pytest

from sphinx_external_toc import _compat


Expand Down Expand Up @@ -248,8 +249,8 @@ def test_compat_module_dict(self):

def test_compat_import_error_handling(self):
"""Test that import errors are handled gracefully."""
import sys
import importlib
import sys

if "sphinx_external_toc._compat" in sys.modules:
del sys.modules["sphinx_external_toc._compat"]
Expand Down
Loading