Skip to content

Commit 24cf843

Browse files
chore: Make Implementation.UNKNOWN._backend_version() safe (#3133)
Co-authored-by: Marco Edward Gorelli <[email protected]>
1 parent f124786 commit 24cf843

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

narwhals/_utils.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -632,9 +632,8 @@ def _import_native_namespace(module_name: str) -> ModuleType:
632632
def backend_version(implementation: Implementation, /) -> tuple[int, ...]:
633633
if not isinstance(implementation, Implementation):
634634
assert_never(implementation)
635-
if implementation is Implementation.UNKNOWN: # pragma: no cover
636-
msg = "Cannot return backend version from UNKNOWN Implementation"
637-
raise AssertionError(msg)
635+
if implementation is Implementation.UNKNOWN:
636+
return (0, 0, 0)
638637
into_version: ModuleType | str
639638
impl = implementation
640639
module_name = _IMPLEMENTATION_TO_MODULE_NAME.get(impl, impl.value)

tests/dependencies/imports_test.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,20 @@ def test_to_native_namespace_unknown() -> None:
5252
AssertionError, match="Cannot return native namespace from UNKNOWN Implementation"
5353
):
5454
impl.to_native_namespace()
55+
56+
57+
@pytest.mark.parametrize("impl", list(Implementation))
58+
def test_backend_version(impl: Implementation) -> None:
59+
version: tuple[int, ...] | None
60+
try:
61+
version = impl._backend_version()
62+
except (ModuleNotFoundError, ValueError) as err:
63+
# NOTE: See https://github.com/narwhals-dev/narwhals/issues/2786
64+
assert impl is not Implementation.UNKNOWN
65+
66+
with pytest.raises(type(err)):
67+
impl.to_native_namespace()
68+
version = None
69+
70+
if version is not None:
71+
assert version >= (0, 0, 0)

0 commit comments

Comments
 (0)