Skip to content

Commit

Permalink
馃敡 Add additional Ruff lints (and fix issues) (#862)
Browse files Browse the repository at this point in the history
Co-authored-by: daniel.eades <daniel.eades@seebyte.com>
Co-authored-by: Chris Sewell <chrisj_sewell@hotmail.com>
  • Loading branch information
3 people committed Mar 26, 2024
1 parent c3fa3db commit 4e7128c
Show file tree
Hide file tree
Showing 15 changed files with 34 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docutils_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def modify_toml(content: str) -> str:
dependencies = []
sphinx_extra = []
for dep in doc["project"]["dependencies"]:
if dep.startswith("docutils") or dep.startswith("sphinx"):
if dep.startswith(("docutils", "sphinx")):
sphinx_extra.append(dep)
else:
dependencies.append(dep)
Expand Down
2 changes: 1 addition & 1 deletion myst_parser/_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def field_type(field):
get_args(field.type) if get_origin(field.type) is Union else [field.type]
)
ctype = " | ".join(
str("None" if ftype == type(None) else ftype) # type: ignore
str("None" if ftype == type(None) else ftype) # type: ignore[comparison-overlap]
for ftype in ftypes
)
ctype = " ".join(ctype.splitlines())
Expand Down
4 changes: 2 additions & 2 deletions myst_parser/config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def check_url_schemes(inst: "MdParserConfig", field: dc.Field, value: Any) -> No
raise TypeError(
f"'{field.name}[{key}][classes]' is not a list of str: {val['classes']!r}"
)
new_dict[key] = val # type: ignore
new_dict[key] = val # type: ignore[assignment]
else:
raise TypeError(
f"'{field.name}[{key}]' value is not a string or dict: {val!r}"
Expand Down Expand Up @@ -577,7 +577,7 @@ def read_topmatter(text: Union[str, Iterator[str]]) -> Optional[Dict[str, Any]]:
return None
top_matter = []
for line in text:
if line.startswith("---") or line.startswith("..."):
if line.startswith(("---", "...")):
break
top_matter.append(line.rstrip() + "\n")
try:
Expand Down
2 changes: 1 addition & 1 deletion myst_parser/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ def fetch_inventory(
uri: str, *, timeout: None | float = None, base_url: None | str = None
) -> InventoryType:
"""Fetch an inventory from a URL or local path."""
if uri.startswith("http://") or uri.startswith("https://"):
if uri.startswith(("http://", "https://")):
with urlopen(uri, timeout=timeout) as stream:
return load(stream, base_url=base_url)
with open(uri, "rb") as stream:
Expand Down
4 changes: 2 additions & 2 deletions myst_parser/mdit_to_docutils/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1589,9 +1589,9 @@ def render_dl(self, token: SyntaxTreeNode) -> None:
from sphinx.domains.std import make_glossary_term

term = make_glossary_term(
self.sphinx_env, # type: ignore
self.sphinx_env, # type: ignore[arg-type]
term.children,
None, # type: ignore
None, # type: ignore[arg-type]
term.source,
term.line,
node_id=None,
Expand Down
10 changes: 5 additions & 5 deletions myst_parser/mocking.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,14 +371,14 @@ def run(self) -> list[nodes.Element]:
# tab_width = self.options.get("tab-width", self.document.settings.tab_width)
try:
file_content = path.read_text(encoding=encoding, errors=error_handler)
except FileNotFoundError:
except FileNotFoundError as error:
raise DirectiveError(
4, f'Directive "{self.name}": file not found: {str(path)!r}'
)
) from error
except Exception as error:
raise DirectiveError(
4, f'Directive "{self.name}": error reading file: {path}\n{error}.'
)
) from error

# get required section of text
startline = self.options.get("start-line", None)
Expand Down Expand Up @@ -412,10 +412,10 @@ def run(self) -> list[nodes.Element]:
if "number-lines" in self.options:
try:
startline = int(self.options["number-lines"] or 1)
except ValueError:
except ValueError as err:
raise DirectiveError(
3, ":number-lines: with non-integer start value"
)
) from err
endline = startline + len(file_content.splitlines())
if file_content.endswith("\n"):
file_content = file_content[:-1]
Expand Down
8 changes: 4 additions & 4 deletions myst_parser/parsers/docutils_.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ def _validate_yaml(
"""
try:
output = yaml.safe_load(value)
except Exception:
raise ValueError("Invalid YAML string")
except Exception as err:
raise ValueError("Invalid YAML string") from err
if not isinstance(output, dict):
raise ValueError("Expecting a YAML dictionary")
return output
Expand All @@ -115,8 +115,8 @@ def _validate_url_schemes(
"""
try:
output = yaml.safe_load(value)
except Exception:
raise ValueError("Invalid YAML string")
except Exception as err:
raise ValueError("Invalid YAML string") from err
if isinstance(output, str):
output = {k: None for k in output.split(",")}
if not isinstance(output, dict):
Expand Down
2 changes: 1 addition & 1 deletion myst_parser/parsers/parse_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ def enclose(self, name: str):
count = 0

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


Expand Down
8 changes: 5 additions & 3 deletions myst_parser/sphinx_ext/directives.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""MyST specific directives"""
from __future__ import annotations

from copy import copy
from typing import List, Tuple, cast
from typing import cast

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

def run(self) -> Tuple[List[nodes.Node], List[nodes.system_message]]:
def run(self) -> tuple[list[nodes.Node], list[nodes.system_message]]:
subref_node = nodes.substitution_reference(self.rawtext, self.text)
self.set_source_info(subref_node, self.lineno)
subref_node["refname"] = nodes.fully_normalize_name(self.text)
Expand Down Expand Up @@ -59,7 +61,7 @@ class FigureMarkdown(SphinxDirective):
"name": directives.unchanged,
}

def run(self) -> List[nodes.Node]:
def run(self) -> list[nodes.Node]:
figwidth = self.options.pop("width", None)
figclasses = self.options.pop("class", None)
align = self.options.pop("align", None)
Expand Down
6 changes: 3 additions & 3 deletions myst_parser/sphinx_ext/mathjax.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ def override_mathjax(app: Sphinx):

if "dollarmath" not in app.config["myst_enable_extensions"]:
return
if not app.env.myst_config.update_mathjax: # type: ignore
if not app.env.myst_config.update_mathjax: # type: ignore[attr-defined]
return

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

if "mathjax3_config" in app.config:
# sphinx 4 + mathjax 3
app.config.mathjax3_config = app.config.mathjax3_config or {} # type: ignore
app.config.mathjax3_config = app.config.mathjax3_config or {} # type: ignore[attr-defined]
app.config.mathjax3_config.setdefault("options", {})
if (
"processHtmlClass" in app.config.mathjax3_config["options"]
Expand Down
2 changes: 1 addition & 1 deletion myst_parser/warnings_.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class MystWarnings(Enum):
"""Duplicate Markdown reference definition."""
MD_FOOTNOTE_DUPE = "footnote"
"""Duplicate Markdown footnote definition."""
MD_FOOTNOTE_MISSING = "footnote"
MD_FOOTNOTE_MISSING = "footnote" # noqa: PIE796
"""Missing Markdown footnote definition."""
MD_HEADING_NON_CONSECUTIVE = "header"
"""Non-consecutive heading levels."""
Expand Down
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,13 @@ exclude = [
]

[tool.ruff]
extend-select = ["B0", "C4", "I", "ICN", "ISC", "N", "RUF", "SIM", "UP"]
extend-select = ["B", "C4", "FA", "FURB", "I", "ICN", "ISC", "N", "PERF", "PGH", "PIE", "RUF", "SIM", "UP"]
extend-ignore = ["ISC001", "RUF005", "RUF012"]

[tool.ruff.per-file-ignores]
"myst_parser/parsers/docutils_.py" = ["FA"]
"myst_parser/config/main.py" = ["FA"]

[tool.mypy]
show_error_codes = true
check_untyped_defs = true
Expand Down
2 changes: 1 addition & 1 deletion tests/test_renderers/test_fixtures_docutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,5 +131,5 @@ def settings_from_cmdline(cmdline: str | None) -> dict[str, Any]:
try:
pub.process_command_line(shlex.split(cmdline))
except Exception as err:
raise AssertionError(f"Failed to parse commandline: {cmdline}\n{err}")
raise AssertionError(f"Failed to parse commandline: {cmdline}\n{err}") from err
return vars(pub.settings)
2 changes: 1 addition & 1 deletion tests/test_renderers/test_fixtures_sphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def settings_from_json(string: str | None):
data = json.loads(string)
assert isinstance(data, dict), "settings must be a JSON object"
except Exception as err:
raise AssertionError(f"Failed to parse JSON settings: {string}\n{err}")
raise AssertionError(f"Failed to parse JSON settings: {string}\n{err}") from err
return data


Expand Down
2 changes: 1 addition & 1 deletion tests/test_renderers/test_myst_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_cmdline(file_params: ParamTestData):
except Exception as err:
raise AssertionError(
f"Failed to parse commandline: {file_params.description}\n{err}"
)
) from err
settings = vars(pub.settings)
report_stream = StringIO()
settings["output_encoding"] = "unicode"
Expand Down

0 comments on commit 4e7128c

Please sign in to comment.