From c8c39e8e31606382aaaf99ee3a05bb80e3123ad4 Mon Sep 17 00:00:00 2001 From: Claas Date: Sat, 23 Nov 2024 14:08:08 +0100 Subject: [PATCH] resolved issues raise by `ruff` --- CHANGELOG.md | 3 ++- ruff.toml | 2 -- src/dictIO/dict.py | 2 +- src/dictIO/dict_reader.py | 2 +- src/dictIO/formatter.py | 8 ++++---- src/dictIO/parser.py | 38 ++++++++++++++++++++----------------- src/dictIO/utils/counter.py | 2 +- src/dictIO/utils/strings.py | 2 +- 8 files changed, 31 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ec792fe..d9a11f81 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,8 @@ The changelog format is based on [Keep a Changelog](https://keepachangelog.com/e ## [Unreleased] --/- +### Solved +* Resolved issues raised by `ruff` ## [0.4.0] - 2024-11-11 diff --git a/ruff.toml b/ruff.toml index 8d4d7748..3e9ca73f 100644 --- a/ruff.toml +++ b/ruff.toml @@ -27,8 +27,6 @@ ignore = [ "PLR0915", # Too many statements <- @TODO: reactivate and resolve @CLAROS, 2024-10-11 # Ruff lint rules considered as too strict and hence ignored - "ANN101", # Missing type annotation for `self` argument in instance methods (NOTE: also listed as deprecated by Ruff) - "ANN102", # Missing type annotation for `cls` argument in class methods (NOTE: also listed as deprecated by Ruff) "FIX002", # Line contains TODO, consider resolving the issue "TD003", # Missing issue link on the line following a TODO "S101", # Use of assert detected diff --git a/src/dictIO/dict.py b/src/dictIO/dict.py index 6a97e3c5..2d6939e4 100644 --- a/src/dictIO/dict.py +++ b/src/dictIO/dict.py @@ -614,7 +614,7 @@ def include(self, dict_to_include: SDict[_K, _V]) -> None: placeholder: str = "" while True: ii = self.counter() - placeholder = "INCLUDE%06i" % ii + placeholder = f"INCLUDE{ii:06}" if placeholder in self: continue break diff --git a/src/dictIO/dict_reader.py b/src/dictIO/dict_reader.py index 8407f7c5..495dcb76 100644 --- a/src/dictIO/dict_reader.py +++ b/src/dictIO/dict_reader.py @@ -21,7 +21,7 @@ log, log10, pi, - pow, + pow, # noqa: A004 sin, sqrt, tan, diff --git a/src/dictIO/formatter.py b/src/dictIO/formatter.py index a3b1afda..e7430ce6 100644 --- a/src/dictIO/formatter.py +++ b/src/dictIO/formatter.py @@ -794,7 +794,7 @@ def insert_block_comments( # Search for the placeholder entry we created in _parse_tokenized_dict(), # and insert back the original block_comment. - search_pattern = r"BLOCKCOMMENT%06i\s+BLOCKCOMMENT%06i;" % (key, key) + search_pattern = rf"BLOCKCOMMENT{key:06d}\s+BLOCKCOMMENT{key:06d};" if ( len(re.findall(search_pattern, s)) > 0 ): # if placeholders exist in s that match the key of the current block_comment @@ -836,7 +836,7 @@ def insert_includes( _include_file_name = include_file_name.replace("\\", "\\\\") _include_file_name = self.format_value(_include_file_name) _include_directive = f"#include {_include_file_name}" - search_pattern = r"INCLUDE%06i\s+INCLUDE%06i;" % (key, key) + search_pattern = rf"INCLUDE{key:06d}\s+INCLUDE{key:06d};" s = re.sub(search_pattern, _include_directive, s) return s @@ -851,7 +851,7 @@ def insert_line_comments( for key, line_comment in s_dict.line_comments.items(): # Search for the placeholder entry we created in _parse_tokenized_dict(), # and insert back the original block_comment. - search_pattern = r"LINECOMMENT%06i\s+LINECOMMENT%06i;" % (key, key) + search_pattern = rf"LINECOMMENT{key:06d}\s+LINECOMMENT{key:06d};" s = re.sub(search_pattern, line_comment, s) return s @@ -1073,7 +1073,7 @@ def insert_includes( # and insert back the original include directive. _include_file_name = include_file_name.replace("\\", "\\\\\\\\") _include_directive = f'"#include{key:06d}":"{_include_file_name}"' - search_pattern = r'"INCLUDE%06i"\s*:\s*"INCLUDE%06i"' % (key, key) + search_pattern = rf'"INCLUDE{key:06d}"\s*:\s*"INCLUDE{key:06d}"' s = re.sub(search_pattern, _include_directive, s) return s diff --git a/src/dictIO/parser.py b/src/dictIO/parser.py index b62d478f..14dbb165 100644 --- a/src/dictIO/parser.py +++ b/src/dictIO/parser.py @@ -514,7 +514,7 @@ def _extract_line_comments( # From there, consider all chars until line ending as ONE comment. line_comment = re.findall("/{2}.*$", line)[0] s_dict.line_comments.update({key: line_comment}) - placeholder = "LINECOMMENT%06i" % key + placeholder = f"LINECOMMENT{key:06d}" if not comments: placeholder = "" # Replace line comment with placeholder @@ -545,7 +545,7 @@ def _extract_includes( for index, line in enumerate(s_dict.line_content): if re.search(r"^\s*#\s*include", line): ii = self.counter() - s_dict.line_content[index] = "INCLUDE%06i\n" % ii + s_dict.line_content[index] = f"INCLUDE{ii:06d}\n" include_file_name = re.sub( r"(^\s*#\s*include\s*|\s*$)", @@ -614,7 +614,7 @@ def _extract_block_comments( s_dict.block_comments = {i: block_comments[i] for i in range(len(block_comments))} for key, block_comment in s_dict.block_comments.items(): - placeholder = "BLOCKCOMMENT%06i" % key + placeholder = f"BLOCKCOMMENT{key:06d}" if not comments: placeholder = "" # Replace block comment with placeholder @@ -728,7 +728,7 @@ def _extract_string_literals( ) for string_literal in string_literals: index = self.counter() - placeholder = "STRINGLITERAL%06i" % index + placeholder = f"STRINGLITERAL{index:06d}" # Replace all occurances of the string literal in .block_content with the placeholder (STRINGLITERAL000000) # Note: For re.sub() to work properly we need to escape all special characters @@ -773,7 +773,7 @@ def _extract_expressions( expressions = re.findall(search_pattern, s_dict.block_content, re.MULTILINE) for expression in expressions: index = self.counter() - placeholder = "EXPRESSION%06i" % index + placeholder = f"EXPRESSION{index:06d}" # Replace all occurances of the expression in .block_content with the placeholder (EXPRESSION000000) # Note: For re.sub() to work properly we need to escape all special characters @@ -799,7 +799,7 @@ def _extract_expressions( while match := re.search(search_pattern, s_dict.block_content, re.MULTILINE): reference = match[0] index = self.counter() - placeholder = "EXPRESSION%06i" % index + placeholder = f"EXPRESSION{index:06d}" # Replace the found reference in .block_content with the placeholder (EXPRESSION000000) s_dict.block_content = match.re.sub(placeholder, s_dict.block_content, count=1) # Register the reference as expression in .expressions @@ -975,10 +975,12 @@ def _parse_tokenized_dict( last_index = None # Start at opening bracket, go forward and copy the tokens # until (and including) the accompanied closing bracket. - while ( - tokens[token_index + i][1] != closing_bracket - or tokens[token_index + i][0] != closing_level - and not re.match("^.*COMMENT.*$", str(tokens[token_index + i][1])) + while tokens[token_index + i][1] != closing_bracket or ( + tokens[token_index + i][0] != closing_level + and not re.match( + pattern="^.*COMMENT.*$", + string=str(tokens[token_index + i][1]), + ) ): last_index = token_index + i data_struct_tokens.append(tokens[token_index + i]) @@ -1182,10 +1184,12 @@ def _parse_tokenized_list( last_index = None # Start at opening bracket, go forward and copy the tokens # until (and including) the accompanied closing bracket - while ( - tokens[token_index + i][1] != closing_bracket - or tokens[token_index + i][0] != closing_level - and not re.match("^.*COMMENT.*$", str(tokens[token_index + i][1])) + while tokens[token_index + i][1] != closing_bracket or ( + tokens[token_index + i][0] != closing_level + and not re.match( + pattern="^.*COMMENT.*$", + string=str(tokens[token_index + i][1]), + ) ): last_index = token_index + i temp_tokens.append(tokens[token_index + i]) @@ -1294,7 +1298,7 @@ def _insert_string_literals( """Substitutes STRINGLITERAL placeholders in the dict with the corresponding entry from dict.string_literals.""" for index, string_literal in s_dict.string_literals.items(): # Properties of the expression to be evaluated - placeholder = "STRINGLITERAL%06i" % index # STRINGLITERAL000000 + placeholder = f"STRINGLITERAL{index:06d}" # STRINGLITERAL000000 # The entry from dict.string_literals is parsed once again, # so that entries representing single value native types # (such as bool ,None, int, float) are transformed to its native type, accordingly. @@ -1535,7 +1539,7 @@ def _replace_and_register_expression( with a placeholder (EXPRESSION000000) """ index: int = self.counter() - placeholder: str = "EXPRESSION%06i" % index + placeholder: str = f"EXPRESSION{index:06d}" # Note: For re.sub() to work properly we need to escape all special characters # (covering both '$' as well as any mathematical operators in the expression) _pattern: re.Pattern[str] = re.compile(re.escape(expression)) @@ -1726,7 +1730,7 @@ def _parse_nodes( node_tag: str for node_tag in node_tags: index = self.counter() - indexed_node_tags.append(("%06i_%s" % (index, node_tag), node_tag)) + indexed_node_tags.append((f"{index:06d}_{node_tag}", node_tag)) key: TKey parsed_dict: dict[TKey, TValue] = {} diff --git a/src/dictIO/utils/counter.py b/src/dictIO/utils/counter.py index 61b36ca0..cedb4074 100644 --- a/src/dictIO/utils/counter.py +++ b/src/dictIO/utils/counter.py @@ -3,7 +3,7 @@ # pyright: reportUnnecessaryTypeIgnoreComment=false from typing import Any, ClassVar -__all__ = ["BorgCounter", "Indenter", "DejaVue"] +__all__ = ["BorgCounter", "DejaVue", "Indenter"] class BorgCounter: diff --git a/src/dictIO/utils/strings.py b/src/dictIO/utils/strings.py index 43e0219f..7967f608 100644 --- a/src/dictIO/utils/strings.py +++ b/src/dictIO/utils/strings.py @@ -35,7 +35,7 @@ def string_diff(text_1: str, text_2: str) -> str: message: str = "" for index, item in enumerate(line for line in ndiff(lines_1, lines_2) if not re.search(r"^\s*$", line)): if re.match(r"^[+\-]", item): - message += str.format("diff in line %4i:" % index) + "\n" + message += f"diff in line {index:4d}:" + "\n" message += item + "\n" diffs.append(item)