Skip to content

Commit 5a44e3c

Browse files
committed
refactor!(ruff): Run all automated fixes as of ruff 0.3.4
ruff check --select ALL . --fix --unsafe-fixes --preview --show-fixes; ruff format . Fixed 45 errors: - docs/conf.py: 1 × RET505 (superfluous-else-return) - src/django_docutils/_internal/types.py: 1 × TCH003 (typing-only-standard-library-import) 1 × I001 (unsorted-imports) - src/django_docutils/exc.py: 1 × PIE790 (unnecessary-placeholder) 1 × E303 (too-many-blank-lines) - src/django_docutils/lib/directives/code.py: 1 × RUF025 (unnecessary-dict-comprehension-for-iterable) 1 × RUF021 (parenthesize-chained-operators) - src/django_docutils/lib/directives/registry.py: 1 × TID252 (relative-imports) - src/django_docutils/lib/metadata/process.py: 1 × TID252 (relative-imports) - src/django_docutils/lib/metadata/tests/test_extract.py: 4 × TID252 (relative-imports) 1 × I001 (unsorted-imports) - src/django_docutils/lib/metadata/tests/test_process.py: 2 × TID252 (relative-imports) - src/django_docutils/lib/publisher.py: 1 × RET505 (superfluous-else-return) - src/django_docutils/lib/roles/common.py: 1 × PLR5501 (collapsible-else-if) 1 × TID252 (relative-imports) - src/django_docutils/lib/roles/registry.py: 3 × RET501 (unnecessary-return-none) 1 × TID252 (relative-imports) - src/django_docutils/lib/roles/types.py: 1 × W293 (blank-line-with-whitespace) - src/django_docutils/lib/tests/test_utils.py: 2 × TID252 (relative-imports) - src/django_docutils/lib/tests/test_writers.py: 3 × TID252 (relative-imports) - src/django_docutils/lib/transforms/code.py: 1 × PIE810 (multiple-starts-ends-with) - src/django_docutils/lib/transforms/toc.py: 1 × PLR6201 (literal-membership) 1 × RET505 (superfluous-else-return) - src/django_docutils/lib/utils.py: 1 × W391 (too-many-newlines-at-end-of-file) 1 × PLR1711 (useless-return) - src/django_docutils/lib/views.py: 1 × I001 (unsorted-imports) 1 × TID252 (relative-imports) 1 × RET505 (superfluous-else-return) - src/django_docutils/template.py: 1 × PLW0120 (useless-else-on-loop) - src/django_docutils/templatetags/django_docutils.py: 1 × TID252 (relative-imports) 1 × RSE102 (unnecessary-paren-on-raise-exception) - src/django_docutils/views.py: 1 × RET506 (superfluous-else-raise) 1 × RSE102 (unnecessary-paren-on-raise-exception) 1 × RET504 (unnecessary-assign) 1 × ANN204 (missing-return-type-special-method) - tests/test_docutils_roles.py: 1 × ISC001 (single-line-implicit-string-concatenation)
1 parent b98b873 commit 5a44e3c

File tree

22 files changed

+53
-63
lines changed

22 files changed

+53
-63
lines changed

docs/conf.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -206,15 +206,14 @@ def linkcode_resolve(domain: str, info: t.Dict[str, str]) -> t.Union[None, str]:
206206
fn,
207207
linespec,
208208
)
209-
else:
210-
return "{}/blob/v{}/{}/{}/{}{}".format(
211-
about["__github__"],
212-
about["__version__"],
213-
"src",
214-
about["__package_name__"],
215-
fn,
216-
linespec,
217-
)
209+
return "{}/blob/v{}/{}/{}/{}{}".format(
210+
about["__github__"],
211+
about["__version__"],
212+
"src",
213+
about["__package_name__"],
214+
fn,
215+
linespec,
216+
)
218217

219218

220219
def remove_tabs_js(app: "Sphinx", exc: Exception) -> None:

src/django_docutils/_internal/types.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
.. _typeshed's: https://github.com/python/typeshed/blob/5df8de7/stdlib/_typeshed/__init__.pyi#L115-L118
88
""" # E501
99

10-
from os import PathLike
1110
from typing import TYPE_CHECKING, Union
1211

1312
if TYPE_CHECKING:
13+
from os import PathLike
14+
1415
from typing_extensions import TypeAlias
1516

1617
StrPath: "TypeAlias" = Union[str, "PathLike[str]"] # stable

src/django_docutils/exc.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
class DjangoDocutilsException(Exception):
55
"""Base exception for Django Docutils package."""
66

7-
pass
8-
97

108
class DocutilsNotInstalled(DjangoDocutilsException):
119
"""Docutils is not installed."""

src/django_docutils/lib/directives/code.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ def patch_bash_session_lexer() -> None:
7575
# 'linenos': HtmlFormatter(noclasses=INLINESTYLES, linenos=True),
7676
}
7777

78-
DEFAULT_OPTION_SPEC: t.Dict[str, t.Callable[[str], t.Any]] = {
79-
key: directives.flag for key in VARIANTS
80-
}
78+
DEFAULT_OPTION_SPEC: t.Dict[str, t.Callable[[str], t.Any]] = dict.fromkeys(
79+
VARIANTS, directives.flag
80+
)
8181

8282

8383
class CodeBlock(Directive):
@@ -100,7 +100,7 @@ def run(self) -> t.List[nodes.Node]:
100100
# no lexer found - use the text one instead of an exception
101101
lexer = TextLexer()
102102
# take an arbitrary option if more than one is given
103-
formatter = self.options and VARIANTS[next(iter(self.options))] or DEFAULT
103+
formatter = (self.options and VARIANTS[next(iter(self.options))]) or DEFAULT
104104
parsed = highlight("\n".join(self.content), lexer, formatter)
105105
return [nodes.raw("", parsed, format="html")]
106106

src/django_docutils/lib/directives/registry.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from django.utils.module_loading import import_string
44
from docutils.parsers.rst import directives
55

6-
from ..settings import DJANGO_DOCUTILS_LIB_RST
6+
from django_docutils.lib.settings import DJANGO_DOCUTILS_LIB_RST
77

88

99
def register_django_docutils_directives() -> None:

src/django_docutils/lib/metadata/process.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def process_datetime(metadata):
3434

3535
from django.utils.module_loading import import_string
3636

37-
from ..settings import DJANGO_DOCUTILS_LIB_RST
37+
from django_docutils.lib.settings import DJANGO_DOCUTILS_LIB_RST
3838

3939

4040
def process_metadata(metadata: t.Dict[str, str]) -> t.Dict[str, str]:

src/django_docutils/lib/metadata/tests/test_extract.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
from django.utils.encoding import force_bytes
66
from docutils.core import publish_doctree
77

8-
from ...settings import DJANGO_DOCUTILS_LIB_RST
9-
from ..extract import (
8+
from django_docutils.lib.metadata.extract import (
109
extract_metadata,
1110
extract_subtitle,
1211
extract_title,
1312
)
13+
from django_docutils.lib.settings import DJANGO_DOCUTILS_LIB_RST
1414

1515

1616
def test_extract_title() -> None:

src/django_docutils/lib/metadata/tests/test_process.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
from django.utils.encoding import force_bytes
66
from docutils.core import publish_doctree
77

8-
from ..extract import extract_metadata
9-
from ..process import process_metadata
8+
from django_docutils.lib.metadata.extract import extract_metadata
9+
from django_docutils.lib.metadata.process import process_metadata
1010

1111

1212
def test_process_metadata_file() -> None:

src/django_docutils/lib/publisher.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,5 +182,4 @@ def publish_html_from_doctree(
182182

183183
if show_title:
184184
return mark_safe(force_str(parts["html_body"]))
185-
else:
186-
return mark_safe(force_str(parts["fragment"]))
185+
return mark_safe(force_str(parts["fragment"]))

src/django_docutils/lib/roles/common.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from docutils import nodes, utils
66

7-
from ..utils import split_explicit_title
7+
from django_docutils.lib.utils import split_explicit_title
88

99
if t.TYPE_CHECKING:
1010
from .types import RemoteUrlHandlerFn, RoleFnReturnValue, UrlHandlerFn
@@ -58,15 +58,14 @@ def url_handler(target):
5858

5959
if not has_explicit_title:
6060
title = utils.unescape(title)
61-
else:
62-
if title[:2] == "**" and title[-2:] == "**":
63-
innernodeclass = nodes.strong
64-
title = title.strip("**") # noqa: B005
65-
# In Python 3.9+
66-
# title = title.removeprefix("**").removesuffix("**")
67-
elif title[0] == "*" and title[-1] == "*":
68-
innernodeclass = nodes.emphasis
69-
title = title.strip("*")
61+
elif title[:2] == "**" and title[-2:] == "**":
62+
innernodeclass = nodes.strong
63+
title = title.strip("**") # noqa: B005
64+
# In Python 3.9+
65+
# title = title.removeprefix("**").removesuffix("**")
66+
elif title[0] == "*" and title[-1] == "*":
67+
innernodeclass = nodes.emphasis
68+
title = title.strip("*")
7069

7170
url = url_handler_fn(target)
7271

src/django_docutils/lib/roles/registry.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from django.utils.module_loading import import_string
77
from docutils.parsers.rst import roles
88

9-
from ..settings import DJANGO_DOCUTILS_LIB_RST
9+
from django_docutils.lib.settings import DJANGO_DOCUTILS_LIB_RST
1010

1111

1212
def register_django_docutils_roles() -> None:
@@ -62,10 +62,10 @@ def register_django_docutils_roles() -> None:
6262
None
6363
"""
6464
if not DJANGO_DOCUTILS_LIB_RST:
65-
return None
65+
return
6666

6767
if "roles" not in DJANGO_DOCUTILS_LIB_RST:
68-
return None
68+
return
6969

7070
django_docutils_roles = DJANGO_DOCUTILS_LIB_RST["roles"]
7171

@@ -74,7 +74,7 @@ def register_django_docutils_roles() -> None:
7474
if local_roles:
7575
register_role_mapping(local_roles)
7676

77-
return None
77+
return
7878

7979

8080
def register_role_mapping(role_mapping: t.Dict[str, t.Any]) -> None:

src/django_docutils/lib/roles/types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def __call__(self, target: str) -> t.Tuple[str, str]:
3232
<https://docutils.sourceforge.io/docs/howto/rst-roles.html>`_:
3333
3434
Role functions return a tuple of two values:
35-
35+
3636
- A list of nodes which will be inserted into the document tree at the point where
3737
the interpreted role was encountered (can be an empty list).
3838
- A list of system messages, which will be inserted into the document tree

src/django_docutils/lib/tests/test_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Tests for Django Docutil utilities."""
22

3-
from ..utils import chop_after_docinfo, chop_after_title
3+
from django_docutils.lib.utils import chop_after_docinfo, chop_after_title
44

55

66
def test_chop_after_title() -> None:

src/django_docutils/lib/tests/test_writers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
from docutils.core import publish_doctree
55
from docutils.writers.html5_polyglot import Writer
66

7-
from ..publisher import publish_parts_from_doctree
8-
from ..settings import DJANGO_DOCUTILS_LIB_RST
9-
from ..writers import DjangoDocutilsWriter
7+
from django_docutils.lib.publisher import publish_parts_from_doctree
8+
from django_docutils.lib.settings import DJANGO_DOCUTILS_LIB_RST
9+
from django_docutils.lib.writers import DjangoDocutilsWriter
1010

1111

1212
def test_HTMLWriter_hides_docinfo() -> None:

src/django_docutils/lib/transforms/code.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def apply(self, **kwargs: t.Any) -> None:
112112
patch_bash_session_lexer()
113113

114114
newlexer = BashSessionLexer()
115-
elif text.startswith("{%") or text.startswith("{{"):
115+
elif text.startswith(("{%", "{{")):
116116
from pygments.lexers.templates import DjangoLexer
117117

118118
newlexer = DjangoLexer()

src/django_docutils/lib/transforms/toc.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def build_contents(
5454
ref_id = self.document.set_id(reference)
5555
item = nodes.list_item("", reference)
5656
if (
57-
self.backlinks in ("entry", "top")
57+
self.backlinks in {"entry", "top"}
5858
and title.next_node(nodes.reference) is None
5959
and isinstance(title, (nodes.Element, nodes.TextElement))
6060
):
@@ -72,5 +72,4 @@ def build_contents(
7272
if auto:
7373
contents["classes"].append("auto-toc")
7474
return contents
75-
else:
76-
return []
75+
return []

src/django_docutils/lib/utils.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,5 +125,3 @@ def append_html_to_node(node: nodes.Element, ad_code: str) -> None:
125125

126126
node.append(html_node)
127127
node.replace_self(node)
128-
129-
return None

src/django_docutils/lib/views.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
from django.views.generic.base import ContextMixin, TemplateView
99
from docutils import nodes
1010

11-
from .._internal.types import StrPath
11+
from django_docutils._internal.types import StrPath
12+
1213
from .publisher import (
1314
publish_doctree,
1415
publish_html_from_doctree,
@@ -85,8 +86,7 @@ def get_base_template(self) -> str:
8586
"""TODO: move this out of RSTMixin, it is AMP related, not RST."""
8687
if self.request.GET.get("is_amp", False):
8788
return "django_docutils/base-amp.html"
88-
else:
89-
return "base.html"
89+
return "base.html"
9090

9191

9292
class RSTRawView(TemplateTitleView):

src/django_docutils/template.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ def get_template(self, template_name: str) -> "_EngineTemplate":
4545
continue
4646

4747
return DocutilsTemplate(template_code, self.options)
48-
else:
49-
raise TemplateDoesNotExist(template_name)
48+
raise TemplateDoesNotExist(template_name)
5049

5150

5251
class DocutilsTemplate:

src/django_docutils/templatetags/django_docutils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from django.utils.encoding import force_str
1010
from django.utils.safestring import SafeString, mark_safe
1111

12-
from ..lib.publisher import publish_html_from_source
12+
from django_docutils.lib.publisher import publish_html_from_source
1313

1414
register = template.Library()
1515

@@ -123,7 +123,7 @@ def rst(parser: Parser, token: Token) -> ReStructuredTextNode:
123123
for bit in bits:
124124
match = kwarg_re.match(bit)
125125
if not match:
126-
raise MalformedArgumentsToRSTTag()
126+
raise MalformedArgumentsToRSTTag
127127

128128
name, value = match.groups()
129129
if name:

src/django_docutils/views.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def __init__(
2525
status: t.Optional[int] = None,
2626
charset: t.Optional[str] = None,
2727
using: t.Optional[str] = None,
28-
):
28+
) -> None:
2929
self.rst_name = rst
3030
super().__init__(
3131
request,
@@ -50,8 +50,7 @@ def rendered_content(self) -> str:
5050
)
5151

5252
template = self.resolve_template(self.template_name)
53-
content = template.render(context)
54-
return content
53+
return template.render(context)
5554

5655

5756
class DocutilsViewRstNameImproperlyConfigured(ImproperlyConfigured):
@@ -95,6 +94,5 @@ def render_to_response(
9594
def get_rst_names(self) -> t.List[str]:
9695
"""Follows after get_template_names, but for scanning for rst content."""
9796
if self.rst_name is None:
98-
raise DocutilsViewRstNameImproperlyConfigured()
99-
else:
100-
return [self.rst_name]
97+
raise DocutilsViewRstNameImproperlyConfigured
98+
return [self.rst_name]

tests/test_docutils_roles.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def test_register_django_docutils_roles(monkeypatch: pytest.MonkeyPatch) -> None
5252
from django_docutils.lib.roles import registry as roles_registry_pkg
5353

5454
assert roles_registry_pkg.DJANGO_DOCUTILS_LIB_RST, ( # type:ignore[attr-defined]
55-
"Sanity-check, something truthy should be set." ""
55+
"Sanity-check, something truthy should be set."
5656
)
5757
register_django_docutils_roles()
5858

0 commit comments

Comments
 (0)