Skip to content
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

MAINT: enable ruff formatter #565

Merged
merged 3 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 1 addition & 16 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,12 @@ repos:
- id: mixed-line-ending
- id: trailing-whitespace

- repo: https://github.com/asottile/pyupgrade
rev: v3.15.0
hooks:
- id: pyupgrade
args: [--py38-plus]

- repo: https://github.com/PyCQA/isort
rev: 5.12.0
hooks:
- id: isort

- repo: https://github.com/psf/black
rev: 23.10.1
hooks:
- id: black

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.4
hooks:
- id: ruff
args: ["--fix", "--show-fixes"]
- id: ruff-format

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.6.1
Expand Down
4 changes: 1 addition & 3 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,7 @@ class MystNbConfigDirective(_ConfigBase):
required_arguments = 1
option_spec = {
"sphinx": directives.flag,
"section": lambda x: directives.choice(
x, ["config", "read", "execute", "render"]
),
"section": lambda x: directives.choice(x, ["config", "read", "execute", "render"]),
}

def run(self):
Expand Down
20 changes: 5 additions & 15 deletions myst_nb/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ def quickstart(args: list[str] | None = None):
# write conf.py
(path / "conf.py").write_text(generate_conf_py(), encoding="utf-8")
# write index.md
(path / "index.md").write_text(
generate_index(["notebook1", "notebook2"]), encoding="utf-8"
)
(path / "index.md").write_text(generate_index(["notebook1", "notebook2"]), encoding="utf-8")
# write notebook1.ipynb
(path / "notebook1.ipynb").write_text(generate_jupyter_notebook(), encoding="utf-8")
# write notebook2.md
Expand All @@ -42,12 +40,8 @@ def quickstart(args: list[str] | None = None):

def create_quickstart_cli():
cli = argparse.ArgumentParser(description="Create a basic myst_nb project.")
cli.add_argument(
"path", metavar="PATH", type=str, help="Directory to output the project."
)
cli.add_argument(
"-o", "--overwrite", action="store_true", help="Overwrite existing files."
)
cli.add_argument("path", metavar="PATH", type=str, help="Directory to output the project.")
cli.add_argument("-o", "--overwrite", action="store_true", help="Overwrite existing files.")
cli.add_argument("-v", "--verbose", action="store_true", help="Increase verbosity.")
return cli

Expand Down Expand Up @@ -166,18 +160,14 @@ def create_md_to_nb_cli():
cli = argparse.ArgumentParser(
description="Convert a text-based notebook to a Jupyter notebook."
)
cli.add_argument(
"inpath", metavar="PATH_IN", type=str, help="Path to Markdown file."
)
cli.add_argument("inpath", metavar="PATH_IN", type=str, help="Path to Markdown file.")
cli.add_argument(
"outpath",
metavar="PATH_OUT",
nargs="?",
type=str,
help="Path to output to.",
)
cli.add_argument(
"-o", "--overwrite", action="store_true", help="Overwrite existing files."
)
cli.add_argument("-o", "--overwrite", action="store_true", help="Overwrite existing files.")
cli.add_argument("-v", "--verbose", action="store_true", help="Increase verbosity.")
return cli
27 changes: 7 additions & 20 deletions myst_nb/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
if isinstance(reader, str):
output[suffix] = (reader, {}, False)
elif not isinstance(reader, Sequence):
raise TypeError(
f"`nb_custom_formats` values must be a string or sequence: {reader}"
)
raise TypeError(f"`nb_custom_formats` values must be a string or sequence: {reader}")

Check warning on line 30 in myst_nb/core/config.py

View check run for this annotation

Codecov / codecov/patch

myst_nb/core/config.py#L30

Added line #L30 was not covered by tests
elif len(reader) == 2:
output[suffix] = (reader[0], reader[1], False)
elif len(reader) == 3:
Expand All @@ -40,18 +38,12 @@
f"2 or 3: {reader}"
)
if not isinstance(output[suffix][0], str):
raise TypeError(
f"`nb_custom_formats` values[0] must be a string: {output[suffix][0]}"
)
raise TypeError(f"`nb_custom_formats` values[0] must be a string: {output[suffix][0]}")

Check warning on line 41 in myst_nb/core/config.py

View check run for this annotation

Codecov / codecov/patch

myst_nb/core/config.py#L41

Added line #L41 was not covered by tests
# TODO check can be loaded as a python object?
if not isinstance(output[suffix][1], dict):
raise TypeError(
f"`nb_custom_formats` values[1] must be a dict: {output[suffix][1]}"
)
raise TypeError(f"`nb_custom_formats` values[1] must be a dict: {output[suffix][1]}")

Check warning on line 44 in myst_nb/core/config.py

View check run for this annotation

Codecov / codecov/patch

myst_nb/core/config.py#L44

Added line #L44 was not covered by tests
if not isinstance(output[suffix][2], bool):
raise TypeError(
f"`nb_custom_formats` values[2] must be a bool: {output[suffix][2]}"
)
raise TypeError(f"`nb_custom_formats` values[2] must be a bool: {output[suffix][2]}")

Check warning on line 46 in myst_nb/core/config.py

View check run for this annotation

Codecov / codecov/patch

myst_nb/core/config.py#L46

Added line #L46 was not covered by tests
return output


Expand Down Expand Up @@ -264,8 +256,7 @@
default=False,
metadata={
"validator": instance_of(bool),
"help": "Raise an exception on failed execution, "
"rather than emitting a warning",
"help": "Raise an exception on failed execution, " "rather than emitting a warning",
"sections": (Section.global_lvl, Section.file_lvl, Section.execute),
},
)
Expand Down Expand Up @@ -389,9 +380,7 @@
default=(),
metadata={
"validator": deep_iterable(
has_items(
instance_of(str), instance_of(str), optional(instance_of(int))
),
has_items(instance_of(str), instance_of(str), optional(instance_of(int))),
),
"help": "Overrides for the base render priority of mime types: "
"list of (builder name, mime type, priority)",
Expand All @@ -401,9 +390,7 @@
},
repr=False,
)
output_stderr: Literal[
"show", "remove", "remove-warn", "warn", "error", "severe"
] = dc.field(
output_stderr: Literal["show", "remove", "remove-warn", "warn", "error", "severe"] = dc.field(
default="show",
metadata={
"validator": in_(
Expand Down
8 changes: 2 additions & 6 deletions myst_nb/core/execute/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ def __enter__(self) -> NotebookClientBase:
"""Enter the context manager."""
self.start_client()
# extract glue data from the notebook
self._glue_data = extract_glue_data(
self.notebook, self._source_map, self.logger
)
self._glue_data = extract_glue_data(self.notebook, self._source_map, self.logger)
return self

@final
Expand Down Expand Up @@ -154,9 +152,7 @@ def nb_source_code_lexer(self) -> str | None:
lexer = (metadata.get("kernelspec") or {}).get("language", None)
return lexer

def code_cell_outputs(
self, cell_index: int
) -> tuple[int | None, list[NotebookNode]]:
def code_cell_outputs(self, cell_index: int) -> tuple[int | None, list[NotebookNode]]:
"""Get the outputs of a cell.

:returns: a tuple of the execution_count and the outputs
Expand Down
4 changes: 1 addition & 3 deletions myst_nb/core/execute/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@
return

if self.path is None:
raise ValueError(
"Input source must exist as file, if execution_mode is 'cache'"
)
raise ValueError("Input source must exist as file, if execution_mode is 'cache'")

Check warning on line 48 in myst_nb/core/execute/cache.py

View check run for this annotation

Codecov / codecov/patch

myst_nb/core/execute/cache.py#L48

Added line #L48 was not covered by tests

# attempt to execute the notebook
read_fmt = self._kwargs.get("read_fmt", None)
Expand Down
4 changes: 1 addition & 3 deletions myst_nb/core/execute/direct.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
cwd_context = TemporaryDirectory()
else:
if self.path is None:
raise ValueError(
"Input source must exist as file, if execution_in_temp=False"
)
raise ValueError("Input source must exist as file, if execution_in_temp=False")

Check warning on line 27 in myst_nb/core/execute/direct.py

View check run for this annotation

Codecov / codecov/patch

myst_nb/core/execute/direct.py#L27

Added line #L27 was not covered by tests
cwd_context = nullcontext(str(self.path.parent))

# execute in the context of the current working directory
Expand Down
16 changes: 4 additions & 12 deletions myst_nb/core/execute/inline.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@
resources = {"metadata": {"path": self._tmp_path}}
else:
if self.path is None:
raise ValueError(
"Input source must exist as file, if execution_in_temp=False"
)
raise ValueError("Input source must exist as file, if execution_in_temp=False")

Check warning on line 44 in myst_nb/core/execute/inline.py

View check run for this annotation

Codecov / codecov/patch

myst_nb/core/execute/inline.py#L44

Added line #L44 was not covered by tests
resources = {"metadata": {"path": str(self.path.parent)}}

self.logger.info("Starting inline execution client")
Expand All @@ -68,9 +66,7 @@
msg_id = self._client.kc.kernel_info()
info_msg = self._client.wait_for_reply(msg_id)
if info_msg is not None and "language_info" in info_msg["content"]:
self.notebook.metadata["language_info"] = info_msg["content"][
"language_info"
]
self.notebook.metadata["language_info"] = info_msg["content"]["language_info"]
else:
self.logger.warning("Failed to retrieve language info from kernel")

Expand All @@ -96,9 +92,7 @@
"runtime": _exec_time,
"method": self.nb_config.execution_mode,
"succeeded": False if self._cell_error else True,
"error": f"{self._cell_error.__class__.__name__}"
if self._cell_error
else None,
"error": f"{self._cell_error.__class__.__name__}" if self._cell_error else None,
"traceback": self._exc_string,
}
if not self._cell_error:
Expand All @@ -111,9 +105,7 @@
if self._tmp_path:
shutil.rmtree(self._tmp_path, ignore_errors=True)

def code_cell_outputs(
self, cell_index: int
) -> tuple[int | None, list[NotebookNode]]:
def code_cell_outputs(self, cell_index: int) -> tuple[int | None, list[NotebookNode]]:
cells = self.notebook.get("cells", [])

# ensure all cells up to and including the requested cell have been executed
Expand Down
4 changes: 1 addition & 3 deletions myst_nb/core/lexers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
}


def _token_from_lexer_state(
bold: bool, faint: bool, fg_color: str | None, bg_color: str | None
):
def _token_from_lexer_state(bold: bool, faint: bool, fg_color: str | None, bg_color: str | None):
"""Construct a token given the current lexer state.

We can only emit one token even though we have a multiple-tuple state.
Expand Down
4 changes: 1 addition & 3 deletions myst_nb/core/loggers.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ class DocutilsDocLogger(logging.LoggerAdapter):
]

def __init__(self, document: nodes.document, type_name: str = DEFAULT_LOG_TYPE):
self.logger: logging.Logger = logging.getLogger(
f"{type_name}-{document.source}"
)
self.logger: logging.Logger = logging.getLogger(f"{type_name}-{document.source}")
# docutils handles the level of output logging
self.logger.setLevel(logging.DEBUG)
if not self.logger.handlers:
Expand Down
28 changes: 7 additions & 21 deletions myst_nb/core/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,7 @@
if "file_format" in metadata and metadata["file_format"] == "mystnb":
return True
if (
metadata.get("jupytext", {})
.get("text_representation", {})
.get("format_name", None)
metadata.get("jupytext", {}).get("text_representation", {}).get("format_name", None)
!= "myst"
):
return False
Expand All @@ -164,9 +162,7 @@

Used as plugin for jupyter-cache.
"""
return read_myst_markdown_notebook(
Path(uri).read_text("utf8"), add_source_map=True, path=uri
)
return read_myst_markdown_notebook(Path(uri).read_text("utf8"), add_source_map=True, path=uri)

Check warning on line 165 in myst_nb/core/read.py

View check run for this annotation

Codecov / codecov/patch

myst_nb/core/read.py#L165

Added line #L165 was not covered by tests


def read_myst_markdown_notebook(
Expand All @@ -193,9 +189,7 @@
"""
config = config or MdParserConfig()
# parse markdown file up to the block level (i.e. don't worry about inline text)
inline_config = dc.replace(
config, disable_syntax=(list(config.disable_syntax) + ["inline"])
)
inline_config = dc.replace(config, disable_syntax=(list(config.disable_syntax) + ["inline"]))
parser = create_md_parser(inline_config, RendererHTML)
tokens = parser.parse(text + "\n")
lines = text.splitlines()
Expand Down Expand Up @@ -233,9 +227,7 @@
meta = nbf.from_dict(md_metadata)
if md_source:
source_map.append(start_line)
notebook.cells.append(
nbf_version.new_markdown_cell(source=md_source, metadata=meta)
)
notebook.cells.append(nbf_version.new_markdown_cell(source=md_source, metadata=meta))

# iterate through the tokens to identify notebook cells
nesting_level = 0
Expand All @@ -255,9 +247,7 @@
options, body_lines = _read_fenced_cell(token, len(notebook.cells), "Code")
# Parse :load: or load: tags and populate body with contents of file
if "load" in options:
body_lines = _load_code_from_file(
path, options["load"], token, body_lines
)
body_lines = _load_code_from_file(path, options["load"], token, body_lines)
meta = nbf.from_dict(options)
source_map.append(token_map[0] + 1)
notebook.cells.append(
Expand Down Expand Up @@ -343,17 +333,13 @@
)
if not isinstance(metadata, dict):
raise MystMetadataParsingError(
"Markdown cell {} at line {} is not a dict".format(
cell_index, token.map[0] + 1
)
"Markdown cell {} at line {} is not a dict".format(cell_index, token.map[0] + 1)
)

return metadata


def _load_code_from_file(
nb_path: None | str | Path, file_name: str, token, body_lines: list[str]
):
def _load_code_from_file(nb_path: None | str | Path, file_name: str, token, body_lines: list[str]):
"""load source code from a file."""
if nb_path is None:
raise _LoadFileParsingError("path to notebook not supplied for :load:")
Expand Down
Loading