From bf986e402f320a9de688e70c617416b05f39cd61 Mon Sep 17 00:00:00 2001 From: Christodoulos Tsoulloftas Date: Mon, 24 Jun 2024 07:52:56 +0300 Subject: [PATCH] fix: Use ruff to enforce double quotes on patterns --- tests/formats/dataclass/test_filters.py | 4 ++-- xsdata/formats/dataclass/filters.py | 6 +----- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/tests/formats/dataclass/test_filters.py b/tests/formats/dataclass/test_filters.py index 42f870826..ebd10c60d 100644 --- a/tests/formats/dataclass/test_filters.py +++ b/tests/formats/dataclass/test_filters.py @@ -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" " )" ) @@ -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' diff --git a/xsdata/formats/dataclass/filters.py b/xsdata/formats/dataclass/filters.py index 5c711cede..32715c88f 100644 --- a/xsdata/formats/dataclass/filters.py +++ b/xsdata/formats/dataclass/filters.py @@ -40,7 +40,6 @@ class Filters: DEFAULT_KEY = "default" FACTORY_KEY = "default_factory" - UNESCAPED_DBL_QUOTE_REGEX = re.compile(r"([^\\])\"") __slots__ = ( "substitutions", @@ -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 '""'