openspg/python/knext/common/runnable.py

43 lines
1.2 KiB
Python
Raw Normal View History

2023-12-22 19:49:39 +08:00
# -*- coding: utf-8 -*-
# Copyright 2023 Ant Group CO., Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
# in compliance with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under the License
# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
# or implied.
from typing import TypeVar, Sequence, Type
2023-12-11 10:44:37 +08:00
2023-12-08 11:25:26 +08:00
from pydantic import BaseConfig, BaseModel
2023-12-11 10:44:37 +08:00
Other = TypeVar("Other")
Input = TypeVar("Input", contravariant=True)
Output = TypeVar("Output", covariant=True)
2023-12-08 11:25:26 +08:00
2023-12-11 10:44:37 +08:00
2023-12-18 13:46:44 +08:00
class Runnable(BaseModel):
2023-12-08 11:25:26 +08:00
2023-12-15 17:33:54 +08:00
_last: bool = False
2023-12-08 11:25:26 +08:00
@property
2023-12-11 10:44:37 +08:00
def input_types(self) -> Type[Input]:
2023-12-08 11:25:26 +08:00
return
@property
2023-12-11 10:44:37 +08:00
def output_types(self) -> Type[Output]:
2023-12-08 11:25:26 +08:00
return
2023-12-11 10:44:37 +08:00
def invoke(self, input: Input) -> Sequence[Output]:
2023-12-22 19:49:39 +08:00
raise NotImplementedError(f"`invoke` is not currently supported for {self.__class__.__name__}.")
2023-12-08 11:25:26 +08:00
2023-12-11 10:44:37 +08:00
def __rshift__(self, other: Other):
2023-12-08 11:25:26 +08:00
raise NotImplementedError("To be implemented in subclass")
class Config(BaseConfig):
2023-12-11 10:44:37 +08:00
arbitrary_types_allowed = True