Enable flake8-builtins Ruff rule.

This commit is contained in:
Ilya Simpson 2025-06-01 19:04:45 +12:00
commit f44ec52427
7 changed files with 49 additions and 43 deletions

View file

@ -34,7 +34,7 @@ project = project_data.get("name").upper()
year = datetime.datetime.fromtimestamp(
int(os.environ.get("SOURCE_DATE_EPOCH", time.time())), datetime.timezone.utc
).year
copyright = f"2010{year}" # noqa: RUF001
project_copyright = f"2010{year}" # noqa: RUF001
exclude_patterns = ["_build"]
release = project_data.get("version")
version = ".".join(release.split(".")[:1])

View file

@ -122,8 +122,8 @@ class ComplexHTTPRequestHandler(server.SimpleHTTPRequestHandler):
return mimetype
def log_message(self, format, *args):
logger.info(format, *args)
def log_message(self, msg_format, *args):
logger.info(msg_format, *args)
class RootedHTTPServer(server.HTTPServer):

View file

@ -273,14 +273,14 @@ class PluginTest(unittest.TestCase):
expected = []
for i in range(50):
# function appends value of i to a list
def func(input, i=i):
input.append(i)
def func(dummy_input, i=i):
dummy_input.append(i)
functions.append(func)
# we expect functions to be run in the connection order
dummy_signal.connect(func)
expected.append(i)
input = []
dummy_signal.send(input)
self.assertEqual(input, expected)
dummy_input = []
dummy_signal.send(dummy_input)
self.assertEqual(dummy_input, expected)

View file

@ -998,28 +998,32 @@ class TestFileChangeFilter(unittest.TestCase):
ignore_file_patterns = DEFAULT_CONFIG["IGNORE_FILES"]
def test_regular_files_not_filtered(self):
filter = utils.FileChangeFilter(ignore_file_patterns=self.ignore_file_patterns)
file_change_filter = utils.FileChangeFilter(
ignore_file_patterns=self.ignore_file_patterns
)
basename = "article.rst"
full_path = os.path.join(os.path.dirname(__file__), "content", basename)
for change in watchfiles.Change:
self.assertTrue(filter(change=change, path=basename))
self.assertTrue(filter(change=change, path=full_path))
self.assertTrue(file_change_filter(change=change, path=basename))
self.assertTrue(file_change_filter(change=change, path=full_path))
def test_dotfiles_filtered(self):
filter = utils.FileChangeFilter(ignore_file_patterns=self.ignore_file_patterns)
file_change_filter = utils.FileChangeFilter(
ignore_file_patterns=self.ignore_file_patterns
)
basename = ".config"
full_path = os.path.join(os.path.dirname(__file__), "content", basename)
# Testing with just the hidden file name and the full file path to the hidden file
for change in watchfiles.Change:
self.assertFalse(filter(change=change, path=basename))
self.assertFalse(filter(change=change, path=full_path))
self.assertFalse(file_change_filter(change=change, path=basename))
self.assertFalse(file_change_filter(change=change, path=full_path))
def test_default_filters(self):
# Testing a subset of the default filters
# For reference: https://watchfiles.helpmanual.io/api/filters/#watchfiles.DefaultFilter.ignore_dirs
filter = utils.FileChangeFilter(ignore_file_patterns=[])
file_change_filter = utils.FileChangeFilter(ignore_file_patterns=[])
test_basenames = [
"__pycache__",
".git",
@ -1040,20 +1044,20 @@ class TestFileChangeFilter(unittest.TestCase):
for basename in test_basenames:
full_path = os.path.join(os.path.dirname(__file__), basename)
for change in watchfiles.Change:
self.assertFalse(filter(change=change, path=basename))
self.assertFalse(filter(change=change, path=full_path))
self.assertFalse(file_change_filter(change=change, path=basename))
self.assertFalse(file_change_filter(change=change, path=full_path))
def test_custom_ignore_pattern(self):
filter = utils.FileChangeFilter(ignore_file_patterns=["*.rst"])
file_change_filter = utils.FileChangeFilter(ignore_file_patterns=["*.rst"])
basename = "article.rst"
full_path = os.path.join(os.path.dirname(__file__), basename)
for change in watchfiles.Change:
self.assertFalse(filter(change=change, path=basename))
self.assertFalse(filter(change=change, path=full_path))
self.assertFalse(file_change_filter(change=change, path=basename))
self.assertFalse(file_change_filter(change=change, path=full_path))
# If the user changes `IGNORE_FILES` to only contain ['*.rst'], then dotfiles would not be filtered anymore
basename = ".config"
full_path = os.path.join(os.path.dirname(__file__), basename)
for change in watchfiles.Change:
self.assertTrue(filter(change=change, path=basename))
self.assertTrue(filter(change=change, path=full_path))
self.assertTrue(file_change_filter(change=change, path=basename))
self.assertTrue(file_change_filter(change=change, path=full_path))

View file

@ -320,7 +320,7 @@ def dc2fields(file):
# post_id = fields[0][1:]
# blog_id = fields[1]
# user_id = fields[2]
cat_id = fields[3]
cat_ids = fields[3]
# post_dt = fields[4]
# post_tz = fields[5]
post_creadt = fields[6]
@ -354,8 +354,10 @@ def dc2fields(file):
categories = []
tags = []
if cat_id:
categories = [category_list[id].strip() for id in cat_id.split(",")]
if cat_ids:
categories = [
category_list[cat_id].strip() for cat_id in cat_ids.split(",")
]
# Get tags related to a post
tag = (
@ -453,11 +455,11 @@ def tumblr2fields(api_key, blogname):
).strftime("%Y-%m-%d-")
+ slug
)
format = post.get("format")
post_format = post.get("format")
content = post.get("body")
type = post.get("type")
if type == "photo":
if format == "markdown":
post_type = post.get("type")
if post_type == "photo":
if post_format == "markdown":
fmtstr = "![%s](%s)"
else:
fmtstr = '<img alt="%s" src="%s" />'
@ -466,20 +468,20 @@ def tumblr2fields(api_key, blogname):
% (photo.get("caption"), photo.get("original_size").get("url"))
for photo in post.get("photos")
)
elif type == "quote":
if format == "markdown":
elif post_type == "quote":
if post_format == "markdown":
fmtstr = "\n\n&mdash; %s"
else:
fmtstr = "<p>&mdash; %s</p>"
content = post.get("text") + fmtstr % post.get("source")
elif type == "link":
if format == "markdown":
elif post_type == "link":
if post_format == "markdown":
fmtstr = "[via](%s)\n\n"
else:
fmtstr = '<p><a href="%s">via</a></p>\n'
content = fmtstr % post.get("url") + post.get("description")
elif type == "audio":
if format == "markdown":
elif post_type == "audio":
if post_format == "markdown":
fmtstr = "[via](%s)\n\n"
else:
fmtstr = '<p><a href="%s">via</a></p>\n'
@ -488,8 +490,8 @@ def tumblr2fields(api_key, blogname):
+ post.get("caption")
+ post.get("player")
)
elif type == "video":
if format == "markdown":
elif post_type == "video":
if post_format == "markdown":
fmtstr = "[via](%s)\n\n"
else:
fmtstr = '<p><a href="%s">via</a></p>\n'
@ -506,7 +508,7 @@ def tumblr2fields(api_key, blogname):
else:
players = "\n".join(players)
content = source + caption + players
elif type == "answer":
elif post_type == "answer":
title = post.get("question")
content = (
"<p>"
@ -531,11 +533,11 @@ def tumblr2fields(api_key, blogname):
slug,
date,
post.get("blog_name"),
[type],
[post_type],
tags,
status,
kind,
format,
post_format,
)
offset += len(posts)

View file

@ -125,7 +125,7 @@ select = [
"F", # pyflakes
# the rest in alphabetical order:
# TODO: "A", # flake8-builtins
"A", # flake8-builtins
# TODO: "ARG", # flake8-unused-arguments
"B", # flake8-bugbear
# TODO: "BLE", # flake8-blind-except

View file

@ -56,7 +56,7 @@ def coverage(c):
@task
def format(c, check=False, diff=False):
def formatcode(c, check=False, diff=False):
"""Run Ruff's auto-formatter, optionally with --check or --diff"""
check_flag, diff_flag = "", ""
if check:
@ -83,7 +83,7 @@ def ruff(c, fix=False, diff=False):
def lint(c, fix=False, diff=False):
"""Check code style via linting tools."""
ruff(c, fix=fix, diff=diff)
format(c, check=not fix, diff=diff)
formatcode(c, check=not fix, diff=diff)
@task