From 9a5bba49ccd156de9037996012be5f57c91f06d4 Mon Sep 17 00:00:00 2001 From: Dunedan Date: Fri, 13 Sep 2024 16:27:12 +0200 Subject: [PATCH] Enable various ruff rules This commit enables a bunch of unrelated ruff rules, which only require minimal changes to the code base to enable them. The rules enabled by this commit are: - check the use of datetime objects without timezone (DTZ005) - check the performance of try-except in loops (PERF203) - check the number of function arguments (PLR0913) - check for mutable class defaults (RUF012) - check for the use of secure hashing algorithms (S324) - check for raising base exceptions (TRY002) - check for raising other exceptions where TypeErrors should be raised (TRY004) --- .gitea/CODEOWNERS | 1 + ruff.toml | 10 +++------- source/tools/fontbuilder2/font_loader.py | 5 ++++- source/tools/i18n/i18n_helper/catalog.py | 4 ++-- source/tools/spirv/compile.py | 2 +- source/tools/xmlvalidator/validate_grammar.py | 6 ++---- 6 files changed, 13 insertions(+), 15 deletions(-) diff --git a/.gitea/CODEOWNERS b/.gitea/CODEOWNERS index 36fb0110e8..136c766673 100644 --- a/.gitea/CODEOWNERS +++ b/.gitea/CODEOWNERS @@ -3,6 +3,7 @@ \\.gitea/.* @Stan @Itms ## Linting \\.pre-commit-config\\.yaml @Dunedan +ruff\\.toml @Dunedan ## == Build & Libraries (build|libraries)/.* @Itms @Stan diff --git a/ruff.toml b/ruff.toml index 610e22be18..4d7e8cef79 100644 --- a/ruff.toml +++ b/ruff.toml @@ -10,36 +10,29 @@ ignore = [ "C90", "COM812", "D10", - "DTZ005", "EM", "FA", "FIX", "FBT", "ISC001", "N817", - "PERF203", "PERF401", "PLR0912", - "PLR0913", "PLR0915", "PLR2004", "PLW2901", "PT", "PTH", - "RUF012", "S101", "S310", "S314", - "S324", "S320", "S603", "S607", "T20", "TD002", "TD003", - "TRY002", "TRY003", - "TRY004", "UP038", "W505" ] @@ -52,3 +45,6 @@ max-doc-length = 72 [lint.pydocstyle] convention = "pep257" + +[lint.pylint] +max-args = 8 diff --git a/source/tools/fontbuilder2/font_loader.py b/source/tools/fontbuilder2/font_loader.py index 2eb92b8bd9..3c78e421cb 100644 --- a/source/tools/fontbuilder2/font_loader.py +++ b/source/tools/fontbuilder2/font_loader.py @@ -1,7 +1,10 @@ # Adapted from http://cairographics.org/freetypepython/ +# ruff: noqa: TRY002 + import ctypes import sys +from typing import ClassVar import cairo @@ -40,7 +43,7 @@ _surface = cairo.ImageSurface(cairo.FORMAT_A8, 0, 0) class PycairoContext(ctypes.Structure): - _fields_ = [ + _fields_: ClassVar = [ ("PyObject_HEAD", ctypes.c_byte * object.__basicsize__), ("ctx", ctypes.c_void_p), ("base", ctypes.c_void_p), diff --git a/source/tools/i18n/i18n_helper/catalog.py b/source/tools/i18n/i18n_helper/catalog.py index ce72d16fc9..f0bbcbd11b 100644 --- a/source/tools/i18n/i18n_helper/catalog.py +++ b/source/tools/i18n/i18n_helper/catalog.py @@ -1,6 +1,6 @@ """Wrapper around babel Catalog / .po handling.""" -from datetime import datetime +from datetime import datetime, timezone from babel.messages.catalog import Catalog as BabelCatalog from babel.messages.pofile import read_po, write_po @@ -10,7 +10,7 @@ class Catalog(BabelCatalog): """Wraps a BabelCatalog for convenience.""" def __init__(self, *args, project=None, copyright_holder=None, **other_kwargs): - date = datetime.now() + date = datetime.now(tz=timezone.utc) super().__init__( *args, header_comment=( diff --git a/source/tools/spirv/compile.py b/source/tools/spirv/compile.py index b3eba12257..d890ea9b4f 100755 --- a/source/tools/spirv/compile.py +++ b/source/tools/spirv/compile.py @@ -47,7 +47,7 @@ def execute(command): def calculate_hash(path): assert os.path.isfile(path) with open(path, "rb") as handle: - return hashlib.sha1(handle.read()).hexdigest() + return hashlib.sha256(handle.read()).hexdigest() def compare_spirv(path1, path2): diff --git a/source/tools/xmlvalidator/validate_grammar.py b/source/tools/xmlvalidator/validate_grammar.py index c7e10426d3..48007cde00 100755 --- a/source/tools/xmlvalidator/validate_grammar.py +++ b/source/tools/xmlvalidator/validate_grammar.py @@ -198,10 +198,8 @@ class RelaxNGValidator: relaxng = lxml.etree.RelaxNG(data) error_count = 0 for file in sorted(files): - try: - doc = lxml.etree.parse(str(file[1])) - relaxng.assertValid(doc) - except Exception: + doc = lxml.etree.parse(str(file[1])) + if not relaxng.validate(doc): error_count = error_count + 1 self.logger.exception(file[1])