Skip to content

Commit

Permalink
re-introduce generic type on Component
Browse files Browse the repository at this point in the history
  • Loading branch information
mdekstrand committed Jan 11, 2025
1 parent d1afbcb commit 2ff7d48
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lenskit/lenskit/pipeline/_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class Pipeline:
_nodes: dict[str, Node[Any]]
_aliases: dict[str, Node[Any]]
_defaults: dict[str, Node[Any]]
_components: dict[str, PipelineFunction[Any]]
_components: dict[str, PipelineFunction[Any] | Component[Any]]
_hash: str | None = None
_last: Node[Any] | None = None
_anon_nodes: set[str]
Expand Down
8 changes: 4 additions & 4 deletions lenskit/lenskit/pipeline/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from typing import (
Any,
Callable,
Generic,
Mapping,
ParamSpec,
Protocol,
Expand All @@ -35,7 +36,6 @@

P = ParamSpec("P")
T = TypeVar("T")
Cfg = TypeVar("Cfg")
# COut is only return, so Component[U] can be assigned to Component[T] if U ≼ T.
COut = TypeVar("COut", covariant=True)
PipelineFunction: TypeAlias = Callable[..., COut]
Expand Down Expand Up @@ -130,7 +130,7 @@ def load_params(self, params: dict[str, object]) -> None:
raise NotImplementedError()


class Component:
class Component(Generic[COut]):
"""
Base class for pipeline component objects. Any component that is not just a
function should extend this class.
Expand Down Expand Up @@ -260,7 +260,7 @@ def __repr__(self) -> str:


def instantiate_component(
comp: str | type | FunctionType, config: dict[str, Any] | None
comp: str | type | FunctionType, config: Mapping[str, Any] | None
) -> Callable[..., object]:
"""
Utility function to instantiate a component given its class, function, or
Expand All @@ -281,7 +281,7 @@ def instantiate_component(
return comp
elif issubclass(comp, Component):
cfg = comp.validate_config(config)
return comp(cfg)
return comp(cfg) # type: ignore
else: # pragma: nocover
return comp() # type: ignore

Expand Down

0 comments on commit 2ff7d48

Please sign in to comment.