Skip to content

Commit aa3f04d

Browse files
authored
🔧 Replace flake8 with ruff (#683)
1 parent dff96c4 commit aa3f04d

File tree

15 files changed

+67
-77
lines changed

15 files changed

+67
-77
lines changed

.pre-commit-config.yaml

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,26 @@ repos:
2020
- id: trailing-whitespace
2121

2222
- repo: https://github.com/asottile/pyupgrade
23-
rev: v3.3.0
23+
rev: v3.3.1
2424
hooks:
2525
- id: pyupgrade
2626
args: [--py37-plus]
2727

2828
- repo: https://github.com/PyCQA/isort
29-
rev: 5.10.1
29+
rev: 5.11.4
3030
hooks:
3131
- id: isort
3232

3333
- repo: https://github.com/psf/black
34-
rev: 22.10.0
34+
rev: 22.12.0
3535
hooks:
3636
- id: black
3737

38-
- repo: https://github.com/PyCQA/flake8
39-
rev: 6.0.0
38+
- repo: https://github.com/charliermarsh/ruff-pre-commit
39+
rev: v0.0.218
4040
hooks:
41-
- id: flake8
42-
additional_dependencies:
43-
- flake8-comprehensions
44-
- flake8-bugbear
45-
# - flake8-self~=0.2.2
41+
- id: ruff
42+
args: ["--force-exclude"]
4643

4744
- repo: https://github.com/pre-commit/mirrors-mypy
4845
rev: v0.991

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ class StripUnsupportedLatex(SphinxPostTransform):
161161
default_priority = 900
162162

163163
def run(self):
164-
if not self.app.builder.format == "latex":
164+
if self.app.builder.format != "latex":
165165
return
166166
from docutils import nodes
167167

myst_parser/_compat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from docutils.nodes import Element
66

77
if sys.version_info >= (3, 8):
8-
from typing import Literal, Protocol, TypedDict, get_args, get_origin # noqa: F401
8+
from typing import Literal, Protocol, TypedDict, get_args, get_origin
99
else:
1010
from typing_extensions import ( # noqa: F401
1111
Literal,

myst_parser/_docs.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,11 @@ def field_default(value):
4242
@staticmethod
4343
def field_type(field):
4444
ftypes: Sequence[str]
45-
if get_origin(field.type) is Union:
46-
ftypes = get_args(field.type)
47-
else:
48-
ftypes = [field.type]
45+
ftypes = (
46+
get_args(field.type) if get_origin(field.type) is Union else [field.type]
47+
)
4948
ctype = " | ".join(
50-
str("None" if ftype == type(None) else ftype) # type: ignore # noqa: E721
49+
str("None" if ftype == type(None) else ftype) # type: ignore
5150
for ftype in ftypes
5251
)
5352
ctype = " ".join(ctype.splitlines())
@@ -87,9 +86,10 @@ def run(self):
8786
if field.metadata.get("extension"):
8887
continue
8988

90-
if self.options.get("scope") == "local":
91-
if field.metadata.get("global_only"):
92-
continue
89+
if self.options.get("scope") == "local" and field.metadata.get(
90+
"global_only"
91+
):
92+
continue
9393

9494
if self.options.get("scope") == "global":
9595
name = f"myst_{name}"

myst_parser/mdit_to_docutils/base.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,11 @@ def nested_render_text(
342342
:param inline: whether the text is inline or block
343343
:param allow_headings: whether to allow headings in the text
344344
"""
345-
if inline:
346-
tokens = self.md.parseInline(text, self.md_env)
347-
else:
348-
tokens = self.md.parse(text + "\n", self.md_env)
345+
tokens = (
346+
self.md.parseInline(text, self.md_env)
347+
if inline
348+
else self.md.parse(text + "\n", self.md_env)
349+
)
349350

350351
# remove front matter, if present, e.g. from included documents
351352
if tokens and tokens[0].type == "front_matter":
@@ -390,10 +391,8 @@ def render_children(self, token: SyntaxTreeNode) -> None:
390391

391392
def add_line_and_source_path(self, node, token: SyntaxTreeNode) -> None:
392393
"""Copy the line number and document source path to the docutils node."""
393-
try:
394+
with suppress(ValueError):
394395
node.line = token_line(token)
395-
except ValueError:
396-
pass
397396
node.source = self.document["source"]
398397

399398
def add_line_and_source_path_r(
@@ -487,7 +486,7 @@ def update_section_level_state(self, section: nodes.section, level: int) -> None
487486
if section_level <= level
488487
}
489488

490-
def renderInlineAsText(self, tokens: list[SyntaxTreeNode]) -> str:
489+
def renderInlineAsText(self, tokens: list[SyntaxTreeNode]) -> str: # noqa: N802
491490
"""Special kludge for image `alt` attributes to conform CommonMark spec.
492491
493492
Don't try to use it! Spec requires to show `alt` content with stripped markup,
@@ -1196,9 +1195,10 @@ def render_table_row(self, token: SyntaxTreeNode) -> None:
11961195
"text-align:center",
11971196
):
11981197
entry["classes"].append(f"text-{cast(str, style).split(':')[1]}")
1199-
with self.current_node_context(entry, append=True):
1200-
with self.current_node_context(para, append=True):
1201-
self.render_children(child)
1198+
with self.current_node_context(
1199+
entry, append=True
1200+
), self.current_node_context(para, append=True):
1201+
self.render_children(child)
12021202

12031203
def render_s(self, token: SyntaxTreeNode) -> None:
12041204
"""Render a strikethrough token."""
@@ -1397,7 +1397,7 @@ def render_field_list(self, token: SyntaxTreeNode) -> None:
13971397
children = (token.children or [])[:]
13981398
while children:
13991399
child = children.pop(0)
1400-
if not child.type == "fieldlist_name":
1400+
if child.type != "fieldlist_name":
14011401
error_msg = self.reporter.error(
14021402
(
14031403
"Expected a fieldlist_name as a child of a field_list"

myst_parser/mdit_to_docutils/html_to_nodes.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,16 @@ def html_to_nodes(
103103

104104
else:
105105
children = child.strip().children
106-
if (
107-
children
106+
title = (
107+
"".join(child.render() for child in children.pop(0))
108+
if children
108109
and children[0].name in ("div", "p")
109110
and (
110111
"title" in children[0].attrs.classes
111112
or "admonition-title" in children[0].attrs.classes
112113
)
113-
):
114-
title = "".join(child.render() for child in children.pop(0))
115-
else:
116-
title = "Note"
114+
else "Note"
115+
)
117116

118117
options = "\n".join(
119118
f":{k}: {v}"

myst_parser/mdit_to_docutils/utils.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ def is_external_url(
2929
3030
"""
3131
url_check = urlparse(reference)
32-
if known_url_schemes is not None:
33-
scheme_known = url_check.scheme in known_url_schemes
34-
else:
35-
scheme_known = bool(url_check.scheme)
32+
scheme_known = (
33+
url_check.scheme in known_url_schemes
34+
if known_url_schemes is not None
35+
else bool(url_check.scheme)
36+
)
3637
return scheme_known or (match_fragment and url_check.fragment != "")

myst_parser/mocking.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ def nested_parse(
178178

179179
def parse_target(self, block, block_text, lineno: int):
180180
"""
181-
Taken from https://github.com/docutils-mirror/docutils/blob/e88c5fb08d5cdfa8b4ac1020dd6f7177778d5990/docutils/parsers/rst/states.py#L1927 # noqa: E501
182-
"""
181+
Taken from https://github.com/docutils-mirror/docutils/blob/e88c5fb08d5cdfa8b4ac1020dd6f7177778d5990/docutils/parsers/rst/states.py#L1927
182+
""" # noqa: E501
183183
# Commenting out this code because it only applies to rST
184184
# if block and block[-1].strip()[-1:] == "_": # possible indirect target
185185
# reference = " ".join([line.strip() for line in block])
@@ -267,16 +267,13 @@ def __getattr__(self, name: str):
267267
been defined. Defined attributes will not be overridden.
268268
"""
269269
cls = type(self).__name__
270-
if hasattr(Body, name):
271-
msg = (
272-
f"{cls} has not yet implemented attribute '{name}'. "
273-
"You can parse RST directly via the `{eval-rst}` directive: "
274-
"https://myst-parser.readthedocs.io/en/latest/syntax/syntax.html#how-directives-parse-content" # noqa: E501
275-
)
276-
else:
277-
# The requested `name` is not a docutils Body element
278-
# (such as "footnote", "block_quote", "paragraph", …)
279-
msg = f"{cls} has no attribute '{name}'"
270+
msg = (
271+
f"{cls} has not yet implemented attribute '{name}'. "
272+
"You can parse RST directly via the `{{eval-rst}}` directive: "
273+
"https://myst-parser.readthedocs.io/en/latest/syntax/syntax.html#how-directives-parse-content"
274+
if hasattr(Body, name)
275+
else f"{cls} has no attribute '{name}'"
276+
)
280277
raise MockingError(msg).with_traceback(sys.exc_info()[2])
281278

282279

@@ -419,7 +416,7 @@ def run(self) -> list[nodes.Element]:
419416
startline = int(self.options["number-lines"] or 1)
420417
except ValueError:
421418
raise DirectiveError(
422-
3, ":number-lines: with non-integer " "start value"
419+
3, ":number-lines: with non-integer start value"
423420
)
424421
endline = startline + len(file_content.splitlines())
425422
if file_content.endswith("\n"):

myst_parser/parsers/parse_html.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,11 @@ def find(
178178
iterator = self.walk() if recurse else self
179179
if include_self:
180180
iterator = itertools.chain([self], iterator)
181-
if inspect.isclass(identifier):
182-
test_func = lambda c: isinstance(c, identifier) # noqa: E731
183-
else:
184-
test_func = lambda c: c.name == identifier # noqa: E731
181+
test_func = (
182+
(lambda c: isinstance(c, identifier))
183+
if inspect.isclass(identifier)
184+
else lambda c: c.name == identifier
185+
)
185186
classes = set(classes) if classes is not None else classes
186187
for child in iterator:
187188
if test_func(child):

myst_parser/sphinx_ext/directives.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def run(self) -> List[nodes.Node]:
7979
finally:
8080
state._renderer.md_config.enable_extensions = myst_extensions
8181

82-
if not len(node.children) == 2:
82+
if len(node.children) != 2:
8383
return [
8484
self.figure_error(
8585
"content should be one image, "

0 commit comments

Comments
 (0)