Skip to content

🔧 Add additional Ruff lints (and fix issues) #862

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .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 All @@ -36,7 +36,8 @@ def modify_readme(content: str) -> str:
)
content = content.replace("myst-docutils.readthedocs", "myst-parser.readthedocs")
content = content.replace(
"readthedocs.org/projects/myst-docutils", "readthedocs.org/projects/myst-parser"
"readthedocs.org/projects/myst-docutils",
"readthedocs.org/projects/myst-parser",
)
return content

Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
{
"path": "../myst_parser",
"exclude_files": ["_docs.py"],
}
},
]
autodoc2_hidden_objects = ["dunder", "private", "inherited"]
autodoc2_replace_annotations = [
Expand Down
2 changes: 1 addition & 1 deletion docs/live_preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def convert(input_config: str, input_myst: str, writer_name: str) -> dict:
"doctitle_xform": False,
"sectsubtitle_xform": False,
"initial_header_level": 1,
}
},
)
try:
output = publish_string(
Expand Down
15 changes: 10 additions & 5 deletions 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 Expand Up @@ -135,7 +135,7 @@ def run(self):
continue

if self.options.get("scope") == "local" and field.metadata.get(
"global_only"
"global_only",
):
continue

Expand All @@ -152,7 +152,7 @@ def run(self):
f"* - `{name}`",
f" - `{ctype}`",
f" - {description} (default: `{default}`)",
]
],
)

count += 1
Expand Down Expand Up @@ -202,7 +202,9 @@ def run(self):
name = self.arguments[0]
# load the directive class
klass, _ = directives.directive(
name, self.state.memo.language, self.state.document
name,
self.state.memo.language,
self.state.document,
)
if klass is None:
LOGGER.warning(f"Directive {name} not found.", line=self.lineno)
Expand Down Expand Up @@ -402,7 +404,10 @@ class MystLexer(MarkdownLexer):
(
r"^(\()([^\n]+)(\)=)(\n)",
bygroups(
token.Punctuation, token.Name.Label, token.Punctuation, token.Text
token.Punctuation,
token.Name.Label,
token.Punctuation,
token.Text,
),
),
# :::
Expand Down
6 changes: 5 additions & 1 deletion myst_parser/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ def print_anchors(args=None):
help="Output file (default stdout)",
)
arg_parser.add_argument(
"-l", "--level", type=int, default=2, help="Maximum heading level."
"-l",
"--level",
type=int,
default=2,
help="Maximum heading level.",
)
args = arg_parser.parse_args(args)
parser = create_md_parser(MdParserConfig(), RendererHTML)
Expand Down
15 changes: 10 additions & 5 deletions myst_parser/config/dc_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ def validate_fields(inst: Any) -> None:

class ValidatorType(Protocol):
def __call__(
self, inst: Any, field: dc.Field, value: Any, suffix: str = ""
self,
inst: Any,
field: dc.Field,
value: Any,
suffix: str = "",
) -> None:
...

Expand All @@ -63,7 +67,7 @@ def _validator(inst, field, value, suffix=""):
if not isinstance(value, type_):
raise TypeError(
f"'{field.name}{suffix}' must be of type {type_!r} "
f"(got {value!r} that is a {value.__class__!r})."
f"(got {value!r} that is a {value.__class__!r}).",
)

return _validator
Expand Down Expand Up @@ -94,7 +98,7 @@ def is_callable(inst, field, value, suffix=""):
if not callable(value):
raise TypeError(
f"'{field.name}{suffix}' must be callable "
f"(got {value!r} that is a {value.__class__!r})."
f"(got {value!r} that is a {value.__class__!r}).",
)


Expand All @@ -115,14 +119,15 @@ def _validator(inst, field, value, suffix=""):

if not in_options:
raise ValueError(
f"'{field.name}{suffix}' must be in {options!r} (got {value!r})"
f"'{field.name}{suffix}' must be in {options!r} (got {value!r})",
)

return _validator


def deep_iterable(
member_validator: ValidatorType, iterable_validator: ValidatorType | None = None
member_validator: ValidatorType,
iterable_validator: ValidatorType | None = None,
) -> ValidatorType:
"""
A validator that performs deep validation of an iterable.
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
7 changes: 5 additions & 2 deletions myst_parser/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,10 +401,13 @@ def filter_string(


def fetch_inventory(
uri: str, *, timeout: None | float = None, base_url: None | str = None
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
Loading