From 8af496d3f58fc20bc5e005e2e99d6eb2bc3d8d43 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Thu, 27 Apr 2023 07:26:39 +0200 Subject: [PATCH] fix #838: handle incorrectly passed empty tag regex as default --- src/setuptools_scm/__init__.py | 11 ++++++++++- src/setuptools_scm/version.py | 21 ++++++++++++++------- testing/test_basic_api.py | 7 +++++++ 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/setuptools_scm/__init__.py b/src/setuptools_scm/__init__.py index 96c83495..faea673b 100644 --- a/src/setuptools_scm/__init__.py +++ b/src/setuptools_scm/__init__.py @@ -6,6 +6,7 @@ import os import re +import warnings from typing import Any from typing import Pattern from typing import TYPE_CHECKING @@ -133,7 +134,15 @@ def get_version( version_cls = _validate_version_cls(version_cls, normalize) del normalize if isinstance(tag_regex, str): - tag_regex = re.compile(tag_regex) + if tag_regex == "": + warnings.warn( + DeprecationWarning( + "empty regex for tag regex is invalid, using default" + ) + ) + tag_regex = DEFAULT_TAG_REGEX + else: + tag_regex = re.compile(tag_regex) config = Configuration(**locals()) maybe_version = _get_version(config) diff --git a/src/setuptools_scm/version.py b/src/setuptools_scm/version.py index 128533cd..8cf48e82 100644 --- a/src/setuptools_scm/version.py +++ b/src/setuptools_scm/version.py @@ -38,10 +38,8 @@ def _parse_version_tag( tag: str | object, config: _config.Configuration ) -> dict[str, str] | None: - tagstring = tag if isinstance(tag, str) else str(tag) - match = config.tag_regex.match(tagstring) + match = config.tag_regex.match(str(tag)) - result = None if match: key: str | int if len(match.groups()) == 1: @@ -49,14 +47,23 @@ def _parse_version_tag( else: key = "version" + full = match.group(0) + log.debug("%r %r %s", tag, config.tag_regex, match) + log.debug( + "key %s data %s, %s, %r", key, match.groupdict(), match.groups(), full + ) result = { "version": match.group(key), - "prefix": match.group(0)[: match.start(key)], - "suffix": match.group(0)[match.end(key) :], + "prefix": full[: match.start(key)], + "suffix": full[match.end(key) :], } - log.debug(f"tag '{tag}' parsed to {result}") - return result + log.debug("tag %r parsed to %r", tag, result) + return result + else: + log.debug("tag %r did not parse", tag) + + return None def callable_or_entrypoint(group: str, callable_or_name: str | Any) -> Any: diff --git a/testing/test_basic_api.py b/testing/test_basic_api.py index f1b4c4f1..6fd39bde 100644 --- a/testing/test_basic_api.py +++ b/testing/test_basic_api.py @@ -121,6 +121,13 @@ def test_empty_pretend_version_named( assert res.stdout == "12.34" +def test_get_version_blank_tag_regex() -> None: + with pytest.warns( + DeprecationWarning, match="empty regex for tag regex is invalid, using default" + ): + setuptools_scm.get_version(tag_regex="") + + @pytest.mark.parametrize( "version", ["1.0", "1.2.3.dev1+ge871260", "1.2.3.dev15+ge871260.d20180625", "2345"] )