Skip to content

Commit

Permalink
Add pyupgrade to the pre-commit hook (#920)
Browse files Browse the repository at this point in the history
* Add pyupgrade to the pre-commit hooks
* Run pyupgrade with 'pre-commit run --all'
  • Loading branch information
mwouts authored Feb 9, 2022
1 parent 3803f8d commit d25c0c6
Show file tree
Hide file tree
Showing 40 changed files with 109 additions and 148 deletions.
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,9 @@ repos:
rev: 22.1.0
hooks:
- id: black

- repo: https://github.com/asottile/pyupgrade
rev: v2.31.0
hooks:
- id: pyupgrade
args: ["--py3-plus", "--py36-plus"]

This comment has been minimized.

Copy link
@Skylion007

Skylion007 Feb 14, 2022

Contributor

@mwouts --py36-plus implies --py3-plus have both is redundant.

2 changes: 2 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Jupytext ChangeLog
- We have updated the pre-commit hooks and in particular we switched to the first stable version of `black==22.1.0`.
- We require `pandoc==2.16.2` for testing. The representation for code cells changed from ` ``` {.python}` to ` ``` python` in that version of Pandoc ([#906](https://github.com/mwouts/jupytext/issues/906)). We don't use `pandoc>=2.17` in tests at the moment because of the introduction of cell ids that cannot be filtered.
- Jupytext will not add anymore a UTF-8 encoding on Python scripts when the notebook contains non-ascii characters ([#907](https://github.com/mwouts/jupytext/issues/907))
- We have added `pyupgrade` to the pre-commit hooks used for developing Jupytext ([#907](https://github.com/mwouts/jupytext/issues/907))


1.13.6 (2022-01-11)
-------------------
Expand Down
18 changes: 9 additions & 9 deletions jupytext/cell_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"lines_to_end_of_cell_marker",
]
_IGNORE_CELL_METADATA = ",".join(
"-{}".format(name)
f"-{name}"
for name in [
# Frequent cell metadata that should not enter the text representation
# (these metadata are preserved in the paired Jupyter notebook).
Expand Down Expand Up @@ -108,23 +108,23 @@ def metadata_to_rmd_options(language, metadata, use_runtools=False):
opt_value = metadata[opt_name]
opt_name = opt_name.strip()
if opt_name == "active":
options += ' {}="{}",'.format(opt_name, str(opt_value))
options += f' {opt_name}="{str(opt_value)}",'
elif isinstance(opt_value, bool):
options += " {}={},".format(opt_name, "TRUE" if opt_value else "FALSE")
elif isinstance(opt_value, list):
options += " {}={},".format(
opt_name,
"c({})".format(", ".join(['"{}"'.format(str(v)) for v in opt_value])),
"c({})".format(", ".join([f'"{str(v)}"' for v in opt_value])),
)
elif isinstance(opt_value, str):
if opt_value.startswith("#R_CODE#"):
options += " {}={},".format(opt_name, opt_value[8:])
options += f" {opt_name}={opt_value[8:]},"
elif '"' not in opt_value:
options += ' {}="{}",'.format(opt_name, opt_value)
options += f' {opt_name}="{opt_value}",'
else:
options += " {}='{}',".format(opt_name, opt_value)
options += f" {opt_name}='{opt_value}',"
else:
options += " {}={},".format(opt_name, str(opt_value))
options += f" {opt_name}={str(opt_value)},"
if not language:
options = options[2:]
return options.strip(",").strip()
Expand Down Expand Up @@ -247,7 +247,7 @@ def parse_rmd_options(line):

if not parsing_context.in_global_expression():
raise RMarkdownOptionParsingError(
'Option line "{}" is not properly terminated'.format(line)
f'Option line "{line}" is not properly terminated'
)

return result
Expand Down Expand Up @@ -512,5 +512,5 @@ def metadata_to_text(language_or_title, metadata=None, plain_json=False):
elif metadata[key] is None:
text.append(key)
else:
text.append("{}={}".format(key, dumps(metadata[key])))
text.append(f"{key}={dumps(metadata[key])}")
return " ".join(text)
16 changes: 7 additions & 9 deletions jupytext/cell_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def last_two_lines_blank(source):
)


class BaseCellReader(object):
class BaseCellReader:
"""A class that can read notebook cells from their text representation"""

default_comment_magics = None
Expand Down Expand Up @@ -353,7 +353,7 @@ class MarkdownCellReader(BaseCellReader):
default_comment_magics = False

def __init__(self, fmt=None, default_language=None):
super(MarkdownCellReader, self).__init__(fmt, default_language)
super().__init__(fmt, default_language)
self.split_at_heading = (fmt or {}).get("split_at_heading", False)
self.in_region = False
self.in_raw = False
Expand All @@ -368,9 +368,7 @@ def metadata_and_language_from_option_line(self, line):
self.in_region = True
groups = match_region.groups()
region_name = groups[0]
self.end_region_re = re.compile(
r"^<!--\s*#end{}\s*-->\s*$".format(region_name)
)
self.end_region_re = re.compile(rf"^<!--\s*#end{region_name}\s*-->\s*$")
self.cell_metadata_json = self.cell_metadata_json or is_json_metadata(
groups[1]
)
Expand Down Expand Up @@ -636,7 +634,7 @@ class LightScriptCellReader(ScriptCellReader):
cell_marker_end = None

def __init__(self, fmt=None, default_language=None):
super(LightScriptCellReader, self).__init__(fmt, default_language)
super().__init__(fmt, default_language)
self.ext = self.ext or ".py"
script = _SCRIPT_EXTENSIONS[self.ext]
self.default_language = default_language or script["language"]
Expand Down Expand Up @@ -698,7 +696,7 @@ def options_to_metadata(self, options):

# Cell type
for cell_type in ["markdown", "raw", "md"]:
code = "[{}]".format(cell_type)
code = f"[{cell_type}]"
if code in title:
title = title.replace(code, "").strip()
metadata["cell_type"] = cell_type
Expand Down Expand Up @@ -811,7 +809,7 @@ def __init__(self, fmt, default_language=None):
self.comment = script["comment"]
self.comment_suffix = script.get("comment_suffix", "")
self.start_code_re = re.compile(
r"^\s*{}\s*%%(%*)\s(.*)$".format(re.escape(self.comment))
rf"^\s*{re.escape(self.comment)}\s*%%(%*)\s(.*)$"
)
self.alternative_start_code_re = re.compile(
r"^\s*{}\s*(%%|<codecell>|In\[[0-9 ]*\]:?)\s*$".format(
Expand Down Expand Up @@ -907,7 +905,7 @@ class SphinxGalleryScriptCellReader(ScriptCellReader): # pylint: disable=W0223
markdown_marker = None

def __init__(self, fmt=None, default_language="python"):
super(SphinxGalleryScriptCellReader, self).__init__(fmt, default_language)
super().__init__(fmt, default_language)
self.ext = ".py"
self.rst2md = (fmt or {}).get("rst2md", False)

Expand Down
18 changes: 8 additions & 10 deletions jupytext/cell_to_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def three_backticks_or_more(lines):
return code_cell_delimiter


class BaseCellExporter(object):
class BaseCellExporter:
"""A class that represent a notebook cell as text"""

default_comment_magics = None
Expand Down Expand Up @@ -217,9 +217,9 @@ def html_comment(self, metadata, code="region"):
]
region_start = " ".join(region_start)
else:
region_start = "<!-- #{} -->".format(code)
region_start = f"<!-- #{code} -->"

return [region_start] + self.source + ["<!-- #end{} -->".format(code)]
return [region_start] + self.source + [f"<!-- #end{code} -->"]

def cell_to_text(self):
"""Return the text representation of a cell"""
Expand Down Expand Up @@ -284,7 +284,7 @@ def code_to_text(self):
options = metadata_to_rmd_options(
self.language, self.metadata, self.use_runtools
)
lines.append("```{{{}}}".format(options))
lines.append(f"```{{{options}}}")
lines.extend(source)
lines.append("```")
return lines
Expand All @@ -295,9 +295,7 @@ def endofcell_marker(source, comment):
we add an end-of-cell marker"""
endofcell = "-"
while True:
endofcell_re = re.compile(
r"^{}( )".format(re.escape(comment)) + endofcell + r"\s*$"
)
endofcell_re = re.compile(rf"^{re.escape(comment)}( )" + endofcell + r"\s*$")
if list(filter(endofcell_re.match, source)):
endofcell = endofcell + "-"
else:
Expand Down Expand Up @@ -339,7 +337,7 @@ def is_code(self):
self.unfiltered_metadata = copy(self.unfiltered_metadata)
self.unfiltered_metadata.pop("cell_marker", "")
return True
return super(LightScriptCellExporter, self).is_code()
return super().is_code()

def code_to_text(self):
"""Return the text representation of a code cell"""
Expand Down Expand Up @@ -380,7 +378,7 @@ def code_to_text(self):
cell_start.append(options)
lines.append(" ".join(cell_start))
lines.extend(source)
lines.append(self.comment + " {}".format(endofcell))
lines.append(self.comment + f" {endofcell}")
return lines

def explicit_start_marker(self, source):
Expand Down Expand Up @@ -469,7 +467,7 @@ def code_to_text(self):
self.metadata["eval"] = False
options = metadata_to_rmd_options(None, self.metadata, self.use_runtools)
if options:
lines.append("#+ {}".format(options))
lines.append(f"#+ {options}")
lines.extend(source)
return lines

Expand Down
39 changes: 16 additions & 23 deletions jupytext/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import argparse
import glob
import io
import json
import os
import re
Expand Down Expand Up @@ -93,20 +92,20 @@ def parse_jupytext_args(args=None):
"'md', 'Rmd', 'jl', 'py', 'R', ..., 'auto' (script extension matching the notebook language), "
"or a combination of an extension and a format name, e.g. {} ".format(
", ".join(
set(
"md:{}".format(fmt.format_name)
{
f"md:{fmt.format_name}"
for fmt in JUPYTEXT_FORMATS
if fmt.extension == ".md"
)
}
)
)
+ " or {}. ".format(
", ".join(
set(
"py:{}".format(fmt.format_name)
{
f"py:{fmt.format_name}"
for fmt in JUPYTEXT_FORMATS
if fmt.extension == ".py"
)
}
)
)
+ "The default format for scripts is the 'light' format, "
Expand Down Expand Up @@ -458,7 +457,7 @@ def fmt_if_not_ipynb(nb):
):

def single_line(msg, *args, **kwargs):
return "[warning] {}\n".format(msg)
return f"[warning] {msg}\n"

warnings.formatwarning = single_line
warnings.warn(
Expand Down Expand Up @@ -491,7 +490,7 @@ def single_line(msg, *args, **kwargs):
try:
exit_code += jupytext_single_file(nb_file, args, log)
except Exception as err:
sys.stderr.write("[jupytext] Error: {}\n".format(str(err)))
sys.stderr.write(f"[jupytext] Error: {str(err)}\n")

return exit_code

Expand All @@ -501,7 +500,7 @@ def jupytext_single_file(nb_file, args, log):
if nb_file == "-" and args.sync:
msg = "Missing notebook path."
if args.set_formats is not None and os.path.isfile(args.set_formats):
msg += " Maybe you mean 'jupytext --sync {}' ?".format(args.set_formats)
msg += f" Maybe you mean 'jupytext --sync {args.set_formats}' ?"
raise ValueError(msg)

nb_dest = None
Expand All @@ -517,9 +516,7 @@ def jupytext_single_file(nb_file, args, log):
log(
"[jupytext] Ignoring unmatched input path {}{}".format(
nb_file,
" for format {}".format(args.input_format)
if args.input_format
else "",
f" for format {args.input_format}" if args.input_format else "",
)
)
return 0
Expand Down Expand Up @@ -548,9 +545,7 @@ def jupytext_single_file(nb_file, args, log):
log(
"[jupytext] Reading {}{}".format(
nb_file if nb_file != "-" else "stdin",
" in format {}".format(short_form_one_format(fmt))
if "extension" in fmt
else "",
f" in format {short_form_one_format(fmt)}" if "extension" in fmt else "",
)
)

Expand Down Expand Up @@ -690,7 +685,7 @@ def jupytext_single_file(nb_file, args, log):
# Execute the notebook
if args.execute:
kernel_name = notebook.metadata.get("kernelspec", {}).get("name")
log("[jupytext] Executing notebook with kernel {}".format(kernel_name))
log(f"[jupytext] Executing notebook with kernel {kernel_name}")

if nb_dest is not None and nb_dest != "-":
nb_path = os.path.dirname(nb_dest)
Expand All @@ -708,9 +703,7 @@ def jupytext_single_file(nb_file, args, log):
run_path = try_path
break
if not os.path.isdir(run_path):
raise ValueError(
"--run-path={} is not a valid path".format(args.run_path)
)
raise ValueError(f"--run-path={args.run_path} is not a valid path")

if run_path:
resources = {"metadata": {"path": run_path}}
Expand Down Expand Up @@ -781,7 +774,7 @@ def jupytext_single_file(nb_file, args, log):
)

except (NotebookDifference, AssertionError) as err:
sys.stdout.write("{}: {}".format(nb_file, str(err)))
sys.stdout.write(f"{nb_file}: {str(err)}")
return 1
return 0

Expand Down Expand Up @@ -823,7 +816,7 @@ def lazy_write(path, fmt=None, action=None, update_timestamp_only=False):
if modified:
# The text representation of the notebook has changed, we write it on disk
if action is None:
message = "[jupytext] Updating {}".format(shlex.quote(path))
message = f"[jupytext] Updating {shlex.quote(path)}"
else:
message = "[jupytext] Writing {path}{format}{action}".format(
path=shlex.quote(path),
Expand All @@ -837,7 +830,7 @@ def lazy_write(path, fmt=None, action=None, update_timestamp_only=False):

log(message)
create_prefix_dir(path, fmt)
with io.open(path, "w", encoding="utf-8") as fp:
with open(path, "w", encoding="utf-8") as fp:
fp.write(new_content)

# Otherwise, we only update the timestamp of the text file to make sure
Expand Down
2 changes: 1 addition & 1 deletion jupytext/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def compare_notebooks(
)
except AssertionError as error:
if raise_on_first_difference:
raise NotebookDifference("Notebook metadata differ: {}".format(str(error)))
raise NotebookDifference(f"Notebook metadata differ: {str(error)}")
modified_metadata = True

error = []
Expand Down
7 changes: 3 additions & 4 deletions jupytext/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,10 @@ def global_jupytext_configuration_directories():
config_dirs.extend(["/usr/local/share/", "/usr/share/"])

for config_dir in config_dirs:
for config_dir_jupytext_or_not in [
yield from [
os.path.join(config_dir, "jupytext"),
config_dir,
]:
yield config_dir_jupytext_or_not
]


def find_global_jupytext_configuration_file():
Expand All @@ -323,7 +322,7 @@ def find_jupytext_configuration_file(path, search_parent_dirs=True):
if os.path.isfile(pyproject_path):
import toml

with open(pyproject_path, "r") as stream:
with open(pyproject_path) as stream:
doc = toml.loads(stream.read())
if doc.get("tool", {}).get("jupytext") is not None:
return pyproject_path
Expand Down
Loading

0 comments on commit d25c0c6

Please sign in to comment.