Skip to content

Commit

Permalink
resolved issues raise by ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
ClaasRostock committed Nov 23, 2024
1 parent e73dbd2 commit c8c39e8
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 28 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/dictIO/dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/dictIO/dict_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
log,
log10,
pi,
pow,
pow, # noqa: A004
sin,
sqrt,
tan,
Expand Down
8 changes: 4 additions & 4 deletions src/dictIO/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
38 changes: 21 additions & 17 deletions src/dictIO/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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*$)",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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] = {}
Expand Down
2 changes: 1 addition & 1 deletion src/dictIO/utils/counter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# pyright: reportUnnecessaryTypeIgnoreComment=false
from typing import Any, ClassVar

__all__ = ["BorgCounter", "Indenter", "DejaVue"]
__all__ = ["BorgCounter", "DejaVue", "Indenter"]


class BorgCounter:
Expand Down
2 changes: 1 addition & 1 deletion src/dictIO/utils/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit c8c39e8

Please sign in to comment.