Skip to content

Commit 10896f3

Browse files
authored
Use NS_PATTERN_STR instead of compiled regex (#979)
2 parents d99e641 + b575db6 commit 10896f3

File tree

6 files changed

+14
-17
lines changed

6 files changed

+14
-17
lines changed

src/ssvc/namespaces.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
# subject to its own license.
2424
# DM24-0278
2525

26+
import re
2627
from enum import StrEnum
2728

2829
from ssvc.utils.defaults import MAX_NS_LENGTH, MIN_NS_LENGTH, X_PFX
29-
from ssvc.utils.patterns import NS_PATTERN
30+
from ssvc.utils.patterns import NS_PATTERN_STR
3031

3132
EXT_SEP = "/"
3233
FRAG_SEP = "#"
@@ -86,7 +87,7 @@ def validate(cls, value: str) -> str:
8687
ValueError: if the value is not a valid namespace
8788
8889
"""
89-
valid = NS_PATTERN.match(value)
90+
valid = re.match(NS_PATTERN_STR, value)
9091

9192
if valid:
9293
# pattern matches, so we can proceed with further checks

src/ssvc/utils/field_specs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from pydantic import Field
2828

2929
from ssvc.utils.defaults import MAX_NS_LENGTH, MIN_NS_LENGTH
30-
from ssvc.utils.patterns import NS_PATTERN, VERSION_PATTERN
30+
from ssvc.utils.patterns import NS_PATTERN_STR, VERSION_PATTERN
3131

3232
VersionString = Annotated[
3333
str,
@@ -50,7 +50,7 @@
5050
"x_example.test#test//.example.test#private-extension",
5151
"ssvc/de-DE/.example.organization#reference-arch-1",
5252
],
53-
pattern=NS_PATTERN.pattern,
53+
pattern=NS_PATTERN_STR,
5454
min_length=MIN_NS_LENGTH,
5555
max_length=MAX_NS_LENGTH,
5656
),

src/ssvc/utils/patterns.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
# subject to its own license.
2323
# DM24-0278
2424

25-
import re
26-
2725
# from https://semver.org/
2826
VERSION_PATTERN = r"^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$"
2927
"""A regular expression pattern for semantic versioning (semver)."""
@@ -81,5 +79,3 @@
8179

8280
# --- Combine all parts into the full namespace pattern ---
8381
NS_PATTERN_STR = rf"^{namespace}$"
84-
85-
NS_PATTERN = re.compile(NS_PATTERN_STR)

src/test/test_namespaces.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
# This Software includes and/or makes use of Third-Party Software each
1717
# subject to its own license.
1818
# DM24-0278
19-
19+
import re
2020
import unittest
2121

2222
from ssvc.namespaces import NameSpace, RESERVED_NS
23-
from ssvc.utils.patterns import NS_PATTERN
23+
from ssvc.utils.patterns import NS_PATTERN_STR
2424

2525

2626
class MyTestCase(unittest.TestCase):
@@ -41,7 +41,7 @@ def test_ns_pattern(self):
4141

4242
for ns in should_match:
4343
with self.subTest(ns=ns):
44-
self.assertTrue(NS_PATTERN.match(ns), ns)
44+
self.assertTrue(re.match(NS_PATTERN_STR, ns), ns)
4545

4646
should_not_match = [
4747
"",
@@ -62,8 +62,8 @@ def test_ns_pattern(self):
6262
failures = []
6363
for ns in should_not_match:
6464
with self.subTest(ns=ns):
65-
# re.search() to catch if NS_PATTERN is not anchored at start
66-
match = NS_PATTERN.search(ns)
65+
# re.search() to catch if NS_PATTERN_STR is not anchored at start
66+
match = re.search(NS_PATTERN_STR, ns)
6767
if match:
6868
failures.append(
6969
f"Unexpected match for '{ns}': {match.group(0)}"

src/test/test_namespaces_pattern.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
BASE_PATTERN,
2828
EXT_SEGMENT_PATTERN,
2929
LENGTH_CHECK_PATTERN,
30-
NS_PATTERN,
30+
NS_PATTERN_STR,
3131
)
3232

3333
logger = logging.getLogger(__name__)
@@ -98,7 +98,7 @@ def setUp(self):
9898

9999
def test_ns_pattern(self):
100100
self._test_successes_failures(
101-
NS_PATTERN.pattern, self.expect_fail, self.expect_success
101+
NS_PATTERN_STR, self.expect_fail, self.expect_success
102102
)
103103

104104
def test_base_pattern(self):

src/test/test_selections.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
from ssvc import selection
2525
from ssvc.selection import MinimalDecisionPointValue, SelectionList
26-
from ssvc.utils.patterns import NS_PATTERN, VERSION_PATTERN
26+
from ssvc.utils.patterns import NS_PATTERN_STR, VERSION_PATTERN
2727

2828

2929
class MyTestCase(unittest.TestCase):
@@ -61,7 +61,7 @@ def test_minimal_selection_init(self):
6161
self.assertIsInstance(self.s1.namespace, str)
6262
self.assertRegex(
6363
self.s1.namespace,
64-
NS_PATTERN,
64+
NS_PATTERN_STR,
6565
"Namespace does not match the required pattern",
6666
)
6767

0 commit comments

Comments
 (0)