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 union type bug in pipeline type-checker and enhance Entity ID type #511

Merged
merged 2 commits into from
Nov 17, 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
2 changes: 1 addition & 1 deletion lenskit/lenskit/data/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def create(cls, data: QueryInput) -> RecQuery:
assert_never(f"invalid type {type(data)}")


QueryInput: TypeAlias = RecQuery | EntityId | ItemList | np.integer | None
QueryInput: TypeAlias = RecQuery | EntityId | ItemList | None
"""
Types that can be converted to a query by :meth:`RecQuery.create`.
"""
2 changes: 1 addition & 1 deletion lenskit/lenskit/data/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
FeedbackType: TypeAlias = Literal["explicit", "implicit"]
"Types of feedback supported."

EntityId: TypeAlias = int | str | bytes
EntityId: TypeAlias = int | str | bytes | np.integer[Any] | np.string_
"Allowable entity identifier types."
NPEntityId: TypeAlias = np.integer[Any] | np.str_ | np.bytes_ | np.object_
"Allowable entity identifier types (NumPy version)"
Expand Down
5 changes: 3 additions & 2 deletions lenskit/lenskit/pipeline/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import re
import warnings
from importlib import import_module
from types import GenericAlias, NoneType
from types import GenericAlias, NoneType, UnionType
from typing import ( # type: ignore
Generic,
Protocol,
Expand Down Expand Up @@ -128,7 +128,8 @@ def is_compatible_data(obj: object, *targets: type | TypeVar) -> bool:
except TypeError:
pass

if get_origin(target) == Union:
origin = get_origin(target)
if origin == UnionType or origin == Union:
types = get_args(target)
if is_compatible_data(obj, *types):
return True
Expand Down
10 changes: 9 additions & 1 deletion lenskit/tests/pipeline/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from collections.abc import Iterable, Sequence
from pathlib import Path
from types import NoneType
from typing import TypeVar
from typing import Any, TypeVar

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -102,6 +102,14 @@ def test_numpy_typecheck():
assert not is_compatible_data(np.arange(10), NDArray[np.float64])


def test_numpy_scalar_typecheck():
assert is_compatible_data(np.int32(4270), np.integer[Any])


def test_numpy_scalar_typecheck2():
assert is_compatible_data(np.int32(4270), np.integer[Any] | int)


def test_pandas_typecheck():
assert is_compatible_data(pd.Series(["a", "b"]), ArrayLike)

Expand Down
Loading