Skip to content

Commit 4e7128c

Browse files
danieleadesdaniel.eadeschrisjsewell
authored
🔧 Add additional Ruff lints (and fix issues) (#862)
Co-authored-by: daniel.eades <daniel.eades@seebyte.com> Co-authored-by: Chris Sewell <chrisj_sewell@hotmail.com>
1 parent c3fa3db commit 4e7128c

15 files changed

Lines changed: 34 additions & 28 deletions

File tree

.github/workflows/docutils_setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def modify_toml(content: str) -> str:
1616
dependencies = []
1717
sphinx_extra = []
1818
for dep in doc["project"]["dependencies"]:
19-
if dep.startswith("docutils") or dep.startswith("sphinx"):
19+
if dep.startswith(("docutils", "sphinx")):
2020
sphinx_extra.append(dep)
2121
else:
2222
dependencies.append(dep)

myst_parser/_docs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def field_type(field):
9696
get_args(field.type) if get_origin(field.type) is Union else [field.type]
9797
)
9898
ctype = " | ".join(
99-
str("None" if ftype == type(None) else ftype) # type: ignore
99+
str("None" if ftype == type(None) else ftype) # type: ignore[comparison-overlap]
100100
for ftype in ftypes
101101
)
102102
ctype = " ".join(ctype.splitlines())

myst_parser/config/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def check_url_schemes(inst: "MdParserConfig", field: dc.Field, value: Any) -> No
104104
raise TypeError(
105105
f"'{field.name}[{key}][classes]' is not a list of str: {val['classes']!r}"
106106
)
107-
new_dict[key] = val # type: ignore
107+
new_dict[key] = val # type: ignore[assignment]
108108
else:
109109
raise TypeError(
110110
f"'{field.name}[{key}]' value is not a string or dict: {val!r}"
@@ -577,7 +577,7 @@ def read_topmatter(text: Union[str, Iterator[str]]) -> Optional[Dict[str, Any]]:
577577
return None
578578
top_matter = []
579579
for line in text:
580-
if line.startswith("---") or line.startswith("..."):
580+
if line.startswith(("---", "...")):
581581
break
582582
top_matter.append(line.rstrip() + "\n")
583583
try:

myst_parser/inventory.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ def fetch_inventory(
404404
uri: str, *, timeout: None | float = None, base_url: None | str = None
405405
) -> InventoryType:
406406
"""Fetch an inventory from a URL or local path."""
407-
if uri.startswith("http://") or uri.startswith("https://"):
407+
if uri.startswith(("http://", "https://")):
408408
with urlopen(uri, timeout=timeout) as stream:
409409
return load(stream, base_url=base_url)
410410
with open(uri, "rb") as stream:

myst_parser/mdit_to_docutils/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,9 +1589,9 @@ def render_dl(self, token: SyntaxTreeNode) -> None:
15891589
from sphinx.domains.std import make_glossary_term
15901590

15911591
term = make_glossary_term(
1592-
self.sphinx_env, # type: ignore
1592+
self.sphinx_env, # type: ignore[arg-type]
15931593
term.children,
1594-
None, # type: ignore
1594+
None, # type: ignore[arg-type]
15951595
term.source,
15961596
term.line,
15971597
node_id=None,

myst_parser/mocking.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -371,14 +371,14 @@ def run(self) -> list[nodes.Element]:
371371
# tab_width = self.options.get("tab-width", self.document.settings.tab_width)
372372
try:
373373
file_content = path.read_text(encoding=encoding, errors=error_handler)
374-
except FileNotFoundError:
374+
except FileNotFoundError as error:
375375
raise DirectiveError(
376376
4, f'Directive "{self.name}": file not found: {str(path)!r}'
377-
)
377+
) from error
378378
except Exception as error:
379379
raise DirectiveError(
380380
4, f'Directive "{self.name}": error reading file: {path}\n{error}.'
381-
)
381+
) from error
382382

383383
# get required section of text
384384
startline = self.options.get("start-line", None)
@@ -412,10 +412,10 @@ def run(self) -> list[nodes.Element]:
412412
if "number-lines" in self.options:
413413
try:
414414
startline = int(self.options["number-lines"] or 1)
415-
except ValueError:
415+
except ValueError as err:
416416
raise DirectiveError(
417417
3, ":number-lines: with non-integer start value"
418-
)
418+
) from err
419419
endline = startline + len(file_content.splitlines())
420420
if file_content.endswith("\n"):
421421
file_content = file_content[:-1]

myst_parser/parsers/docutils_.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ def _validate_yaml(
9797
"""
9898
try:
9999
output = yaml.safe_load(value)
100-
except Exception:
101-
raise ValueError("Invalid YAML string")
100+
except Exception as err:
101+
raise ValueError("Invalid YAML string") from err
102102
if not isinstance(output, dict):
103103
raise ValueError("Expecting a YAML dictionary")
104104
return output
@@ -115,8 +115,8 @@ def _validate_url_schemes(
115115
"""
116116
try:
117117
output = yaml.safe_load(value)
118-
except Exception:
119-
raise ValueError("Invalid YAML string")
118+
except Exception as err:
119+
raise ValueError("Invalid YAML string") from err
120120
if isinstance(output, str):
121121
output = {k: None for k in output.split(",")}
122122
if not isinstance(output, dict):

myst_parser/parsers/parse_html.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ def enclose(self, name: str):
362362
count = 0
363363

364364
# It pops all the items which do not match with the closing tag.
365-
for _ in range(0, count):
365+
for _ in range(count):
366366
self.stack.pop()
367367

368368

myst_parser/sphinx_ext/directives.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""MyST specific directives"""
2+
from __future__ import annotations
3+
24
from copy import copy
3-
from typing import List, Tuple, cast
5+
from typing import cast
46

57
from docutils import nodes
68
from docutils.parsers.rst import directives
@@ -27,7 +29,7 @@ class SubstitutionReferenceRole(SphinxRole):
2729
Note, in ``docutils/parsers/rst/roles.py`` this is left unimplemented.
2830
"""
2931

30-
def run(self) -> Tuple[List[nodes.Node], List[nodes.system_message]]:
32+
def run(self) -> tuple[list[nodes.Node], list[nodes.system_message]]:
3133
subref_node = nodes.substitution_reference(self.rawtext, self.text)
3234
self.set_source_info(subref_node, self.lineno)
3335
subref_node["refname"] = nodes.fully_normalize_name(self.text)
@@ -59,7 +61,7 @@ class FigureMarkdown(SphinxDirective):
5961
"name": directives.unchanged,
6062
}
6163

62-
def run(self) -> List[nodes.Node]:
64+
def run(self) -> list[nodes.Node]:
6365
figwidth = self.options.pop("width", None)
6466
figclasses = self.options.pop("class", None)
6567
align = self.options.pop("align", None)

myst_parser/sphinx_ext/mathjax.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ def override_mathjax(app: Sphinx):
5353

5454
if "dollarmath" not in app.config["myst_enable_extensions"]:
5555
return
56-
if not app.env.myst_config.update_mathjax: # type: ignore
56+
if not app.env.myst_config.update_mathjax: # type: ignore[attr-defined]
5757
return
5858

59-
mjax_classes = app.env.myst_config.mathjax_classes # type: ignore
59+
mjax_classes = app.env.myst_config.mathjax_classes # type: ignore[attr-defined]
6060

6161
if "mathjax3_config" in app.config:
6262
# sphinx 4 + mathjax 3
63-
app.config.mathjax3_config = app.config.mathjax3_config or {} # type: ignore
63+
app.config.mathjax3_config = app.config.mathjax3_config or {} # type: ignore[attr-defined]
6464
app.config.mathjax3_config.setdefault("options", {})
6565
if (
6666
"processHtmlClass" in app.config.mathjax3_config["options"]

0 commit comments

Comments
 (0)