Skip to content

Commit

Permalink
Merge pull request #3318 from boxydog/more_ruff_fixes3
Browse files Browse the repository at this point in the history
  • Loading branch information
justinmayer authored May 31, 2024
2 parents ccb4e58 + 9b77a90 commit 2687475
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 54 deletions.
72 changes: 36 additions & 36 deletions pelican/tests/test_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,15 @@ def test_ignore_empty_posts(self):
self.assertTrue(self.posts)
for (
title,
content,
fname,
date,
author,
categ,
tags,
status,
kind,
format,
_content,
_fname,
_date,
_author,
_categ,
_tags,
_status,
_kind,
_format,
) in self.posts:
self.assertTrue(title.strip())

Expand All @@ -127,15 +127,15 @@ def test_recognise_page_kind(self):
pages_data = []
for (
title,
content,
_content,
fname,
date,
author,
categ,
tags,
status,
_date,
_author,
_categ,
_tags,
_status,
kind,
format,
_format,
) in self.posts:
if kind == "page":
pages_data.append((title, fname))
Expand All @@ -147,7 +147,7 @@ def test_dirpage_directive_for_page_kind(self):
silent_f2p = mute(True)(fields2pelican)
test_post = filter(lambda p: p[0].startswith("Empty Page"), self.posts)
with temporary_folder() as temp:
fname = list(silent_f2p(test_post, "markdown", temp, dirpage=True))[0]
fname = next(iter(silent_f2p(test_post, "markdown", temp, dirpage=True)))
self.assertTrue(fname.endswith("pages%sempty.md" % os.path.sep))

def test_dircat(self):
Expand Down Expand Up @@ -176,15 +176,15 @@ def test_unless_custom_post_all_items_should_be_pages_or_posts(self):
pages_data = []
for (
title,
content,
_content,
fname,
date,
author,
categ,
tags,
status,
_date,
_author,
_categ,
_tags,
_status,
kind,
format,
_format,
) in self.posts:
if kind in {"page", "article"}:
pass
Expand All @@ -197,15 +197,15 @@ def test_recognise_custom_post_type(self):
cust_data = []
for (
title,
content,
fname,
date,
author,
categ,
tags,
status,
_content,
_fname,
_date,
_author,
_categ,
_tags,
_status,
kind,
format,
_format,
) in self.custposts:
if kind in {"page", "article"}:
pass
Expand Down Expand Up @@ -346,7 +346,7 @@ def r(f):
silent_f2p = mute(True)(fields2pelican)
test_post = filter(lambda p: p[0].startswith("Code in List"), self.posts)
with temporary_folder() as temp:
md = [r(f) for f in silent_f2p(test_post, "markdown", temp)][0]
md = next(r(f) for f in silent_f2p(test_post, "markdown", temp))
self.assertTrue(re.search(r"\s+a = \[1, 2, 3\]", md))
self.assertTrue(re.search(r"\s+b = \[4, 5, 6\]", md))

Expand All @@ -362,7 +362,7 @@ def r(f):
silent_f2p = mute(True)(fields2pelican)
test_post = filter(lambda p: p[0].startswith("Code in List"), self.posts)
with temporary_folder() as temp:
md = [r(f) for f in silent_f2p(test_post, "markdown", temp)][0]
md = next(r(f) for f in silent_f2p(test_post, "markdown", temp))
sample_line = re.search(r"- This is a code sample", md).group(0)
code_line = re.search(r"\s+a = \[1, 2, 3\]", md).group(0)
self.assertTrue(sample_line.rindex("This") < code_line.rindex("a"))
Expand All @@ -375,7 +375,7 @@ def r(f):
silent_f2p = mute(True)(fields2pelican)
test_post = filter(lambda p: p[0].startswith("Post with raw data"), self.posts)
with temporary_folder() as temp:
md = [r(f) for f in silent_f2p(test_post, "markdown", temp)][0]
md = next(r(f) for f in silent_f2p(test_post, "markdown", temp))
escaped_quotes = re.search(r'\\[\'"“”‘’]', md)
self.assertFalse(escaped_quotes)

Expand All @@ -387,7 +387,7 @@ def r(f):
silent_f2p = mute(True)(fields2pelican)
test_post = filter(lambda p: p[0].startswith("Caption on image"), self.posts)
with temporary_folder() as temp:
md = [r(f) for f in silent_f2p(test_post, "markdown", temp)][0]
md = next(r(f) for f in silent_f2p(test_post, "markdown", temp))

caption = re.search(r"\[caption", md)
self.assertFalse(caption)
Expand Down
11 changes: 4 additions & 7 deletions pelican/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,10 @@ def test_slugify_use_unicode(self):
)

# check with preserve case
for value, expected in samples:
self.assertEqual(
utils.slugify(
"Çığ", regex_subs=subs, preserve_case=True, use_unicode=True
),
"Çığ",
)
self.assertEqual(
utils.slugify("Çığ", regex_subs=subs, preserve_case=True, use_unicode=True),
"Çığ",
)

# check normalization
samples = (
Expand Down
12 changes: 6 additions & 6 deletions pelican/tools/pelican_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -1070,14 +1070,14 @@ def fields2pelican(
rc = subprocess.call(cmd, shell=True)
if rc < 0:
error = "Child was terminated by signal %d" % -rc
exit(error)
sys.exit(error)

elif rc > 0:
error = "Please, check your Pandoc installation."
exit(error)
sys.exit(error)
except OSError as e:
error = "Pandoc execution failed: %s" % e
exit(error)
sys.exit(error)

with open(out_filename, encoding="utf-8") as fs:
content = fs.read()
Expand Down Expand Up @@ -1222,18 +1222,18 @@ def main():
"You must provide one of --blogger, --dotclear, "
"--medium, --tumblr, --wpfile or --feed options"
)
exit(error)
sys.exit(error)

if not os.path.exists(args.output):
try:
os.mkdir(args.output)
except OSError:
error = "Unable to create the output folder: " + args.output
exit(error)
sys.exit(error)

if args.wp_attach and input_type != "wordpress":
error = "You must be importing a wordpress xml to use the --wp-attach option"
exit(error)
sys.exit(error)

if input_type == "blogger":
fields = blogger2fields(args.input)
Expand Down
2 changes: 1 addition & 1 deletion pelican/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ def feed(self, *args, **kwargs) -> None:
def getoffset(self) -> int:
line_start = 0
lineno, line_offset = self.getpos()
for i in range(lineno - 1):
for _ in range(lineno - 1):
line_start = self.rawdata.index("\n", line_start) + 1
return line_start + line_offset

Expand Down
2 changes: 1 addition & 1 deletion pelican/writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def _get_localcontext(context, name, kwargs, relative_urls):
}

# generated pages, and write
for page_num in range(list(paginators.values())[0].num_pages):
for page_num in range(next(iter(paginators.values())).num_pages):
paginated_kwargs = kwargs.copy()
for key in paginators.keys():
paginator = paginators[key]
Expand Down
11 changes: 8 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -168,20 +168,25 @@ select = [

ignore = [
# suppression in order of # of violations in Dec 2023:
"B007", # unused-loop-control-variable
"T201", # print
"PLW2901", # redefined-loop-name
"SLF001", # private-member-access
"RUF001", # ambiguous-unicode-character-string
"PLR2004", # magic-value-comparison
"PLR0912", # too-many-branches
"PLR0913", # too-many-arguments
# RUF005: this is a different style of concatenating literals. It perhaps performs
# a bit better, but doesn't seem any more readable to me. So, ignore it.
"RUF005", # collection-literal-concatenation
# TODO: several classes have class variables. If that is correct, we should
# annotate them with ClassVar.
# See https://docs.astral.sh/ruff/rules/mutable-class-default/
"RUF012", # mutable-class-default
"PLR0915", # too-many-statements
# Note: we have a couple of "namespace packages" (i.e. missing __init__.py)
# Not sure if we should add __init__.py to them, or they really need to be
# namespace packages.
"INP001", # implicit-namespace-package
"RUF015", # unnecessary-iterable-allocation-for-first-element
"PLR1722", # sys-exit-alias
# ruff-format wants us to ignore ISC001. I don't love that, but okay.
# "warning: The following rules may cause conflicts when used with the formatter:
# `ISC001`. To avoid unexpected behavior, we recommend disabling these rules,
Expand Down

0 comments on commit 2687475

Please sign in to comment.