Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

refactor(TagRules): extract tag_formats property and simplify list comprehension #1549

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions commitizen/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from collections.abc import Iterable, Sequence
from dataclasses import dataclass, field
from functools import cached_property
from itertools import chain
from string import Template
from typing import TYPE_CHECKING, NamedTuple

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

@property
def tag_formats(self) -> Iterable[str]:
return chain([self.tag_format], self.legacy_tag_formats)

@cached_property
def version_regexes(self) -> list[re.Pattern]:
"""Regexes for all legit tag formats, current and legacy"""
tag_formats = [self.tag_format, *self.legacy_tag_formats]
regexes = (self._format_regex(p) for p in tag_formats)
return [re.compile(r) for r in regexes]
return [re.compile(self._format_regex(f)) for f in self.tag_formats]

@cached_property
def ignored_regexes(self) -> list[re.Pattern]:
"""Regexes for known but ignored tag formats"""
regexes = (self._format_regex(p, star=True) for p in self.ignored_tag_formats)
return [re.compile(r) for r in regexes]
return [
re.compile(self._format_regex(f, star=True))
for f in self.ignored_tag_formats
]

def _format_regex(self, tag_pattern: str, star: bool = False) -> str:
"""
Expand Down Expand Up @@ -240,10 +245,7 @@ def find_tag_for(
) -> GitTag | None:
"""Find the first matching tag for a given version."""
version = self.scheme(version) if isinstance(version, str) else version
possible_tags = set(
self.normalize_tag(version, f)
for f in (self.tag_format, *self.legacy_tag_formats)
)
possible_tags = set(self.normalize_tag(version, f) for f in self.tag_formats)
candidates = [t for t in tags if t.name in possible_tags]
if len(candidates) > 1:
warnings.warn(
Expand Down