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( year = datetime.datetime.fromtimestamp(
int(os.environ.get("SOURCE_DATE_EPOCH", time.time())), datetime.timezone.utc int(os.environ.get("SOURCE_DATE_EPOCH", time.time())), datetime.timezone.utc
).year ).year
copyright = f"2010{year}" # noqa: RUF001 project_copyright = f"2010{year}" # noqa: RUF001
exclude_patterns = ["_build"] exclude_patterns = ["_build"]
release = project_data.get("version") release = project_data.get("version")
version = ".".join(release.split(".")[:1]) version = ".".join(release.split(".")[:1])

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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