Skip to content

Commit

Permalink
Merge pull request #1055 from tefra/fix-1054
Browse files Browse the repository at this point in the history
fix: Use ruff to enforce double quotes on patterns
  • Loading branch information
tefra authored Jun 24, 2024
2 parents 1f42a6a + bf986e4 commit f75e9fa
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 7 deletions.
4 changes: 2 additions & 2 deletions tests/formats/dataclass/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ def test_field_definition_with_restriction_pattern(self, mock_field_default_valu
"field(\n"
" default=None,\n"
" metadata={\n"
' "pattern": r"([^\\ \\? > < \\* / \\" \\": |]{1,256})",\n'
' "pattern": r\'([^\\ \\? > < \\* / " ": |]{1,256})\',\n'
" }\n"
" )"
)
Expand Down Expand Up @@ -953,7 +953,7 @@ def test_format_metadata(self):
' "text": "foo",\n'
' "text_two": "fo\'o",\n'
' "text_three": "fo\\"o",\n'
' "pattern": r"foo",\n'
" \"pattern\": r'foo',\n"
' "custom": Telephone(country_code=30, area_code=123, number=4567),\n'
' "level_two": {\n'
' "a": 1,\n'
Expand Down
6 changes: 1 addition & 5 deletions xsdata/formats/dataclass/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class Filters:

DEFAULT_KEY = "default"
FACTORY_KEY = "default_factory"
UNESCAPED_DBL_QUOTE_REGEX = re.compile(r"([^\\])\"")

__slots__ = (
"substitutions",
Expand Down Expand Up @@ -569,10 +568,7 @@ def format_string(self, data: str, indent: int, key: str = "", pad: int = 0) ->
return data

if key == "pattern":
# escape double quotes because double quotes surround the regex string
# in the rendered output
value = self.UNESCAPED_DBL_QUOTE_REGEX.sub(r'\1\\"', data)
return f'r"{value}"'
return f"r{repr(data)}".replace("\\\\", "\\")

if data == "":
return '""'
Expand Down

0 comments on commit f75e9fa

Please sign in to comment.