Skip to content

Commit

Permalink
maint: enable ruff formatter (#565)
Browse files Browse the repository at this point in the history
* maint: enable ruff formatter

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* fix: update typing

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
agoose77 and pre-commit-ci[bot] authored Nov 6, 2023
1 parent c89d452 commit 1d83433
Show file tree
Hide file tree
Showing 31 changed files with 120 additions and 348 deletions.
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 @@ def custom_formats_converter(value: dict) -> Dict[str, Tuple[str, dict, bool]]:
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}")
elif len(reader) == 2:
output[suffix] = (reader[0], reader[1], False)
elif len(reader) == 3:
Expand All @@ -40,18 +38,12 @@ def custom_formats_converter(value: dict) -> Dict[str, Tuple[str, dict, bool]]:
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]}")
# 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]}")
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]}")
return output


Expand Down Expand Up @@ -264,8 +256,7 @@ def __post_init__(self):
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 @@ def __post_init__(self):
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 @@ def __post_init__(self):
},
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 @@ def start_client(self):
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'")

# 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 @@ def start_client(self):
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")
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 @@ def start_client(self):
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")
resources = {"metadata": {"path": str(self.path.parent)}}

self.logger.info("Starting inline execution client")
Expand All @@ -68,9 +66,7 @@ def start_client(self):
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 @@ def close_client(self, exc_type, exc_val, exc_tb):
"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 @@ def close_client(self, exc_type, exc_val, exc_tb):
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 @@ def is_myst_markdown_notebook(text: str | Iterator[str]) -> bool:
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 @@ def myst_nb_reader_plugin(uri: str) -> nbf.NotebookNode:
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)


def read_myst_markdown_notebook(
Expand All @@ -193,9 +189,7 @@ def read_myst_markdown_notebook(
"""
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 @@ def _flush_markdown(start_line, token, md_metadata):
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 @@ def _flush_markdown(start_line, token, md_metadata):
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 @@ def _read_cell_metadata(token, cell_index):
)
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

0 comments on commit 1d83433

Please sign in to comment.