Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
JsonPrimitive,
ends_with,
fractional,
parse_version,
normalize_version,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Renaming parse_version to normalize_version is a breaking change for consumers of this module. Given that this file is explicitly intended for backward compatibility (as noted in the header comment), it should continue to export parse_version as an alias.

Suggested change
normalize_version,
normalize_version,
normalize_version as parse_version,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about this one, is this necessary? I changed it locally but the pre-commit hook reverted it :D

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think it is is ok. this is an internal function, I don't think anyone relied on it. just to be a bit more carful let's add it as a change in the description.

sem_ver,
starts_with,
string_comp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"~unixsocket",
"~deprecated",
"~fractional-v1",
"~operator-errors",
]


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
"~deprecated",
"~fractional-v1",
"~operator-errors",
"~semver-v-prefix",
"~fractional-single-entry",
"~semver-numeric-context",
]
# TODO remove last 4 tags when adjusted flagd is released


def pytest_collection_modifyitems(config, items):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ def string_comp(
arg1, arg2 = args
if not isinstance(arg1, str):
logger.debug(f"incorrect argument for first argument, expected string: {arg1}")
return False
return None
Comment thread
leakonvalinka marked this conversation as resolved.
if not isinstance(arg2, str):
logger.debug(f"incorrect argument for second argument, expected string: {arg2}")
return False
return None

return comparator(arg1, arg2)

Expand All @@ -144,8 +144,8 @@ def sem_ver(data: dict, *args: JsonLogicArg) -> bool | None: # noqa: C901
arg1, op, arg2 = args

try:
v1 = parse_version(arg1)
v2 = parse_version(arg2)
v1 = normalize_version(arg1)
v2 = normalize_version(arg2)
except ValueError as e:
logger.exception(e)
return None
Expand All @@ -171,9 +171,15 @@ def sem_ver(data: dict, *args: JsonLogicArg) -> bool | None: # noqa: C901
return None


def parse_version(arg: typing.Any) -> semver.Version:
def normalize_version(arg: typing.Any) -> semver.Version:
version = str(arg)
if version.startswith(("v", "V")):
version = version[1:]

# Pad partial versions (e.g. "1" → "1.0.0", "1.2" → "1.2.0")
numeric_part = version.split("-")[0].split("+")[0]
dot_count = numeric_part.count(".")
if dot_count < 2:
version = numeric_part + ".0" * (2 - dot_count) + version[len(numeric_part) :]

return semver.Version.parse(version)
9 changes: 8 additions & 1 deletion tools/openfeature-flagd-core/tests/test_targeting.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def test_starts_with_no_args(self) -> None:

def test_starts_with_non_string(self) -> None:
result = starts_with({}, 123, "abc")
assert result is False
assert result is None


class TestEndsWith:
Expand All @@ -78,6 +78,10 @@ def test_ends_with_false(self) -> None:
result = ends_with({}, "hello world", "hello")
assert result is False

def test_ends_with_non_string(self) -> None:
result = ends_with({}, 123, "abc")
assert result is None


class TestSemVer:
def test_equal(self) -> None:
Expand Down Expand Up @@ -113,6 +117,9 @@ def test_minor_no_match(self) -> None:
def test_v_prefix(self) -> None:
assert sem_ver({}, "v2.0.0", "=", "2.0.0") is True

def test_partial_version(self) -> None:
Copy link
Copy Markdown
Member

@gruebel gruebel Apr 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you also add the v2.1 -. 2.1.0 test?

assert sem_ver({}, "2", "=", "2.0.0") is True

def test_invalid_version(self) -> None:
result = sem_ver({}, "not-a-version", "=", "1.0.0")
assert result is None
Expand Down
Loading