Skip to content

Commit 89dd44c

Browse files
committed
pipeline: fix typecheck for type aliases
1 parent 7a0c015 commit 89dd44c

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

src/lenskit/pipeline/_types.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from types import FunctionType, GenericAlias, NoneType, UnionType
1414
from typing import (
1515
Any,
16+
TypeAliasType,
1617
TypeVar,
1718
Union,
1819
_GenericAlias, # type: ignore # noqa: PLC2701
@@ -24,7 +25,7 @@
2425

2526
from lenskit.diagnostics import PipelineWarning, TypecheckWarning
2627

27-
type TypeExpr = type | UnionType
28+
type TypeExpr = type | UnionType | TypeAliasType
2829
"""
2930
Type for (resolved) type expressions.
3031
@@ -75,6 +76,10 @@ def is_compatible_type(typ: type, *targets: TypeExpr) -> bool:
7576
all of the targets, and ``True`` otherwise.
7677
"""
7778
for target in targets:
79+
# resolve type aliases
80+
if isinstance(target, TypeAliasType):
81+
target = target.__value__
82+
7883
# try a straight subclass check first, but gracefully handle incompatible types
7984
try:
8085
if issubclass(typ, target):
@@ -125,6 +130,10 @@ def is_compatible_data(obj: object, *targets: TypeExpr) -> bool:
125130
all of the targets, and ``True`` otherwise.
126131
"""
127132
for target in targets:
133+
# resolve type aliases
134+
if isinstance(target, TypeAliasType):
135+
target = target.__value__
136+
128137
# try a straight subclass check first, but gracefully handle incompatible types
129138
try:
130139
if isinstance(obj, target):

tests/pipeline/test_types.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
from pytest import mark, warns
2424

25-
from lenskit.data import Dataset, MatrixRelationshipSet, RelationshipSet
25+
from lenskit.data import MatrixRelationshipSet, QueryInput, RecQuery, RelationshipSet
2626
from lenskit.pipeline._types import (
2727
TypecheckWarning,
2828
import_path_string,
@@ -208,3 +208,13 @@ def test_is_instance_proto():
208208

209209
def test_is_subclass_proto():
210210
assert is_instance_or_subclass(list, Sequence)
211+
212+
213+
def test_query_subtype():
214+
assert is_compatible_type(RecQuery, QueryInput)
215+
216+
217+
def test_query_valid():
218+
query = RecQuery(47)
219+
assert is_compatible_data(query, RecQuery)
220+
assert is_compatible_data(query, QueryInput)

0 commit comments

Comments
 (0)