fix(string-constructor): resolve Optional typing crash and regex warnings

- Fixed TypeError caused by invalid use of typing.Optional with two arguments.
  Replaced Optional[Dict[str, Any], None] with correct Optional[Dict[str, Any]] = None.
- Updated __escape_match() to pass integer brace counts instead of raw strings
  to _n_brackets_after_escape_for_existing_key, preventing runtime errors.
- Converted regex patterns to raw strings (r\'...\') to eliminate SyntaxWarning
  about invalid escape sequences (\\s, \\{).
This commit is contained in:
MaraScott
2025-08-29 14:09:52 +02:00
parent 200dff7c81
commit f764fefe76
2 changed files with 13 additions and 13 deletions
+9 -9
View File
@@ -8,16 +8,16 @@ from inspect import getdoc as _getdoc
import re as _re
_re_indent_match = _re.compile("(\t*)( +)(\t*)(.*?)$").match
_re_tab_indent_match = _re.compile("(\t+)(.*?)$").match
_re_indent_match = _re.compile(r"(\t*)( +)(\t*)(.*?)$").match
_re_tab_indent_match = _re.compile(r"(\t+)(.*?)$").match
_re_list_line_match = _re.compile(
"(\s*)("
"[-*•]+"
"|"
"[a-zA-Z]\s*[.)]"
"|"
"[0-9+]\s*[.)]"
")\s+"
r"(\s*)("
r"[-*•]+"
r"|"
r"[a-zA-Z]\s*[.)]"
r"|"
r"[0-9+]\s*[.)]"
r")\s+"
).match
+4 -4
View File
@@ -42,7 +42,7 @@ def _rebuild_parsed_keyword(n_opening_brackets: int, inside_brackets: str, n_clo
_re_formatting_keyword_sub = _re.compile( # Pre-compiled regex-sub func to find and replace {keyword} patterns
'(\{+)([^{}]*)(\}+)'
r'(\{+)([^{}]*)(\}+)'
# TODO: Takes leading/trailing braces into account, but not nested ones. Maybe implement some day,
# but it would require true parsing - with stack of braces, nested parts iterator, etc.
).sub
@@ -59,7 +59,7 @@ class _Formatter:
It's done this way to avoid extra conditions in the loop + to organize all the setup work in one place.
"""
format_dict: _t.Optional[_t.Dict[str, _t.Any], None] = None
format_dict: _t.Optional[_t.Dict[str, _t.Any]] = None
recursive: bool = False
safe: bool = True
@@ -88,8 +88,8 @@ class _Formatter:
def __escape_match(self, match: _re.Match[str]) -> str:
opening_brackets, inside_brackets, closing_brackets = match.groups()
if inside_brackets.strip() in self.format_dict:
n_opening = _n_brackets_after_escape_for_existing_key(opening_brackets)
n_closing = _n_brackets_after_escape_for_existing_key(closing_brackets)
n_opening = _n_brackets_after_escape_for_existing_key(len(opening_brackets))
n_closing = _n_brackets_after_escape_for_existing_key(len(closing_brackets))
return _rebuild_parsed_keyword(n_opening, inside_brackets, n_closing)
else:
return _rebuild_parsed_keyword(len(opening_brackets) * 2, inside_brackets, len(closing_brackets) * 2)