Skip to content

Commit 7a531db

Browse files
bearomorphismLee-W
authored andcommitted
refactor(TagRules): extract tag_formats property and simplify list comprehension
1 parent 93f0f27 commit 7a531db

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

commitizen/tags.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from collections.abc import Iterable, Sequence
66
from dataclasses import dataclass, field
77
from functools import cached_property
8+
from itertools import chain
89
from string import Template
910
from typing import TYPE_CHECKING, NamedTuple
1011

@@ -88,18 +89,22 @@ class TagRules:
8889
ignored_tag_formats: Sequence[str] = field(default_factory=list)
8990
merge_prereleases: bool = False
9091

92+
@property
93+
def tag_formats(self) -> Iterable[str]:
94+
return chain([self.tag_format], self.legacy_tag_formats)
95+
9196
@cached_property
9297
def version_regexes(self) -> list[re.Pattern]:
9398
"""Regexes for all legit tag formats, current and legacy"""
94-
tag_formats = [self.tag_format, *self.legacy_tag_formats]
95-
regexes = (self._format_regex(p) for p in tag_formats)
96-
return [re.compile(r) for r in regexes]
99+
return [re.compile(self._format_regex(f)) for f in self.tag_formats]
97100

98101
@cached_property
99102
def ignored_regexes(self) -> list[re.Pattern]:
100103
"""Regexes for known but ignored tag formats"""
101-
regexes = (self._format_regex(p, star=True) for p in self.ignored_tag_formats)
102-
return [re.compile(r) for r in regexes]
104+
return [
105+
re.compile(self._format_regex(f, star=True))
106+
for f in self.ignored_tag_formats
107+
]
103108

104109
def _format_regex(self, tag_pattern: str, star: bool = False) -> str:
105110
"""
@@ -240,10 +245,7 @@ def find_tag_for(
240245
) -> GitTag | None:
241246
"""Find the first matching tag for a given version."""
242247
version = self.scheme(version) if isinstance(version, str) else version
243-
possible_tags = set(
244-
self.normalize_tag(version, f)
245-
for f in (self.tag_format, *self.legacy_tag_formats)
246-
)
248+
possible_tags = set(self.normalize_tag(version, f) for f in self.tag_formats)
247249
candidates = [t for t in tags if t.name in possible_tags]
248250
if len(candidates) > 1:
249251
warnings.warn(

0 commit comments

Comments
 (0)