Skip to content

Commit

Permalink
Enable ruff COM (flake8-commas) checks
Browse files Browse the repository at this point in the history
  • Loading branch information
mitya57 committed Dec 8, 2024
1 parent fe7663e commit 85fb72f
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ jobs:
- name: Install ruff
run: python -m pip install ruff
- name: Run ruff check
run: ruff check --select F,E,W,I,UP --target-version py310 .
run: ruff check --select F,E,W,I,UP,COM --target-version py310 .
- name: Run ruff format
run: ruff format --diff .
8 changes: 6 additions & 2 deletions markup2html.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,19 @@ def export_file(args: argparse.Namespace) -> None:
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--web-environment", help="export for web environment", action="store_true"
"--web-environment",
help="export for web environment",
action="store_true",
)
parser.add_argument(
"--include-stylesheet",
help="embed the stylesheet into html",
action="store_true",
)
parser.add_argument(
"--fallback-title", help="fallback title of the HTML document", metavar="TITLE"
"--fallback-title",
help="fallback title of the HTML document",
metavar="TITLE",
)
parser.add_argument("input_file", help="input file")
parser.add_argument("output_file", help="output file")
Expand Down
9 changes: 6 additions & 3 deletions markups/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,21 @@ def get_available_markups() -> list[type[AbstractMarkup]]:

@overload
def get_markup_for_file_name(
filename: str, return_class: Literal[False] = False
filename: str,
return_class: Literal[False] = False,
) -> AbstractMarkup | None: ...


@overload
def get_markup_for_file_name(
filename: str, return_class: Literal[True]
filename: str,
return_class: Literal[True],
) -> type[AbstractMarkup] | None: ...


def get_markup_for_file_name(
filename: str, return_class: bool = False
filename: str,
return_class: bool = False,
) -> AbstractMarkup | type[AbstractMarkup] | None:
"""
:param filename: name of the file
Expand Down
6 changes: 5 additions & 1 deletion markups/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ class ConvertedMarkup:
"""

def __init__(
self, body: str, title: str = "", stylesheet: str = "", javascript: str = ""
self,
body: str,
title: str = "",
stylesheet: str = "",
javascript: str = "",
):
self.title = title
self.stylesheet = stylesheet
Expand Down
22 changes: 15 additions & 7 deletions markups/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,17 @@ def available() -> bool:
return getattr(markdown, "__version_info__", (2,)) >= (3,)

def _load_extensions_list_from_txt_file(
self, filename: str
self,
filename: str,
) -> Iterator[_name_and_config]:
with open(filename) as extensions_file:
for line in extensions_file:
if not line.startswith("#"):
yield self._split_extension_config(line.rstrip())

def _load_extensions_list_from_yaml_file(
self, filename: str
self,
filename: str,
) -> Iterator[_name_and_config]:
with open(filename) as extensions_file:
try:
Expand All @@ -119,7 +121,8 @@ def _load_extensions_list_from_yaml_file(
yield item, {}

def _get_global_extensions(
self, filename: str | None
self,
filename: str | None,
) -> Iterator[_name_and_config]:
local_directory = os.path.dirname(filename) if filename else ""
choices = [
Expand Down Expand Up @@ -171,7 +174,8 @@ def _split_extension_config(self, extension_name: str) -> _name_and_config:
return extension_name, {x.strip(): y.strip() for (x, y) in pairs}

def _split_extensions_configs(
self, extensions: Iterable[str]
self,
extensions: Iterable[str],
) -> Iterator[_name_and_config]:
"""Splits the configuration options from a list of strings.
Expand All @@ -181,7 +185,8 @@ def _split_extensions_configs(
yield self._split_extension_config(extension)

def _apply_extensions(
self, document_extensions: Iterable[_name_and_config] | None = None
self,
document_extensions: Iterable[_name_and_config] | None = None,
) -> None:
extensions = self.global_extensions.copy()
extensions.extend(self._split_extensions_configs(self.requested_extensions))
Expand All @@ -207,7 +212,8 @@ def _apply_extensions(
candidate = self._canonicalize_extension_name(name)
if candidate is None:
warnings.warn(
f'Extension "{name}" does not exist.', ImportWarning
f'Extension "{name}" does not exist.',
ImportWarning,
)
continue
canonical_name = candidate
Expand All @@ -223,7 +229,9 @@ def _apply_extensions(
self.extension_configs = extension_configs

def __init__(
self, filename: str | None = None, extensions: list[str] | None = None
self,
filename: str | None = None,
extensions: list[str] | None = None,
):
AbstractMarkup.__init__(self, filename)
import markdown
Expand Down
9 changes: 7 additions & 2 deletions markups/restructuredtext.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@

class CustomHTMLTranslator(HTMLTranslator): # type: ignore
def starttag( # type: ignore
self, node, tagname, suffix="\n", empty=False, **attributes
self,
node,
tagname,
suffix="\n",
empty=False,
**attributes,
):
if getattr(node, "line", None) is not None:
attributes["data-posmap"] = node.line
Expand Down Expand Up @@ -65,7 +70,7 @@ def __init__(
"syntax_highlight": "short",
"halt_level": 5, # Never convert system messages to exceptions
"stylesheet_path": "minimal.css", # Do not include plain.css
}
},
)
AbstractMarkup.__init__(self, filename)
self.writer = Writer()
Expand Down
6 changes: 4 additions & 2 deletions tests/test_asciidoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@


@unittest.skipUnless(
AsciiDocMarkup.available(), "asciidoc.py and/or lxml not available"
AsciiDocMarkup.available(),
"asciidoc.py and/or lxml not available",
)
class AsciiDocTextTest(unittest.TestCase):
def test_basic(self) -> None:
Expand All @@ -26,7 +27,8 @@ def test_basic(self) -> None:
def test_error_handling(self) -> None:
markup = AsciiDocMarkup()
with self.assertWarnsRegex(
SyntaxWarning, "section title not allowed in list item"
SyntaxWarning,
"section title not allowed in list item",
):
converted = markup.convert(INVALID_SYNTAX)
self.assertIn("Foo", converted.get_document_body())
Expand Down
16 changes: 9 additions & 7 deletions tests/test_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,12 @@ def test_extensions_loading(self) -> None:
markup = MarkdownMarkup()
self.assertIsNone(markup._canonicalize_extension_name("nonexistent"))
self.assertIsNone(
markup._canonicalize_extension_name("nonexistent(someoption)")
markup._canonicalize_extension_name("nonexistent(someoption)"),
)
self.assertIsNone(markup._canonicalize_extension_name(".foobar"))
self.assertEqual(
markup._canonicalize_extension_name("meta"), "markdown.extensions.meta"
markup._canonicalize_extension_name("meta"),
"markdown.extensions.meta",
)
name, parameters = markup._split_extension_config("toc(anchorlink=1, foo=bar)")
self.assertEqual(name, "toc")
Expand All @@ -181,7 +182,7 @@ def test_loading_extensions_by_module_name(self) -> None:

def test_removing_duplicate_extensions(self) -> None:
markup = MarkdownMarkup(
extensions=["remove_extra", "toc", "markdown.extensions.toc"]
extensions=["remove_extra", "toc", "markdown.extensions.toc"],
)
self.assertEqual(len(markup.extensions), 1)
self.assertIn("markdown.extensions.toc", markup.extensions)
Expand Down Expand Up @@ -209,7 +210,7 @@ def test_document_extensions_parameters(self) -> None:
" toc(title=Table of contents, baselevel=3) wikilinks --->\n\n"
)
html = markup.convert(
toc_header + "[TOC]\n\n# Header\n[[Link]]"
toc_header + "[TOC]\n\n# Header\n[[Link]]",
).get_document_body()
self.assertEqual(
html,
Expand Down Expand Up @@ -248,7 +249,7 @@ def test_remove_extra(self) -> None:
def test_remove_extra_document_extension(self) -> None:
markup = MarkdownMarkup(extensions=[])
html = markup.convert(
"Required-Extensions: remove_extra\n\n" + tables_source
"Required-Extensions: remove_extra\n\n" + tables_source,
).get_document_body()
self.assertNotIn("<table>", html)

Expand Down Expand Up @@ -321,7 +322,8 @@ def test_extensions_txt_file(self) -> None:
f.write("foo\n# bar\nbaz(arg=value)\n")
markup = MarkdownMarkup(filename=join(tmpdirname, "foo.md"))
self.assertEqual(
markup.global_extensions, [("foo", {}), ("baz", {"arg": "value"})]
markup.global_extensions,
[("foo", {}), ("baz", {"arg": "value"})],
)

@unittest.skipIf(not HAVE_YAML, "PyYAML module is not available")
Expand All @@ -339,7 +341,7 @@ def test_extensions_yaml_file(self) -> None:
" permalink: True\n"
' separator: "_"\n'
" toc_depth: 3\n"
"- sane_lists\n"
"- sane_lists\n",
)
markup = MarkdownMarkup(filename=join(tmpdirname, "foo.md"))
self.assertEqual(
Expand Down
3 changes: 2 additions & 1 deletion tests/test_public_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ def test_api(self) -> None:
markup_class = markups.get_markup_for_file_name("myfile.mkd", return_class=True)
self.assertEqual(markups.MarkdownMarkup, markup_class)
markup_class = markups.get_markup_for_file_name(
"myfile.adoc", return_class=True
"myfile.adoc",
return_class=True,
)
self.assertEqual(markups.AsciiDocMarkup, markup_class)

Expand Down
6 changes: 4 additions & 2 deletions tests/test_restructuredtext.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,17 @@ def test_mathjax_loading(self) -> None:

def test_errors(self) -> None:
markup = ReStructuredTextMarkup(
"/dev/null", settings_overrides={"warning_stream": False}
"/dev/null",
settings_overrides={"warning_stream": False},
)
body = markup.convert("`").get_document_body() # unclosed role
self.assertIn('<p class="system-message-title">System Message: WARNING/2', body)
self.assertIn("/dev/null", body)

def test_errors_overridden(self) -> None:
markup = ReStructuredTextMarkup(
"/dev/null", settings_overrides={"report_level": 4}
"/dev/null",
settings_overrides={"report_level": 4},
)
body = markup.convert("`").get_document_body() # unclosed role
self.assertNotIn("System Message", body)
Expand Down

0 comments on commit 85fb72f

Please sign in to comment.