Skip to content
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

[Backport 1.1 latest] Avoid packaging for semver #10684

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions .changes/unreleased/Fixes-20230201-154418.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Fixes
body: Remove pin on packaging and stop using it for prerelease comparisons
time: 2023-02-01T15:44:18.279158-05:00
custom:
Author: gshank
Issue: "6834"
6 changes: 6 additions & 0 deletions .changes/unreleased/Fixes-20230224-001338.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Fixes
body: Fix semver comparison logic by ensuring numeric values
time: 2023-02-24T00:13:38.23242+01:00
custom:
Author: jtcohen6
Issue: "7039"
47 changes: 39 additions & 8 deletions core/dbt/semver.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
from dataclasses import dataclass
import re
import warnings
from typing import List

from packaging import version as packaging_version

from dbt.exceptions import VersionsNotCompatibleException
import dbt.utils

Expand Down Expand Up @@ -70,6 +67,11 @@ class VersionSpecification(dbtClassMixin):
_VERSION_REGEX = re.compile(_VERSION_REGEX_PAT_STR, re.VERBOSE)


def _cmp(a, b):
"""Return negative if a<b, zero if a==b, positive if a>b."""
return (a > b) - (a < b)


@dataclass
class VersionSpecifier(VersionSpecification):
def to_version_string(self, skip_matcher=False):
Expand Down Expand Up @@ -142,13 +144,19 @@ def compare(self, other):
return 1
if b is None:
return -1
# This suppresses the LegacyVersion deprecation warning
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=DeprecationWarning)
if packaging_version.parse(a) > packaging_version.parse(b):

# Check the prerelease component only
prcmp = self._nat_cmp(a, b)
if prcmp != 0: # either -1 or 1
return prcmp
# else is equal and will fall through

else: # major/minor/patch, should all be numbers
if int(a) > int(b):
return 1
elif packaging_version.parse(a) < packaging_version.parse(b):
elif int(a) < int(b):
return -1
# else is equal and will fall through

equal = (
self.matcher == Matchers.GREATER_THAN_OR_EQUAL
Expand Down Expand Up @@ -212,6 +220,29 @@ def is_upper_bound(self):
def is_exact(self):
return self.matcher == Matchers.EXACT

@classmethod
def _nat_cmp(cls, a, b):
def cmp_prerelease_tag(a, b):
if isinstance(a, int) and isinstance(b, int):
return _cmp(a, b)
elif isinstance(a, int):
return -1
elif isinstance(b, int):
return 1
else:
return _cmp(a, b)

a, b = a or "", b or ""
a_parts, b_parts = a.split("."), b.split(".")
a_parts = [int(x) if re.match(r"^\d+$", x) else x for x in a_parts]
b_parts = [int(x) if re.match(r"^\d+$", x) else x for x in b_parts]
for sub_a, sub_b in zip(a_parts, b_parts):
cmp_result = cmp_prerelease_tag(sub_a, sub_b)
if cmp_result != 0:
return cmp_result
else:
return _cmp(len(a), len(b))


@dataclass
class VersionRange:
Expand Down
2 changes: 1 addition & 1 deletion core/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"mashumaro==2.9",
"minimal-snowplow-tracker==0.0.2",
"networkx>=2.3,<2.8.4",
"packaging>=20.9,<22.0",
"packaging>20.9",
"sqlparse>=0.2.3,<0.4.4",
"dbt-extractor~=0.4.1",
"typing-extensions>=3.7.4",
Expand Down
20 changes: 16 additions & 4 deletions test/unit/test_semver.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,25 @@ def test__resolve_to_specific_version(self):
['1.0.0', '1.1.0a1', '1.1.0', '1.2.0a1', '1.2.0']),
'1.1.0')

self.assertEqual(
resolve_to_specific_version(
# https://github.com/dbt-labs/dbt-core/issues/7039
# 10 is greater than 9
create_range('>0.9.0', '<0.10.0'),
['0.9.0', '0.9.1', '0.10.0']),
'0.9.1')

def test__filter_installable(self):
assert filter_installable(
installable = filter_installable(
['1.1.0', '1.2.0a1', '1.0.0','2.1.0-alpha','2.2.0asdf','2.1.0','2.2.0','2.2.0-fishtown-beta','2.2.0-2'],
install_prerelease=True
) == ['1.0.0', '1.1.0', '1.2.0a1','2.1.0-alpha','2.1.0','2.2.0asdf','2.2.0-fishtown-beta','2.2.0-2','2.2.0']
)
expected = ['1.0.0', '1.1.0', '1.2.0a1','2.1.0-alpha','2.1.0','2.2.0-2','2.2.0asdf','2.2.0-fishtown-beta','2.2.0']
assert installable == expected

assert filter_installable(
installable = filter_installable(
['1.1.0', '1.2.0a1', '1.0.0','2.1.0-alpha','2.2.0asdf','2.1.0','2.2.0','2.2.0-fishtown-beta'],
install_prerelease=False
) == ['1.0.0', '1.1.0','2.1.0','2.2.0']
)
expected = ['1.0.0', '1.1.0','2.1.0','2.2.0']
assert installable == expected
Loading