Skip to content

Commit

Permalink
fix: parse_version was not parsing duckdb pre-preleases correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli committed Jan 8, 2025
1 parent 373320e commit 4aacb5f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
9 changes: 5 additions & 4 deletions narwhals/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ def _is_iterable(arg: Any | Iterable[Any]) -> bool:
return isinstance(arg, Iterable) and not isinstance(arg, (str, bytes, Series))


def parse_version(version: Sequence[str | int]) -> tuple[int, ...]:
def parse_version(version: str) -> tuple[int, ...]:
"""Simple version parser; split into a tuple of ints for comparison.
Arguments:
Expand All @@ -382,9 +382,10 @@ def parse_version(version: Sequence[str | int]) -> tuple[int, ...]:
Parsed version number.
"""
# lifted from Polars
if isinstance(version, str): # pragma: no cover
version = version.split(".")
return tuple(int(re.sub(r"\D", "", str(v))) for v in version)
# [marco]: Take care of DuckDB pre-releases which end with e.g. `-dev4108`
# and pandas pre-releases which end with e.g. .dev0+618.gb552dc95c9
version = re.sub(r"(\D?dev.*$)", "", version)
return tuple(int(re.sub(r"\D", "", str(v))) for v in version.split("."))


def isinstance_or_issubclass(obj: Any, cls: Any) -> bool:
Expand Down
13 changes: 13 additions & 0 deletions tests/utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from pandas.testing import assert_series_equal

import narwhals.stable.v1 as nw
from narwhals.utils import parse_version
from tests.utils import PANDAS_VERSION
from tests.utils import get_module_version_as_tuple

Expand Down Expand Up @@ -271,3 +272,15 @@ def test_generate_temporary_column_name_raise() -> None:
match="Internal Error: Narwhals was not able to generate a column name with ",
):
nw.generate_temporary_column_name(n_bytes=1, columns=columns)


@pytest.mark.parametrize(
("version", "expected"),
[
("2020.1.2", (2020, 1, 2)),
("2020.1.2-dev123", (2020, 1, 2)),
("3.0.0.dev0+618.gb552dc95c9", (3, 0, 0)),
],
)
def test_parse_version(version: str, expected: tuple[int, ...]) -> None:
assert parse_version(version) == expected

0 comments on commit 4aacb5f

Please sign in to comment.