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

fix: parse_version was not parsing duckdb pre-preleases correctly #1763

Merged
merged 1 commit into from
Jan 9, 2025
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
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
Loading