Skip to content

Commit

Permalink
🔧 Use ruff-format (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjsewell authored May 12, 2024
1 parent 950908b commit 7762458
Show file tree
Hide file tree
Showing 14 changed files with 128 additions and 123 deletions.
1 change: 1 addition & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ jobs:
- name: Upload to Codecov
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
name: mdit-py-plugins-pytests
flags: pytests
file: ./coverage.xml
Expand Down
20 changes: 6 additions & 14 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,22 @@ exclude: >
repos:

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

- repo: https://github.com/PyCQA/isort
rev: 5.13.2
hooks:
- id: isort

- repo: https://github.com/psf/black
rev: 23.12.1
hooks:
- id: black

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.13
rev: v0.4.4
hooks:
- id: ruff
- id: ruff
args: [--fix]
- id: ruff-format

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.8.0
rev: v1.10.0
hooks:
- id: mypy
additional_dependencies: [markdown-it-py~=3.0]
Expand Down
8 changes: 4 additions & 4 deletions mdit_py_plugins/admon/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from contextlib import suppress
import re
from typing import TYPE_CHECKING, Callable, List, Sequence, Tuple
from typing import TYPE_CHECKING, Callable, Sequence

from markdown_it import MarkdownIt
from markdown_it.rules_block import StateBlock
Expand All @@ -17,7 +17,7 @@
from markdown_it.utils import EnvType, OptionsDict


def _get_multiple_tags(params: str) -> Tuple[List[str], str]:
def _get_multiple_tags(params: str) -> tuple[list[str], str]:
"""Check for multiple tags when the title is double quoted."""
re_tags = re.compile(r'^\s*(?P<tokens>[^"]+)\s+"(?P<title>.*)"\S*$')
match = re_tags.match(params)
Expand All @@ -27,7 +27,7 @@ def _get_multiple_tags(params: str) -> Tuple[List[str], str]:
raise ValueError("No match found for parameters")


def _get_tag(_params: str) -> Tuple[List[str], str]:
def _get_tag(_params: str) -> tuple[list[str], str]:
"""Separate the tag name from the admonition title."""
params = _params.strip()
if not params:
Expand Down Expand Up @@ -212,7 +212,7 @@ def renderDefault(
_options: OptionsDict,
env: EnvType,
) -> str:
return self.renderToken(tokens, idx, _options, env) # type: ignore
return self.renderToken(tokens, idx, _options, env) # type: ignore[attr-defined,no-any-return]

render = render or renderDefault

Expand Down
5 changes: 3 additions & 2 deletions mdit_py_plugins/amsmath/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""An extension to capture amsmath latex environments."""

from __future__ import annotations

import re
from typing import TYPE_CHECKING, Callable, Optional, Sequence
from typing import TYPE_CHECKING, Callable, Sequence

from markdown_it import MarkdownIt
from markdown_it.common.utils import escapeHtml
Expand Down Expand Up @@ -57,7 +58,7 @@


def amsmath_plugin(
md: MarkdownIt, *, renderer: Optional[Callable[[str], str]] = None
md: MarkdownIt, *, renderer: Callable[[str], str] | None = None
) -> None:
"""Parses TeX math equations, without any surrounding delimiters,
only for top-level `amsmath <https://ctan.org/pkg/amsmath>`__ environments:
Expand Down
10 changes: 5 additions & 5 deletions mdit_py_plugins/attrs/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def _span_rule(state: StateInline, silent: bool) -> bool:
state.pos = labelStart
state.posMax = labelEnd
token = state.push("span_open", "span", 1)
token.attrs = attrs # type: ignore
token.attrs = attrs # type: ignore[assignment]
state.md.inline.tokenize(state)
token = state.push("span_close", "span", -1)

Expand Down Expand Up @@ -190,7 +190,7 @@ def _attr_block_rule(
return True

token = state.push("attrs_block", "", 0)
token.attrs = attrs # type: ignore
token.attrs = attrs # type: ignore[assignment]
token.map = [startLine, startLine + 1]

state.line = startLine + 1
Expand All @@ -211,9 +211,9 @@ def _attr_resolve_block_rule(state: StateCore) -> None:

# classes are appended
if "class" in state.tokens[i].attrs and "class" in next_token.attrs:
state.tokens[i].attrs[
"class"
] = f"{state.tokens[i].attrs['class']} {next_token.attrs['class']}"
state.tokens[i].attrs["class"] = (
f"{state.tokens[i].attrs['class']} {next_token.attrs['class']}"
)

if next_token.type == "attrs_block":
# subsequent attribute blocks take precedence, when merging
Expand Down
1 change: 1 addition & 0 deletions mdit_py_plugins/attrs/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class <- '.' name
bareval <- (ASCII_ALPHANUM | ':' | '_' | '-')+
quotedval <- '"' ([^"] | '\"') '"'
"""

from __future__ import annotations

from enum import Enum
Expand Down
3 changes: 2 additions & 1 deletion mdit_py_plugins/container/index.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Process block-level custom containers."""

from __future__ import annotations

from math import floor
Expand Down Expand Up @@ -56,7 +57,7 @@ def renderDefault(
if tokens[idx].nesting == 1:
tokens[idx].attrJoin("class", name)

return self.renderToken(tokens, idx, _options, env) # type: ignore
return self.renderToken(tokens, idx, _options, env) # type: ignore[attr-defined,no-any-return]

min_markers = 3
marker_str = marker
Expand Down
1 change: 1 addition & 0 deletions mdit_py_plugins/deflist/index.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Process definition lists."""

from markdown_it import MarkdownIt
from markdown_it.rules_block import StateBlock

Expand Down
14 changes: 7 additions & 7 deletions mdit_py_plugins/dollarmath/index.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import re
from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Sequence
from typing import TYPE_CHECKING, Any, Callable, Sequence

from markdown_it import MarkdownIt
from markdown_it.common.utils import escapeHtml, isWhiteSpace
Expand All @@ -24,9 +24,9 @@ def dollarmath_plugin(
allow_digits: bool = True,
allow_blank_lines: bool = True,
double_inline: bool = False,
label_normalizer: Optional[Callable[[str], str]] = None,
renderer: Optional[Callable[[str, Dict[str, Any]], str]] = None,
label_renderer: Optional[Callable[[str], str]] = None,
label_normalizer: Callable[[str], str] | None = None,
renderer: Callable[[str, dict[str, Any]], str] | None = None,
label_renderer: Callable[[str], str] | None = None,
) -> None:
"""Plugin for parsing dollar enclosed math,
e.g. inline: ``$a=1$``, block: ``$$b=2$$``
Expand All @@ -53,7 +53,7 @@ def dollarmath_plugin(
"""
if label_normalizer is None:
label_normalizer = lambda label: re.sub(r"\s+", "-", label)
label_normalizer = lambda label: re.sub(r"\s+", "-", label) # noqa: E731

md.inline.ruler.before(
"escape",
Expand All @@ -76,7 +76,7 @@ def dollarmath_plugin(

_label_renderer: Callable[[str], str]
if label_renderer is None:
_label_renderer = (
_label_renderer = ( # noqa: E731
lambda label: f'<a href="#{label}" class="mathlabel" title="Permalink to this equation">¶</a>'
)
else:
Expand Down Expand Up @@ -286,7 +286,7 @@ def _math_inline_dollar(state: StateInline, silent: bool) -> bool:

def math_block_dollar(
allow_labels: bool = True,
label_normalizer: Optional[Callable[[str], str]] = None,
label_normalizer: Callable[[str], str] | None = None,
allow_blank_lines: bool = False,
) -> Callable[[StateBlock, int, int, bool], bool]:
"""Generate block dollar rule."""
Expand Down
3 changes: 2 additions & 1 deletion mdit_py_plugins/field_list/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Field list plugin"""

from contextlib import contextmanager
from typing import Iterator, Optional, Tuple

Expand Down Expand Up @@ -176,7 +177,7 @@ def _fieldlist_rule(

has_first_line = contentStart < maximum
if block_indent is None: # no body content
if not has_first_line: # noqa SIM108
if not has_first_line: # noqa: SIM108
# no body or first line, so just use default
block_indent = 2
else:
Expand Down
13 changes: 7 additions & 6 deletions mdit_py_plugins/footnote/index.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Process footnotes"""

from __future__ import annotations

from typing import TYPE_CHECKING, List, Optional, Sequence
from typing import TYPE_CHECKING, Sequence

from markdown_it import MarkdownIt
from markdown_it.helpers import parseLinkLabel
Expand Down Expand Up @@ -184,7 +185,7 @@ def footnote_inline(state: StateInline, silent: bool) -> bool:
refs = state.env.setdefault("footnotes", {}).setdefault("list", {})
footnoteId = len(refs)

tokens: List[Token] = []
tokens: list[Token] = []
state.md.inline.parse(
state.src[labelStart:labelEnd], state.md, state.env, tokens
)
Expand Down Expand Up @@ -270,7 +271,7 @@ def footnote_tail(state: StateCore) -> None:
if "footnotes" not in state.env:
return

current: List[Token] = []
current: list[Token] = []
tok_filter = []
for tok in state.tokens:
if tok.type == "footnote_reference_open":
Expand All @@ -290,7 +291,7 @@ def footnote_tail(state: StateCore) -> None:
if insideRef:
current.append(tok)

tok_filter.append((not insideRef))
tok_filter.append(not insideRef)

state.tokens = [t for t, f in zip(state.tokens, tok_filter) if f]

Expand Down Expand Up @@ -329,7 +330,7 @@ def footnote_tail(state: StateCore) -> None:

state.tokens.extend(tokens)
if state.tokens[len(state.tokens) - 1].type == "paragraph_close":
lastParagraph: Optional[Token] = state.tokens.pop()
lastParagraph: Token | None = state.tokens.pop()
else:
lastParagraph = None

Expand Down Expand Up @@ -482,4 +483,4 @@ def render_footnote_anchor(
ident += ":" + str(tokens[idx].meta["subId"])

# ↩ with escape code to prevent display as Apple Emoji on iOS
return ' <a href="#fnref' + ident + '" class="footnote-backref">\u21a9\uFE0E</a>'
return ' <a href="#fnref' + ident + '" class="footnote-backref">\u21a9\ufe0e</a>'
1 change: 1 addition & 0 deletions mdit_py_plugins/front_matter/index.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Process front matter."""

from markdown_it import MarkdownIt
from markdown_it.rules_block import StateBlock

Expand Down
31 changes: 23 additions & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,30 @@ exclude = [
"tests/",
]

[tool.isort]
profile = "black"
force_sort_within_sections = true
known_first_party = ["mdit_py_plugins", "tests"]
[tool.ruff.lint]
extend-select = [
"B", # flake8-bugbear
"C4", # flake8-comprehensions
"I", # isort
"ICN", # flake8-import-conventions
"ISC", # flake8-implicit-str-concat
"N", # pep8-naming
"PERF", # perflint (performance anti-patterns)
"PGH", # pygrep-hooks
"PIE", # flake8-pie
"PTH", # flake8-use-pathlib
"RUF", # Ruff-specific rules
"SIM", # flake8-simplify
"UP", # pyupgrade
"T20", # flake8-print
]
extend-ignore = ["ISC001", "N802", "N803", "N806"]

[tool.ruff.lint.per-file-ignores]
"tests/**.py" = ["T201"]

[tool.ruff]
line-length = 110
extend-select = ["B0", "C4", "ICN", "ISC", "N", "RUF", "SIM"]
extend-ignore = ["E731", "N802", "N803", "N806"]
[tool.ruff.lint.isort]
force-sort-within-sections = true

[tool.mypy]
show_error_codes = true
Expand Down
Loading

0 comments on commit 7762458

Please sign in to comment.