Skip to content

Commit

Permalink
chore(ruff) Automated fixes for typing annotations
Browse files Browse the repository at this point in the history
Fixed 215 errors:
- docs/conf.py:
     2 × UP007 (non-pep604-annotation)
     2 × UP037 (quoted-annotation)
     1 × I002 (missing-required-import)
     1 × I001 (unsorted-imports)
- src/django_docutils/__about__.py:
     1 × I002 (missing-required-import)
- src/django_docutils/_internal/types.py:
     2 × UP037 (quoted-annotation)
     1 × I002 (missing-required-import)
- src/django_docutils/exc.py:
     1 × I002 (missing-required-import)
- src/django_docutils/lib/directives/code.py:
     1 × TC003 (typing-only-standard-library-import)
     1 × I002 (missing-required-import)
     1 × UP037 (quoted-annotation)
     1 × I001 (unsorted-imports)
- src/django_docutils/lib/directives/registry.py:
     1 × I002 (missing-required-import)
- src/django_docutils/lib/metadata/extract.py:
     2 × UP007 (non-pep604-annotation)
     1 × F401 (unused-import)
     1 × I002 (missing-required-import)
     1 × I001 (unsorted-imports)
- src/django_docutils/lib/metadata/process.py:
     1 × I002 (missing-required-import)
- src/django_docutils/lib/metadata/processors.py:
     1 × I002 (missing-required-import)
- src/django_docutils/lib/metadata/tests/test_extract.py:
     1 × I001 (unsorted-imports)
     1 × TC003 (typing-only-standard-library-import)
     1 × I002 (missing-required-import)
- src/django_docutils/lib/metadata/tests/test_process.py:
     1 × I002 (missing-required-import)
- src/django_docutils/lib/publisher.py:
    11 × UP007 (non-pep604-annotation)
     2 × UP037 (quoted-annotation)
     1 × I002 (missing-required-import)
     1 × TC002 (typing-only-third-party-import)
     1 × I001 (unsorted-imports)
- src/django_docutils/lib/roles/common.py:
     3 × UP037 (quoted-annotation)
     2 × UP007 (non-pep604-annotation)
     1 × I002 (missing-required-import)
- src/django_docutils/lib/roles/email.py:
     2 × UP007 (non-pep604-annotation)
     2 × I001 (unsorted-imports)
     1 × TC001 (typing-only-first-party-import)
     1 × I002 (missing-required-import)
     1 × TC002 (typing-only-third-party-import)
- src/django_docutils/lib/roles/file.py:
     6 × UP007 (non-pep604-annotation)
     1 × TC002 (typing-only-third-party-import)
     1 × I002 (missing-required-import)
- src/django_docutils/lib/roles/github.py:
     2 × UP007 (non-pep604-annotation)
     2 × I001 (unsorted-imports)
     1 × I002 (missing-required-import)
     1 × TC001 (typing-only-first-party-import)
     1 × TC002 (typing-only-third-party-import)
- src/django_docutils/lib/roles/hackernews.py:
     2 × UP007 (non-pep604-annotation)
     2 × I001 (unsorted-imports)
     1 × I002 (missing-required-import)
     1 × TC001 (typing-only-first-party-import)
     1 × TC002 (typing-only-third-party-import)
- src/django_docutils/lib/roles/kbd.py:
     2 × UP007 (non-pep604-annotation)
     1 × TC002 (typing-only-third-party-import)
     1 × I002 (missing-required-import)
- src/django_docutils/lib/roles/leanpub.py:
     2 × UP007 (non-pep604-annotation)
     2 × I001 (unsorted-imports)
     1 × I002 (missing-required-import)
     1 × TC001 (typing-only-first-party-import)
     1 × TC002 (typing-only-third-party-import)
- src/django_docutils/lib/roles/pypi.py:
     2 × UP007 (non-pep604-annotation)
     2 × I001 (unsorted-imports)
     1 × I002 (missing-required-import)
     1 × TC001 (typing-only-first-party-import)
     1 × TC002 (typing-only-third-party-import)
- src/django_docutils/lib/roles/readthedocs.py:
     2 × UP007 (non-pep604-annotation)
     2 × I001 (unsorted-imports)
     1 × TC001 (typing-only-first-party-import)
     1 × I002 (missing-required-import)
     1 × TC002 (typing-only-third-party-import)
- src/django_docutils/lib/roles/registry.py:
     1 × I002 (missing-required-import)
- src/django_docutils/lib/roles/twitter.py:
     2 × UP007 (non-pep604-annotation)
     2 × I001 (unsorted-imports)
     1 × I002 (missing-required-import)
     1 × TC001 (typing-only-first-party-import)
     1 × TC002 (typing-only-third-party-import)
- src/django_docutils/lib/roles/types.py:
     1 × I002 (missing-required-import)
- src/django_docutils/lib/roles/url.py:
     2 × UP007 (non-pep604-annotation)
     2 × I001 (unsorted-imports)
     1 × I002 (missing-required-import)
     1 × TC001 (typing-only-first-party-import)
     1 × TC002 (typing-only-third-party-import)
- src/django_docutils/lib/roles/wikipedia.py:
     2 × UP007 (non-pep604-annotation)
     2 × I001 (unsorted-imports)
     1 × TC001 (typing-only-first-party-import)
     1 × I002 (missing-required-import)
     1 × TC002 (typing-only-third-party-import)
- src/django_docutils/lib/settings.py:
     1 × UP007 (non-pep604-annotation)
     1 × I002 (missing-required-import)
- src/django_docutils/lib/tests/test_utils.py:
     1 × I002 (missing-required-import)
- src/django_docutils/lib/tests/test_writers.py:
     1 × I002 (missing-required-import)
- src/django_docutils/lib/text.py:
     1 × I002 (missing-required-import)
- src/django_docutils/lib/transforms/code.py:
     8 × UP037 (quoted-annotation)
     1 × UP007 (non-pep604-annotation)
     1 × I002 (missing-required-import)
- src/django_docutils/lib/transforms/toc.py:
     2 × UP007 (non-pep604-annotation)
     1 × I002 (missing-required-import)
- src/django_docutils/lib/types.py:
     1 × I002 (missing-required-import)
- src/django_docutils/lib/utils.py:
     1 × TC003 (typing-only-standard-library-import)
     1 × I002 (missing-required-import)
     1 × UP037 (quoted-annotation)
     1 × I001 (unsorted-imports)
- src/django_docutils/lib/views.py:
     7 × UP007 (non-pep604-annotation)
     2 × I001 (unsorted-imports)
     2 × TC002 (typing-only-third-party-import)
     1 × I002 (missing-required-import)
     1 × TC001 (typing-only-first-party-import)
- src/django_docutils/lib/writers.py:
     4 × UP007 (non-pep604-annotation)
     1 × TC002 (typing-only-third-party-import)
     1 × I002 (missing-required-import)
- src/django_docutils/template.py:
     4 × UP037 (quoted-annotation)
     2 × UP007 (non-pep604-annotation)
     1 × I002 (missing-required-import)
     1 × TC002 (typing-only-third-party-import)
- src/django_docutils/templatetags/django_docutils.py:
     7 × UP007 (non-pep604-annotation)
     1 × F401 (unused-import)
     1 × I002 (missing-required-import)
     1 × I001 (unsorted-imports)
- src/django_docutils/views.py:
    11 × UP007 (non-pep604-annotation)
     2 × TC002 (typing-only-third-party-import)
     1 × I002 (missing-required-import)
     1 × I001 (unsorted-imports)
- tests/constants.py:
     1 × I002 (missing-required-import)
- tests/settings.py:
     1 × I002 (missing-required-import)
     1 × UP037 (quoted-annotation)
- tests/test_docutils_roles.py:
     2 × TC002 (typing-only-third-party-import)
     1 × UP007 (non-pep604-annotation)
     1 × I002 (missing-required-import)
     1 × TC003 (typing-only-standard-library-import)
     1 × I001 (unsorted-imports)
- tests/test_template.py:
     1 × TC003 (typing-only-standard-library-import)
     1 × I002 (missing-required-import)
     1 × UP037 (quoted-annotation)
     1 × I001 (unsorted-imports)
- tests/test_templatetag.py:
     1 × I002 (missing-required-import)

Found 567 errors (215 fixed, 352 remaining).
43 files reformatted, 12 files left unchanged

uv run ruff check --select ALL . --fix --unsafe-fixes --preview --show-fixes; uv run ruff format .
  • Loading branch information
tony committed Jan 4, 2025
1 parent e1e5759 commit 255ab1d
Show file tree
Hide file tree
Showing 44 changed files with 267 additions and 144 deletions.
10 changes: 6 additions & 4 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Sphinx configuration for Django Docutils."""

# flake8: noqa: E501
from __future__ import annotations

import inspect
import os
import pathlib
Expand Down Expand Up @@ -77,7 +79,7 @@
html_extra_path = ["manifest.json"]
html_theme = "furo"
html_theme_path: list[str] = []
html_theme_options: dict[str, t.Union[str, list[dict[str, str]]]] = {
html_theme_options: dict[str, str | list[dict[str, str]]] = {
"light_logo": "img/icons/logo.svg",
"dark_logo": "img/icons/logo-dark.svg",
"footer_icons": [
Expand Down Expand Up @@ -147,7 +149,7 @@
}


def linkcode_resolve(domain: str, info: dict[str, str]) -> t.Union[None, str]:
def linkcode_resolve(domain: str, info: dict[str, str]) -> None | str:
"""
Determine the URL corresponding to Python object.
Expand Down Expand Up @@ -217,13 +219,13 @@ def linkcode_resolve(domain: str, info: dict[str, str]) -> t.Union[None, str]:
)


def remove_tabs_js(app: "Sphinx", exc: Exception) -> None:
def remove_tabs_js(app: Sphinx, exc: Exception) -> None:
"""Fix for sphinx-inline-tabs#18."""
if app.builder.format == "html" and not exc:
tabs_js = pathlib.Path(app.builder.outdir) / "_static" / "tabs.js"
tabs_js.unlink(missing_ok=True)


def setup(app: "Sphinx") -> None:
def setup(app: Sphinx) -> None:
"""Sphinx setup hook."""
app.connect("build-finished", remove_tabs_js)
2 changes: 2 additions & 0 deletions src/django_docutils/__about__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Metadata package for Django Docutils."""

from __future__ import annotations

__title__ = "django-docutils"
__package_name__ = "django_docutils"
__description__ = "Docutils (a.k.a. reStructuredText, reST, RST) support for django."
Expand Down
6 changes: 4 additions & 2 deletions src/django_docutils/_internal/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@
.. _typeshed's: https://github.com/python/typeshed/blob/5df8de7/stdlib/_typeshed/__init__.pyi#L115-L118
""" # E501

from __future__ import annotations

from typing import TYPE_CHECKING, Union

if TYPE_CHECKING:
from os import PathLike

from typing_extensions import TypeAlias

StrPath: "TypeAlias" = Union[str, "PathLike[str]"] # stable
StrPath: TypeAlias = Union[str, "PathLike[str]"] # stable
""":class:`os.PathLike` or :class:`str`"""

StrOrBytesPath: "TypeAlias" = Union[
StrOrBytesPath: TypeAlias = Union[
str,
bytes,
"PathLike[str]",
Expand Down
2 changes: 2 additions & 0 deletions src/django_docutils/exc.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Exceptions for Django Docutils."""

from __future__ import annotations


class DjangoDocutilsException(Exception):
"""Base exception for Django Docutils package."""
Expand Down
7 changes: 5 additions & 2 deletions src/django_docutils/lib/directives/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@
:license: BSD, see LICENSE for details.
"""

from __future__ import annotations

import re
import typing as t
from collections.abc import Callable

from docutils import nodes
from docutils.parsers.rst import Directive, directives
Expand All @@ -48,6 +49,8 @@
from pygments.lexers.special import TextLexer

if t.TYPE_CHECKING:
from collections.abc import Callable

from pygments.formatter import Formatter


Expand All @@ -72,7 +75,7 @@ def patch_bash_session_lexer() -> None:
DEFAULT = HtmlFormatter(cssclass="highlight code-block", noclasses=INLINESTYLES)

#: Add name -> formatter pairs for every variant you want to use
VARIANTS: dict[str, "Formatter[str]"] = {
VARIANTS: dict[str, Formatter[str]] = {
# 'linenos': HtmlFormatter(noclasses=INLINESTYLES, linenos=True),
}

Expand Down
2 changes: 2 additions & 0 deletions src/django_docutils/lib/directives/registry.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Register douctils directives for django-docutils."""

from __future__ import annotations

from django.utils.module_loading import import_string
from docutils.parsers.rst import directives

Expand Down
6 changes: 3 additions & 3 deletions src/django_docutils/lib/metadata/extract.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"""Title, Subtitle, and Metadata extraction of reStructuredText."""

import typing as t
from __future__ import annotations

from django.template.defaultfilters import truncatewords
from django.utils.html import strip_tags
from docutils import nodes


def extract_title(document: nodes.document) -> t.Optional[str]:
def extract_title(document: nodes.document) -> str | None:
"""Return the title of the document.
Parameters
Expand Down Expand Up @@ -77,7 +77,7 @@ def extract_metadata(document: nodes.document) -> dict[str, str]:
return output


def extract_subtitle(document: nodes.document) -> t.Optional[str]:
def extract_subtitle(document: nodes.document) -> str | None:
"""Return the subtitle of the document."""
for node in document.traverse(nodes.PreBibliographic): # type:ignore
if isinstance(node, nodes.subtitle):
Expand Down
2 changes: 2 additions & 0 deletions src/django_docutils/lib/metadata/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def process_datetime(metadata):
See *processors.py* for more examples.
"""

from __future__ import annotations

from django.utils.module_loading import import_string

from django_docutils.lib.settings import DJANGO_DOCUTILS_LIB_RST
Expand Down
2 changes: 2 additions & 0 deletions src/django_docutils/lib/metadata/processors.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Metadata processing for Django Docutils."""

from __future__ import annotations

import datetime
import typing as t

Expand Down
7 changes: 6 additions & 1 deletion src/django_docutils/lib/metadata/tests/test_extract.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Test metadata and title extraction from reStructuredText."""

import pathlib
from __future__ import annotations

from typing import TYPE_CHECKING

from django.utils.encoding import force_bytes
from docutils.core import publish_doctree
Expand All @@ -12,6 +14,9 @@
)
from django_docutils.lib.settings import DJANGO_DOCUTILS_LIB_RST

if TYPE_CHECKING:
import pathlib


def test_extract_title() -> None:
"""Assert title extraction from reStructuredText."""
Expand Down
2 changes: 2 additions & 0 deletions src/django_docutils/lib/metadata/tests/test_process.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Test docutils metadata (docinfo) processing in Django Docutils."""

from __future__ import annotations

import datetime

from django.utils.encoding import force_bytes
Expand Down
28 changes: 15 additions & 13 deletions src/django_docutils/lib/publisher.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
"""Docutils Publisher fors for Django Docutils."""

from __future__ import annotations

import typing as t

from django.utils.encoding import force_bytes, force_str
from django.utils.safestring import mark_safe
from docutils import io, nodes
from docutils.core import Publisher, publish_doctree as docutils_publish_doctree
from docutils.readers.doctree import Reader
from docutils.writers.html5_polyglot import Writer

from .directives.registry import register_django_docutils_directives
from .roles.registry import register_django_docutils_roles
Expand All @@ -17,19 +18,20 @@

if t.TYPE_CHECKING:
from docutils import SettingsSpec
from docutils.writers.html5_polyglot import Writer

docutils_settings = DJANGO_DOCUTILS_LIB_RST.get("docutils", {})


def publish_parts_from_doctree(
document: nodes.document,
destination_path: t.Optional[str] = None,
writer: t.Optional[Writer] = None,
destination_path: str | None = None,
writer: Writer | None = None,
writer_name: str = "pseudoxml",
settings: t.Optional[t.Any] = None,
settings_spec: "t.Optional[SettingsSpec]" = None,
settings_overrides: t.Optional[t.Any] = None,
config_section: t.Optional[str] = None,
settings: t.Any | None = None,
settings_spec: SettingsSpec | None = None,
settings_overrides: t.Any | None = None,
config_section: str | None = None,
enable_exit_status: bool = False,
) -> dict[str, str]:
"""Render docutils doctree into docutils parts."""
Expand All @@ -56,8 +58,8 @@ def publish_parts_from_doctree(

def publish_toc_from_doctree(
doctree: nodes.document,
writer: t.Optional[Writer] = None,
) -> t.Optional[str]:
writer: Writer | None = None,
) -> str | None:
"""Publish table of contents from docutils doctree."""
if not writer:
writer = DjangoDocutilsWriter()
Expand Down Expand Up @@ -98,7 +100,7 @@ def publish_toc_from_doctree(


def publish_doctree(
source: t.Union[str, bytes],
source: str | bytes,
settings_overrides: t.Any = docutils_settings,
) -> nodes.document:
"""Split off ability to get doctree (a.k.a. document).
Expand Down Expand Up @@ -139,8 +141,8 @@ class PublishHtmlDocTreeKwargs(TypedDict):

def publish_html_from_source(
source: str,
**kwargs: "Unpack[PublishHtmlDocTreeKwargs]",
) -> t.Optional[str]:
**kwargs: Unpack[PublishHtmlDocTreeKwargs],
) -> str | None:
"""Return HTML from reStructuredText source string."""
doctree = publish_doctree(source)
return publish_html_from_doctree(doctree, **kwargs)
Expand All @@ -150,7 +152,7 @@ def publish_html_from_doctree(
doctree: nodes.document,
show_title: bool = True,
toc_only: bool = False,
) -> t.Optional[str]:
) -> str | None:
"""Return HTML from reStructuredText document (doctree).
Parameters
Expand Down
12 changes: 7 additions & 5 deletions src/django_docutils/lib/roles/common.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Core parts for Django Docutils roles."""

from __future__ import annotations

import typing as t

from docutils import nodes, utils
Expand All @@ -13,9 +15,9 @@
def generic_url_role(
name: str,
text: str,
url_handler_fn: "UrlHandlerFn",
innernodeclass: type[t.Union[nodes.Text, nodes.TextElement]] = nodes.Text,
) -> "RoleFnReturnValue":
url_handler_fn: UrlHandlerFn,
innernodeclass: type[nodes.Text | nodes.TextElement] = nodes.Text,
) -> RoleFnReturnValue:
"""Docutils Role for Django Docutils.
This generic role also handles explicit titles (``:role:`yata yata <target>```)
Expand Down Expand Up @@ -78,8 +80,8 @@ def url_handler(target):
def generic_remote_url_role(
name: str,
text: str,
url_handler_fn: "RemoteUrlHandlerFn",
innernodeclass: type[t.Union[nodes.Text, nodes.TextElement]] = nodes.Text,
url_handler_fn: RemoteUrlHandlerFn,
innernodeclass: type[nodes.Text | nodes.TextElement] = nodes.Text,
) -> tuple[list[nodes.reference], list[t.Any]]:
"""Docutils Role that can call an external data source for title and URL.
Expand Down
14 changes: 9 additions & 5 deletions src/django_docutils/lib/roles/email.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
"""Email role for docutils."""

import typing as t
from __future__ import annotations

from docutils.parsers.rst.states import Inliner
import typing as t

from .common import generic_url_role
from .types import RoleFnReturnValue

if t.TYPE_CHECKING:
from docutils.parsers.rst.states import Inliner

from .types import RoleFnReturnValue


def email_role(
Expand All @@ -14,8 +18,8 @@ def email_role(
text: str,
lineno: int,
inliner: Inliner,
options: t.Optional[dict[str, t.Any]] = None,
content: t.Optional[str] = None,
options: dict[str, t.Any] | None = None,
content: str | None = None,
) -> RoleFnReturnValue:
"""Role for linking to email articles.
Expand Down
18 changes: 11 additions & 7 deletions src/django_docutils/lib/roles/file.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
"""File role for Docutils."""

from __future__ import annotations

import os
import typing as t

from docutils import nodes, utils
from docutils.parsers.rst.states import Inliner

if t.TYPE_CHECKING:
from docutils.parsers.rst.states import Inliner


def file_role(
Expand All @@ -13,8 +17,8 @@ def file_role(
text: str,
lineno: int,
inliner: Inliner,
options: t.Optional[dict[str, t.Any]] = None,
content: t.Optional[str] = None,
options: dict[str, t.Any] | None = None,
content: str | None = None,
) -> tuple[list[nodes.emphasis], list[t.Any]]:
"""Role for files.
Expand Down Expand Up @@ -66,8 +70,8 @@ def manifest_role(
text: str,
lineno: int,
inliner: Inliner,
options: t.Optional[dict[str, t.Any]] = None,
content: t.Optional[str] = None,
options: dict[str, t.Any] | None = None,
content: str | None = None,
) -> tuple[list[nodes.emphasis], list[t.Any]]:
"""Role for manifests (package.json, file outputs).
Expand Down Expand Up @@ -103,8 +107,8 @@ def exe_role(
text: str,
lineno: int,
inliner: Inliner,
options: t.Optional[dict[str, t.Any]] = None,
content: t.Optional[str] = None,
options: dict[str, t.Any] | None = None,
content: str | None = None,
) -> tuple[list[nodes.emphasis], list[t.Any]]:
"""Role for executables.
Expand Down
Loading

0 comments on commit 255ab1d

Please sign in to comment.